Embedded FreeRTOS learning eleven, in-depth understanding of task scheduling mechanism

1. Task scheduling mechanism

  • Preemptible: High priority tasks run first
  • Time slice rotation: tasks with the same priority are executed in turn
  • Idle task courtesy: If there are other ready tasks with the same priority as 0, the idle task will voluntarily give up a chance to run
  • The function call vTaskDelay( xDelay5ms ) can actively give up the task execution and give up the CPU usage right
  • The lowest priority interrupt in the system is bigger than the highest priority task
int main( void )
{
	prvSetupHardware();
	
	xTaskCreate(vTask1, "Task 1", 1000, NULL, 0, NULL);
	xTaskCreate(vTask2, "Task 2", 1000, NULL, 0, NULL);
	xTaskCreate(vTask3, "Task 3", 1000, NULL, 2, NULL);

	/* 启动调度器 */
	vTaskStartScheduler();

	/* 如果程序运行到了这里就表示出错了, 一般是内存不足 */
	return 0;
}

=========================================================================

2. Idle task function portTASK_FUNCTION( prvIdleTask, pvParameters )

static portTASK_FUNCTION( prvIdleTask, pvParameters )
{    
    ( void ) pvParameters;   
    portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
    for( ; ; )
    {      
        prvCheckTasksWaitingTermination();
        #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
            {               
                if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
                {
                    taskYIELD();
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }
            }
        #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
        #if ( configUSE_IDLE_HOOK == 1 )
            {
                extern void vApplicationIdleHook( void );               
                vApplicationIdleHook();
            }
        #endif /* configUSE_IDLE_HOOK */
        #endif /* configUSE_TICKLESS_IDLE */
    }
}

In the FreeRTOS operating system, the configuration item configUSE_PREEMPTION == 1 indicates that the same priority can be preempted, and the configuration item configIDLE_SHOULD_YIELD == 1 indicates active courtesy. Only when the task can be preempted, idle politeness is considered. 

If the configuration option is configured as configUSE_PREEMPTION == 0 without preemption, then polite taskYIELD() will be performed directly, and any other situation will be directly polite. When it is preemptible, you can configure whether to be courteous, and there is a case of politeness in non-preemption.

In the idle task portTASK_FUNCTION , a judgment will be made on the tasks in the task ready linked list. If the length of the current task ready linked list listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) is greater than 1, it means that there are other idle tasks in the linked list. A task with a priority of 0. Then the idle task will immediately make the task courtesy. Among them , tskIDLE_PRIORITY =0, that is, pxReadyTasksLists[ 0 ].

 After giving way to other tasks, the idle task function executes  taskYIELD()  to let other tasks run first. After the function is executed to taskYIELD() , the task scheduling function is started immediately. The task scheduling function executes task Task1, task Task2, task Task3, etc. After these three tasks are finished running, go back and execute the idle task function, and continue to execute from the last polite position.

=========================================================================

In the case of preemption or courtesy, the order of task execution is idle_task. After a while, task 1 will be executed immediately. Whether task 2 and task 3 will be executed depends on whether the tasks will be executed in turn.

It can be seen from the xTaskIncrementTick function that only when configUSE_PREEMPTION == 1 can be preempted and configUSE_TIME_SLICING == 1 time slice rotation can the task Task2 be executed. Without preemption or rotation, task Task2 will never be run.

おすすめ

転載: blog.csdn.net/weixin_44651073/article/details/127947891