您的位置:首页 > 其它

FreeRTOS源码解析 -> vTaskDelete()

2014-12-31 17:06 465 查看
vTaskDelete()API 函数
         任务可以使用API函数vTaskDelete()删除自己或其它任务。
         任务被删除后就不复存在,也不会再进入运行态。
         空闲任务的责任是要将分配给已删除任务的内存释放掉。因此有一点很重要,那就是使用vTaskDelete() API函数的任务千万不能把空闲任务的执行时间饿死。

         需要说明一点,只有内核为任务分配的内存空间才会在任务被删除后自动回收。任务自己占用的内存或资源需要由应用程序自己显式地释放。

#if ( INCLUDE_vTaskDelete == 1 )

void vTaskDelete( xTaskHandle pxTaskToDelete )
{
tskTCB *pxTCB;

/*进入临界区*/
taskENTER_CRITICAL();
{
/* Ensure a yield is performed if the current task is being
deleted. */
//如果要删除的任务就是当前在运行的任务
if( pxTaskToDelete == pxCurrentTCB )
{
pxTaskToDelete = NULL;
}

/* If null is passed in here then we are deleting ourselves. */
//得到要删除的任务的句柄
//#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) )
pxTCB = prvGetTCBFromHandle( pxTaskToDelete );

/* Remove task from the ready list and place in the	termination list.
This will stop the task from be scheduled.  The idle task will check
the termination list and free up any memory allocated by the
scheduler for the TCB and stack. */
//从ready列表中删除
vListRemove( &( pxTCB->xGenericListItem ) );

//从event列表中删除(防止任务收到事件后变为就绪态)
/* Is the task waiting on an event also? */
if( pxTCB->xEventListItem.pvContainer )
{
vListRemove( &( pxTCB->xEventListItem ) );
}

/*将pxTCB加入一个新链表,用来在删除动作完成后,在idle task中释放删除任务的内存*/
/*vTaskDelete()只是在链表中把任务删了,保证任务不会再运行,还要释放内存*/
vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );

/* Increment the ucTasksDeleted variable so the idle task knows
there is a task that has been deleted and that it should therefore
check the xTasksWaitingTermination list. */
++uxTasksDeleted;

/* Increment the uxTaskNumberVariable also so kernel aware debuggers
can detect that the task lists need re-generating. */
uxTaskNumber++;

traceTASK_DELETE( pxTCB );
}
/*离开临界区*/
taskEXIT_CRITICAL();

/* Force a reschedule if we have just deleted the current task. */
//如果删除了当前正在运行的任务,强制产生一次调度
if( xSchedulerRunning != pdFALSE )
{
if( ( void * ) pxTaskToDelete == NULL )
{
/*强制上下文切换*/
portYIELD_WITHIN_API();
}
}
}

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息