就绪(Ready):
该任务在就绪列表中,只等待CPU。
运行(Running):
该任务正在执行。
阻塞(Blocked):
该任务不在就绪列表中。
包含任务被挂起、任务被延时、任务正在等待信号量、读写队列或者等待读写事件等。
任务合法性(TaskId)及任务状态校验:判断任务序号以及任务当前状态是否确实为挂起。
改变任务状态:将任务的suspend状态位清掉
起用任务调度:如果任务被阻塞,则调起LOS_Schedule进行调度。
初始化任务队列
加入任务
循环处理任务
销毁任务队列
void *taosProcessSchedQueue(void *param) {
SSchedMsg msg;
SSchedQueue *pSched = (SSchedQueue *)param;
while (1) {
if (sem_wait(&pSched->fullSem) != 0) {
pError('wait %s fullSem failed, errno:%d, reason:%s', pSched->label, errno, strerror(errno));
if (errno == EINTR) {
/* sem_wait is interrupted by interrupt, ignore and continue */
continue;
}
}
if (pthread_mutex_lock(&pSched->queueMutex) != 0)
pError('lock %s queueMutex failed, reason:%s', pSched->label, strerror(errno));
msg = pSched->queue[pSched->fullSlot];
memset(pSched->queue pSched->fullSlot, 0, sizeof(SSchedMsg));
pSched->fullSlot = (pSched->fullSlot 1) % pSched->queueSize;//从队尾取出消息不断处理
if (pthread_mutex_unlock(&pSched->queueMutex) != 0)
pError('unlock %s queueMutex failed, reason:%s
', pSched->label, strerror(errno));
if (sem_post(&pSched->emptySem) != 0)
pError('post %s emptySem failed, reason:%s
', pSched->label, strerror(errno));
if (msg.fp)
(*(msg.fp))(&msg);
else if (msg.tfp)
(*(msg.tfp))(msg.ahandle, msg.thandle);
}
}
int taosScheduleTask(void *qhandle, SSchedMsg *pMsg) {
SSchedQueue *pSched = (SSchedQueue *)qhandle;
if (pSched == NULL) {
pError('sched is not ready, msg:%p is dropped', pMsg);
return 0;
}
if (sem_wait(&pSched->emptySem) != 0) pError('wait %s emptySem failed, reason:%s', pSched->label, strerror(errno));
if (pthread_mutex_lock(&pSched->queueMutex) != 0)
pError('lock %s queueMutex failed, reason:%s', pSched->label, strerror(errno));
pSched->queue[pSched->emptySlot] = *pMsg;
pSched->emptySlot = (pSched->emptySlot 1) % pSched->queueSize;
if (pthread_mutex_unlock(&pSched->queueMutex) != 0)
pError('unlock %s queueMutex failed, reason:%s', pSched->label, strerror(errno));
if (sem_post(&pSched->fullSem) != 0) pError('post %s fullSem failed, reason:%s', pSched->label, strerror(errno));
return 0;
}
软件定时器是系统资源,在模块初始化的时候已经分配了一块连续的内存,系统支持的最大定时器个数可以在los_config.h文件中配置。
软件定时器使用了系统的一个队列和任务资源,软件定时器的触发遵循队列规则,先进先出。
定时时间短的定时器总是比定时时间长的靠近队列头,满足优先被触发的准则。
软件定时器以Tick为基本计时单位,当用户创建并启动一个软件定时器时,Huawei LiteOS会根据当前系统Tick时间及用户设置的定时间隔确定该定时器的到期Tick时间,并将该定时器控制结构挂入计时全局链表。
当Tick中断到来时,在Tick中断处理函数中扫描软件定时器的计时全局链表,看是否有定时器超时,若有则将超时的定时器记录下来。
Tick处理结束后,软件定时器任务(优先级为最高)被唤醒,在该任务中调用之前记录下来的超时定时器的处理函数。
首先明确鸿蒙的定时器是为了节省硬件定时器资源而设计的。
由于硬件定时器往往数量有限而系统实际运行中,对于定时器的需求往往高于硬件定时器的数量,所以操作系统都会实现软件定时器以满足用户需求。
先启动硬件定时器,注册硬件定时器的tick事件,也就是硬件定时器到时发生tick时会调用软件定时器的处理函数。
将在同一时刻到期的timer放在同一链表中。
在硬件产生tick事件时,取出当时到期的定时器列表,并顺序调起链表内所有到时定时器的处理函数。
以上函数的运行原理动画解析如下:
联系客服