《FreeRTOS使命删除篇》

[复制链接]
发表于 2025-12-21 14:16:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
       
  • 先容FreeRTOS使命删除函数的实行过程,逐行分析源代码。   
  • 要利用使命删除函数vTaskDelete,必要设置宏INCLUDE_vTaskDelete为1。   
  • 删除使命函数vTaskDelete的参数为待删除使命的使命句柄(即使命控制块)。   
  • 当函数参数为NULL时,代表删除的是调用函数的使命自己(即正在运行的使命)。  
  源码

  1. /* 示例,在正在运行的任务中调用任务删除函数。*/
  2. vTaskDelete(NULL);
复制代码
  1. /* 任务删除 */
  2. #if ( INCLUDE_vTaskDelete == 1 ) /* 需要INCLUDE_vTaskDelete宏配置为1 */
  3.     void vTaskDelete( TaskHandle_t xTaskToDelete ) /* 函数参数为待删除任务的任务句柄,可以用NULL代替当前任务句柄。 */
  4.     {
  5.    
  6.    
  7.         TCB_t * pxTCB;
  8.         taskENTER_CRITICAL(); /* 进入临界区,本质是关闭全局中断。 */
  9.         {
  10.    
  11.    
  12.             /* If null is passed in here then it is the calling task that is
  13.              * being deleted.
  14.              * 如果在此处传递NULL,则删除的是调用函数的任务本身。
  15.              */
  16.             pxTCB = prvGetTCBFromHandle( xTaskToDelete );
  17.             /* Remove task from the ready/delayed list.
  18.              * 从就绪/延迟列表中删除任务
  19.              * 如果删除后列表中没有列表项,则uxListRemove返回0
  20.              */
  21.             /* 将任务控制块的成员状态列表项xStateListItem从列表中移除 */
  22.             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  23.             {
  24.    
  25.    
  26.                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  27.             }
  28.             else
  29.             {
  30.    
  31.    
  32.                 mtCOVERAGE_TEST_MARKER();
  33.             }
  34.             /* Is the task waiting on an event also?
  35.              * 任务是否也在等待事件?
  36.              * 任务控制块的成员xEventListItem是否挂载在其他列表中
  37.              */
  38.             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  39.             {
  40.    
  41.    
  42.                 /* 将事件列表项从等待事件列表中移出 */
  43.                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  44.             }
  45.             else
  46.             {
  47.    
  48.    
  49.                 mtCOVERAGE_TEST_MARKER();
  50.             }
  51.             /* Increment the uxTaskNumber also so kernel aware debuggers can
  52.              * detect that the task lists need re-generating.  This is done before
  53.              * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
  54.              * not return.
  55.              * 同时自增uxTaskNumber,以便内核感知调试器可以检测到任务列表需要重新生成。
  56.              * 这是在portPRE_TASK_DELETE_HOOK之前完成的,因为在Windows端口中,宏不会返回
  57.              */
  58.             uxTaskNumber++; /* 任务列表需要重新生成 */
  59.             /* 删除的是调用任务本身时 */
  60.             if( pxTCB == pxCurrentTCB )
  61.             {
  62.    
  63.    
  64.                 /* A task is deleting itself.  This cannot complete within the
  65.                  * task itself, as a context switch to another task is required.
  66.                  * Place the task in the termination list.  The idle task will
  67.                  * check the termination list and free up any memory allocated by
  68.                  * the scheduler for the TCB and stack of the deleted task.
  69.                  * 一个任务正在删除自己。
  70.                  * 这无法在任务本身内完成,因为需要切换到另一个任务的上下文。
  71.                  * 将任务放入等待结束列表中。
  72.                  * 空闲任务将检查等待结束列表,并释放调度器为准备删除任务分配的TCB和堆栈内存。
  73.                  */
  74.                 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
  75.                 /* Increment the ucTasksDeleted variable so the idle task knows
  76.                  * there is a task that has been deleted and that it should therefore
  77.                  * check the xTasksWaitingTermination list.
  78.                  * 递增ucTasksDeleted变量,以便空闲任务知道有一个任务已被删除,
  79.                  * 因此它应该检查xTasksWaitingTermination列表。
  80.                  */
  81.                 ++uxDeletedTasksWaitingCleanUp;
  82.                 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
  83.                  * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port.
  84.                  * 在调用portPRE_TASK_delete_hook之前调用delete钩子,
  85.                  * 因为portPRE_TAS K_delete_ hook在Win32端口中不返回。
  86.                  */
  87.                 traceTASK_DELETE( pxTCB );
  88.                 /* The pre-delete hook is primarily for the Windows simulator,
  89.                  * in which Windows specific clean up operations are performed,
  90.                  * after which it is not possible to yield away from this task -
  91.                  * hence xYieldPending is used to latch that a context switch is
  92.                  * required.
  93.                  * 预删除挂钩主要用于Windows模拟器,
  94.                  * 在该模拟器中执行特定于Windows的清理操作,之后不可能放弃此任务,
  95.                  * 因此xYieldPending用于锁定需要进行上下文切换。
  96.                  */
  97.                 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
  98.             }
  99.             else
  100.             {
  101.    
  102.    
  103.                 --uxCurrentNumberOfTasks; /* 当前任务个数自减一 */
  104.                 traceTASK_DELETE( pxTCB );
  105.                 /* Reset the next expected unblock time in case it referred to
  106.                  * the task that has just been deleted.
  107.                  * 重置下一个预期的解锁时间,以防它引用了刚刚被删除的任务
  108.                  */
  109.                 prvResetNextTaskUnblockTime()
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表