您的位置:首页 > 其它

uC /OS-II中任务的挂起和恢复

2015-03-28 14:40 399 查看

任务的挂起

1.定义:什么是挂起一个任务?


所谓任务的挂起,就是停止这个任务的运行

2.方法:如何挂起一个任务?


通过调用系统提供的
OSTaskSuspend ()
函数来挂起自身或除空闲任务之外的其他任务。

3.函数分析


/*
*********************************************************************************************************
*                                            SUSPEND A TASK
*
* Description: This function is called to suspend a task.  The task can be the calling task if the
*              priority passed to OSTaskSuspend() is the priority of the calling task or OS_PRIO_SELF.
*
* Arguments  : prio     is the priority of the task to suspend.  If you specify OS_PRIO_SELF, the
*                       calling task will suspend itself and rescheduling will occur.
*
* Returns    : OS_NO_ERR                if the requested task is suspended
*              OS_TASK_SUSPEND_IDLE     if you attempted to suspend the idle task which is not allowed.
*              OS_PRIO_INVALID          if the priority you specify is higher that the maximum allowed
*                                       (i.e. >= OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
*              OS_TASK_SUSPEND_PRIO     if the task to suspend does not exist
*
* Note       : You should use this function with great care.  If you suspend a task that is waiting for
*              an event (i.e. a message, a semaphore, a queue ...) you will prevent this task from
*              running when the event arrives.
*********************************************************************************************************
*/

#if OS_TASK_SUSPEND_EN > 0
INT8U  OSTaskSuspend (INT8U prio)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
OS_CPU_SR  cpu_sr;
#endif
BOOLEAN    self;
OS_TCB    *ptcb;

#if OS_ARG_CHK_EN > 0
if (prio == OS_IDLE_PRIO) {                                 /* Not allowed to suspend idle task    */
return (OS_TASK_SUSPEND_IDLE);
}
if (prio >= OS_LOWEST_PRIO && prio != OS_PRIO_SELF) {       /* Task priority valid ?               */
return (OS_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
if (prio == OS_PRIO_SELF) {                                 /* See if suspend SELF                 */
prio = OSTCBCur->OSTCBPrio;
self = TRUE;
} else if (prio == OSTCBCur->OSTCBPrio) {                   /* See if suspending self              */
self = TRUE;
} else {
self = FALSE;                                           /* No suspending another task          */
}
ptcb = OSTCBPrioTbl[prio];
if (ptcb == (OS_TCB *)0) {                                  /* Task to suspend must exist          */
OS_EXIT_CRITICAL();
return (OS_TASK_SUSPEND_PRIO);
}
if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0x00) { /* Make task not ready                 */
OSRdyGrp &= ~ptcb->OSTCBBitY;
}
ptcb->OSTCBStat |= OS_STAT_SUSPEND;                         /* Status of task is 'SUSPENDED'       */
OS_EXIT_CRITICAL();
if (self == TRUE) {                                         /* Context switch only if SELF         */
OS_Sched();
}
return (OS_NO_ERR);
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  os