下图是执行结果。
如果我将OSTimeDly(100);替换成for(;;)那么这个2个任务只有一个可以运行了。原因就是OSTimeDly(100);函数。我们看看OSTimeDly(100);的说明
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
void OSTimeDly (INT16U ticks)
-
{
-
#if OS_CRITICAL_METHOD == 3 //中断函数被设定为模式3
-
OS_CPU_SR cpu_sr;
-
#endif
-
-
-
if (ticks > 0) {
-
OS_ENTER_CRITICAL();
-
if ((OSRdyTbl[OSTCBCur->OSTCBY] &= ~OSTCBCur->OSTCBBitX) == 0) {
-
OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
-
}
-
-
OSTCBCur->OSTCBDly = ticks;
-
OS_EXIT_CRITICAL();
-
OS_Sched();
-
}
-
}
/*
*********************************************************************************************************
* DELAY TASK 'n' TICKS
*
* Description: This function is called to delay execution of the currently running task until the
* specified number of system ticks expires. This, of course, directly equates to delaying
* the current task for some time to expire. No delay will result If the specified delay is
* 0. If the specified delay is greater than 0 then, a context switch will result.
*
* Arguments : ticks is the time delay that the task will be suspended in number of clock 'ticks'.
* Note that by specifying 0, the task will not be delayed.
*
* Returns : none
*********************************************************************************************************
*/
void OSTimeDly (INT32U ticks)
{
INT8U y;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
return;
}
if (OSLockNesting > 0u) { /* See if called with scheduler locked */
return;
}
if (ticks > 0u) { /* 0 means no delay! */
OS_ENTER_CRITICAL();
y = OSTCBCur->OSTCBY; /* Delay current task */
OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
if (OSRdyTbl[y] == 0u) {
OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
}
OSTCBCur->OSTCBDly = ticks; /* Load ticks in TCB */
OS_EXIT_CRITICAL();
OS_Sched(); /* Find next task to run! */
}
}
不多说了,就是
OSTimeDly (100);可以使得当前任务进入到挂起状态,然后执行一次任务调度,然后执行下一个优先级最高的就绪任务,这样就使得2个任务可以交替进行了。
如果将
OSTimeDly (100);替换为for(;;)该任务就无法挂起,另外一个任务就无法得到执行。如下图,我将任务2的延时换成了for(j=0;j<99999999;j++);执行结果如下。
可以看到一旦任务2得到执行,它会一直占用CPU,即使它的优先级只是6,低于任务1的优先级5。
阅读(5884) | 评论(0) | 转发(0) |