最近刚刚学习ucosii,到了恢复任务函数OSTaskResume()的时候查了一些人的理解,感觉没有到点子上,现在我按自己的理解过一遍,先上代码:
<pre name="code" class="cpp">#if OS_TASK_SUSPEND_EN > 0
INT8U OSTaskResume (INT8U prio)
{
OS_TCB *ptcb;
#if OS_CRITICAL_METHOD == 3 /* Storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
#if OS_ARG_CHK_EN > 0
if (prio >= OS_LOWEST_PRIO) { /* Make sure task priority is valid */
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
ptcb = OSTCBPrioTbl[prio];
if (ptcb == (OS_TCB *)0) { /* Task to suspend must exist */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_RESUME_PRIO);
}
if (ptcb == OS_TCB_RESERVED) { /* See if assigned to Mutex */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) != OS_STAT_RDY) { /* Task must be suspended */
ptcb->OSTCBStat &= ~(INT8U)OS_STAT_SUSPEND; /* Remove suspension */
if (ptcb->OSTCBStat == OS_STAT_RDY) { /* See if task is now ready */
if (ptcb->OSTCBDly == 0) {
OSRdyGrp |= ptcb->OSTCBBitY; /* Yes, Make task ready to run */
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OS_EXIT_CRITICAL();
if (OSRunning == OS_TRUE) {
OS_Sched(); /* Find new highest priority task */
}
} else {
OS_EXIT_CRITICAL();
}
} else { /* Must be pending on event */
OS_EXIT_CRITICAL();
}
return (OS_ERR_NONE);
}
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_SUSPENDED);<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">} </span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">#endif</span>
其实这个函数的核心功能就是这一段:
if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) != OS_STAT_RDY) { /* Task must be suspended */ ptcb->OSTCBStat &= ~(INT8U)OS_STAT_SUSPEND; /* Remove suspension */ if (ptcb->OSTCBStat == OS_STAT_RDY) { /* See if task is now ready */ if (ptcb->OSTCBDly == 0) { OSRdyGrp |= ptcb->OSTCBBitY; /* Yes, Make task ready to run */ OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX; OS_EXIT_CRITICAL(); if (OSRunning == OS_TRUE) { OS_Sched(); /* Find new highest priority task */ }
</pre><pre name="code" class="cpp" style="font-size: 18px; font-weight: bold;">也就是:判断这个任务是否被挂起——>取消任务在控制块中挂起的记录——>恢复任务就绪表——>调用任务调度器OS_Sched()进行一次任务调度——>返回。
其余的主要是判断任务的优先级别是否合法,任务是否存在等等,下图是函数OSTaskResume()的流程图:<span style="font-family: Arial, Helvetica, sans-serif;">说到底ucosii的内核就是用C语言写的,无非就是比我们常见的C语言程序多了些指针、结构体,以及命明比较复杂,但是仔细看还是那些语法,先从大概方向判断要干什么,然后从每条语句看其所要表达的意思,就是这么简单。</span>