FREERTOS之任务的挂起和唤醒

2009-05-13 声明:本篇文章主要参考了http://blog.ednchina.com/bluehacker  
它把所有挂起的任务加到 xSuspendedTaskList 中,而且一旦调用 vTaskSuspend() 函数挂起一个任务,该任务就将从所有它原先连入的链表中删除(包括就绪表,延时表和它等待的事件链表),也就是说,一旦一个任务被挂起,它将取消先前它的延时和对事件的等待。
void vTaskSuspend( xTaskHandle pxTaskToSuspend )
{
        tskTCB *pxTCB;
 
        taskENTER_CRITICAL();
        {
            /* Ensure a yield is performed if the current task is being
            suspended. */
            if( pxTaskToSuspend == pxCurrentTCB )
            {
                pxTaskToSuspend = NULL; //token
            }
 
            /* 得到对应任务tcb */
            pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
 
            traceTASK_SUSPEND( pxTaskToSuspend );
 
            /* 把任务从就绪表或者延时链表中删除 */
            vListRemove( &( pxTCB->xGenericListItem ) );
 
            /* 如果任务也在等待某事件,则取消等待 */
            if( pxTCB->xEventListItem.pvContainer )
            {
                vListRemove( &( pxTCB->xEventListItem ) );
            }
           // 插到xSuspendedTaskList
            vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
        }
        taskEXIT_CRITICAL();
 
        /* 如果挂起的是当前任务,则调度 */
        if( ( void * ) pxTaskToSuspend == NULL )
        {
            taskYIELD();
        }
    }
 
相反的唤醒就是把任务从xSuspendedTaskList中删除,加到对应的就绪链表中(根据任务的优先级),然后如果唤醒的任务优先级高于当前任务优先级,则调度。
void vTaskResume( xTaskHandle pxTaskToResume )
{
        tskTCB *pxTCB;
 
        /* Remove the task from whichever list it is currently in, and place
        it in the ready list. */
        pxTCB = ( tskTCB * ) pxTaskToResume;
 
        /* The parameter cannot be NULL as it is impossible to resume the
        currently executing task. */
        if( pxTCB != NULL )
        {
            taskENTER_CRITICAL();
            {
                if( prvIsTaskSuspended( pxTCB ) == pdTRUE )
                {
                    traceTASK_RESUME( pxTCB );
 
                    /* As we are in a critical section we can access the ready
                    lists even if the scheduler is suspended. */
                    vListRemove(  &( pxTCB->xGenericListItem ) );
                    prvAddTaskToReadyQueue( pxTCB );
 
                    /* We may have just resumed a higher priority task. */
                    if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
                    {
                        /* This yield may not cause the task just resumed to run, butwill leave the lists in the correct state for the next yield. */
                        taskYIELD();
                    }
                }
            }
            taskEXIT_CRITICAL();
        }
    }
 

你可能感兴趣的:(职场,休闲,freertos)