voidvTaskSwitchContext(void){if(uxSchedulerSuspended!=(UBaseType_t)pdFALSE){/* The scheduler is currently suspended - do not allow a context
* switch. */xYieldPending=pdTRUE;}else{xYieldPending=pdFALSE;traceTASK_SWITCHED_OUT();#if ( configGENERATE_RUN_TIME_STATS == 1 ){#ifdef portALT_GET_RUN_TIME_COUNTER_VALUEportALT_GET_RUN_TIME_COUNTER_VALUE(ulTotalRunTime);#elseulTotalRunTime=portGET_RUN_TIME_COUNTER_VALUE();#endif/* Add the amount of time the task has been running to the
* accumulated time so far. The time the task started running was
* stored in ulTaskSwitchedInTime. Note that there is no overflow
* protection here so count values are only valid until the timer
* overflows. The guard against negative values is to protect
* against suspect run time stat counter implementations - which
* are provided by the application, not the kernel. */if(ulTotalRunTime>ulTaskSwitchedInTime){pxCurrentTCB->ulRunTimeCounter+=(ulTotalRunTime-ulTaskSwitchedInTime);}else{mtCOVERAGE_TEST_MARKER();}ulTaskSwitchedInTime=ulTotalRunTime;}#endif/* configGENERATE_RUN_TIME_STATS *//* Check for stack overflow, if configured. */taskCHECK_FOR_STACK_OVERFLOW();/* Before the currently running task is switched out, save its errno. */#if ( configUSE_POSIX_ERRNO == 1 ){pxCurrentTCB->iTaskErrno=FreeRTOS_errno;}#endif/* Select a new task to run using either the generic C or port
* optimised asm code. */taskSELECT_HIGHEST_PRIORITY_TASK();/*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */traceTASK_SWITCHED_IN();/* After the new task is switched in, update the global errno. */#if ( configUSE_POSIX_ERRNO == 1 ){FreeRTOS_errno=pxCurrentTCB->iTaskErrno;}#endif#if ( configUSE_NEWLIB_REENTRANT == 1 ){/* Switch Newlib's _impure_ptr variable to point to the _reent
* structure specific to this task.
* See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
* for additional information. */_impure_ptr=&(pxCurrentTCB->xNewLib_reent);}#endif/* configUSE_NEWLIB_REENTRANT */}}/*-----------------------------------------------------------*/
/*
* Task control block. A task control block (TCB) is allocated for each task,
* and stores task state information, including a pointer to the task's context
* (the task's run time environment, including register values)
*/typedefstructtskTaskControlBlock/* Theoldnamingconventionisusedtopreventbreakingkernelawaredebuggers. */{volatileStackType_t*pxTopOfStack;/*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */#if ( portUSING_MPU_WRAPPERS == 1 )xMPU_SETTINGSxMPUSettings;/*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */#endifListItem_txStateListItem;/*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */ListItem_txEventListItem;/*< Used to reference a task from an event list. */UBaseType_tuxPriority;/*< The priority of the task. 0 is the lowest priority. */StackType_t*pxStack;/*< Points to the start of the stack. */charpcTaskName[configMAX_TASK_NAME_LEN];/*< Descriptive name given to the task when created. Facilitates debugging only. *//*lint !e971 Unqualified char types are allowed for strings and single characters only. */#if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )StackType_t*pxEndOfStack;/*< Points to the highest valid address for the stack. */#endif#if ( portCRITICAL_NESTING_IN_TCB == 1 )UBaseType_tuxCriticalNesting;/*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */#endif#if ( configUSE_TRACE_FACILITY == 1 )UBaseType_tuxTCBNumber;/*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */UBaseType_tuxTaskNumber;/*< Stores a number specifically for use by third party trace code. */#endif#if ( configUSE_MUTEXES == 1 )UBaseType_tuxBasePriority;/*< The priority last assigned to the task - used by the priority inheritance mechanism. */UBaseType_tuxMutexesHeld;#endif#if ( configUSE_APPLICATION_TASK_TAG == 1 )TaskHookFunction_tpxTaskTag;#endif#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )void*pvThreadLocalStoragePointers[configNUM_THREAD_LOCAL_STORAGE_POINTERS];#endif#if ( configGENERATE_RUN_TIME_STATS == 1 )uint32_tulRunTimeCounter;/*< Stores the amount of time the task has spent in the Running state. */#endif#if ( configUSE_NEWLIB_REENTRANT == 1 )/* Allocate a Newlib reent structure that is specific to this task.
* Note Newlib support has been included by popular demand, but is not
* used by the FreeRTOS maintainers themselves. FreeRTOS is not
* responsible for resulting newlib operation. User must be familiar with
* newlib and must provide system-wide implementations of the necessary
* stubs. Be warned that (at the time of writing) the current newlib design
* implements a system-wide malloc() that must be provided with locks.
*
* See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
* for additional information. */struct_reentxNewLib_reent;#endif#if ( configUSE_TASK_NOTIFICATIONS == 1 )volatileuint32_tulNotifiedValue[configTASK_NOTIFICATION_ARRAY_ENTRIES];volatileuint8_tucNotifyState[configTASK_NOTIFICATION_ARRAY_ENTRIES];#endif/* See the comments in FreeRTOS.h with the definition of
* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */#if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */uint8_tucStaticallyAllocated;/*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */#endif#if ( INCLUDE_xTaskAbortDelay == 1 )uint8_tucDelayAborted;#endif#if ( configUSE_POSIX_ERRNO == 1 )intiTaskErrno;#endif}tskTCB;