FreeRTOS源码解析 -> vTaskSuspend

    “挂起(suspended)”是非运行状态的子状态。处于挂起状态的任务对调度器而言是不可见的。让一个任务进入挂起状态的唯一办法就是调用vTaskSuspend() API函数;而把一个挂起状态的任务唤醒的唯一途径就是调用vTaskResume()或vTaskResumeFromISR() API函数。大多数应用程序中都不会用到挂起状态。

#if ( INCLUDE_vTaskSuspend == 1 )

	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;
			}

			/* If null is passed in here then we are suspending ourselves. */
			pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
			//和删除类似的方法,都是通过上面两步找到实际要操作的task

			traceTASK_SUSPEND( pxTCB );

			/* Remove task from the ready/delayed list and place in the	suspended list. */
			vListRemove( &( pxTCB->xGenericListItem ) );

			/* Is the task waiting on an event also? */
			
			/* 看一下要挂起的task是否在阻塞态列表中有恢复到ready的event */
			if( pxTCB->xEventListItem.pvContainer )
			{
				//有的话删除,不让他通过event进入ready状态
				vListRemove( &( pxTCB->xEventListItem ) );
			}

            //当前任务插入挂起列表最后
			vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
		}
		//开中断
		taskEXIT_CRITICAL();

        //如果挂起的是当前正在运行的任务
		if( ( void * ) pxTaskToSuspend == NULL )
		{		
			/* 并且调度器是运行的,那么马上进行一次调度 */
			if( xSchedulerRunning != pdFALSE )
			{
				/* We have just suspended the current task. */
				/*强制上下文切换*/
				portYIELD_WITHIN_API();
			}
			
			//调度器没有在运行
			else
			{
				/* The scheduler is not running, but the task that was pointed
				to by pxCurrentTCB has just been suspended and pxCurrentTCB
				must be adjusted to point to a different task. */
				//当前只有一个task,并且这个任务此时被挂起了
				if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1U )
				{
					/* No other tasks are defined, so set pxCurrentTCB back to
					NULL so when the next task is created pxCurrentTCB will
					be set to point to it no matter what its relative priority
					is. */
					pxCurrentTCB = NULL;
				}
				else
				{
					vTaskSwitchContext();
				}
			}
		}
	}

#endif


你可能感兴趣的:(FreeRTOS)