1 前言
RT-Thread中提供的線程調度器是基於全搶佔式優先級的調度,在系統中除了中斷處理函數、調度器上鎖部分的代碼和禁止中斷的代碼是不可搶佔的之外,系統的其他部分都是可以搶佔的,包括線程調度器自身.系統總共支持256個優先級(0 ~ 255,數值越小的優先級越高,0爲最高優先級,255分配給空閒線程使用,一般用戶不使用。在一些資源比較緊張的系統中,可以根據情況選擇只支持8個或32個優先級的系統配置)。在系統中,當有比當前線程優先級還要高的線程就緒時,當前線程將立刻被換出,高優先級線程搶佔處理機進行執行。
2 線程優先級管理系統
rt-thread採用一個數組來實現線程優先級管理系統,如下圖所示,RT-Thread調度器實現中包含一組,總共256個優先級隊列數組(如果系統最大支持32個優先級,那麼這裏將是32個優先級隊列數組),每個優先級隊列採用雙向環形鏈表的方式鏈接,255優先級隊列中一般只包含一個idle線程。
其源碼定義如下:
- rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
2 調度器初始化
- /**
- * @ingroup SystemInit
- * This function will initialize the system scheduler
- */
- void rt_system_scheduler_init(void)
- {
- register rt_base_t offset;
- rt_scheduler_lock_nest = 0;//調度器嵌套鎖計數器設爲0
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
- RT_THREAD_PRIORITY_MAX));
- for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)//所有優先級對應的線程鏈表初始化
- {
- rt_list_init(&rt_thread_priority_table[offset]);
- }
- rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;//rt_current_prority爲全局變量,初始化
- rt_current_thread = RT_NULL;//全局變量rt_current_thread初始化爲空
- /* initialize ready priority group */
- rt_thread_ready_priority_group = 0;//全局變量rt_thread_ready_priority_group初始化爲0
- #if RT_THREAD_PRIORITY_MAX > 32
- /* initialize ready table */
- rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
- #endif
- /* initialize thread defunct */
- rt_list_init(&rt_thread_defunct);//初始化全局空閒線程處理的回調線程鏈表,rt_thread_defunct爲這線程鏈表,只在系統空閒時被空閒線程操作
- }
3 啓動線程調度器
- /**
- * @ingroup SystemInit
- * This function will startup scheduler. It will select one thread
- * with the highest priority level, then switch to it.
- */
- void rt_system_scheduler_start(void)
- {
- register struct rt_thread *to_thread;
- register rt_ubase_t highest_ready_priority;
- //以下代碼是查找出新高優先級的線程
- #if RT_THREAD_PRIORITY_MAX == 8
- highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group];
- #else
- register rt_ubase_t number;
- /* find out the highest priority task */
- if (rt_thread_ready_priority_group & 0xff)
- {
- number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff];
- }
- else if (rt_thread_ready_priority_group & 0xff00)
- {
- number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8;
- }
- else if (rt_thread_ready_priority_group & 0xff0000)
- {
- number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16;
- }
- else
- {
- number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24;
- }
- #if RT_THREAD_PRIORITY_MAX > 32
- highest_ready_priority = (number << 3) +
- rt_lowest_bitmap[rt_thread_ready_table[number]];
- #else
- highest_ready_priority = number;
- #endif
- #endif
- /* get switch to thread *///得到線程
- to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
- struct rt_thread,
- tlist);
- rt_current_thread = to_thread;//設置當前線程
- /* switch to new thread */
- rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);//此函數是與MCU相關的函數,實現切換到目的線程的功能
- /* never come back */
- }
4 往調度器添加線程
- /*
- * This function will insert a thread to system ready queue. The state of
- * thread will be set as READY and remove from suspend queue.
- *
- * @param thread the thread to be inserted
- * @note Please do not invoke this function in user application.
- */
- void rt_schedule_insert_thread(struct rt_thread *thread)
- {
- register rt_base_t temp;
- RT_ASSERT(thread != RT_NULL);
- /* disable interrupt */
- temp = rt_hw_interrupt_disable();//關中斷
- /* change stat */
- thread->stat = RT_THREAD_READY;
- /* insert thread to ready list */
- rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),//往當前優先級鏈表中添加線程節點
- &(thread->tlist));
- /* set priority mask */
- #if RT_THREAD_PRIORITY_MAX <= 32
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%s], the priority: %d\n",
- thread->name, thread->current_priority));
- #else
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
- ("insert thread[%s], the priority: %d 0x%x %d\n",
- thread->name,
- thread->number,
- thread->number_mask,
- thread->high_mask));
- #endif
- #if RT_THREAD_PRIORITY_MAX > 32
- rt_thread_ready_table[thread->number] |= thread->high_mask;
- #endif
- rt_thread_ready_priority_group |= thread->number_mask;
- /* enable interrupt */
- rt_hw_interrupt_enable(temp);//開中斷
- }
5 將線程從調度器中移除
- /*
- * This function will remove a thread from system ready queue.
- *
- * @param thread the thread to be removed
- *
- * @note Please do not invoke this function in user application.
- */
- void rt_schedule_remove_thread(struct rt_thread *thread)
- {
- register rt_base_t temp;
- RT_ASSERT(thread != RT_NULL);
- /* disable interrupt */
- temp = rt_hw_interrupt_disable();//關中斷
- #if RT_THREAD_PRIORITY_MAX <= 32
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%s], the priority: %d\n",
- thread->name, thread->current_priority));
- #else
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
- ("remove thread[%s], the priority: %d 0x%x %d\n",
- thread->name,
- thread->number,
- thread->number_mask,
- thread->high_mask));
- #endif
- /* remove thread from ready list */
- rt_list_remove(&(thread->tlist));//從隊列中移除
- if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))//如果當前優先級鏈表已空
- {
- #if RT_THREAD_PRIORITY_MAX > 32
- rt_thread_ready_table[thread->number] &= ~thread->high_mask;
- if (rt_thread_ready_table[thread->number] == 0)
- {
- rt_thread_ready_priority_group &= ~thread->number_mask;
- }
- #else
- rt_thread_ready_priority_group &= ~thread->number_mask;
- #endif
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(temp);//開中斷
- }
6 線程調度
- /**
- * @addtogroup Thread
- */
- /*@{*/
- /**
- * This function will perform one schedule. It will select one thread
- * with the highest priority level, then switch to it.
- */
- void rt_schedule(void)
- {
- rt_base_t level;
- struct rt_thread *to_thread;
- struct rt_thread *from_thread;
- /* disable interrupt */
- level = rt_hw_interrupt_disable();//關中斷
- /* check the scheduler is enabled or not */
- if (rt_scheduler_lock_nest == 0)//當前不處於線程嵌套中
- {
- register rt_ubase_t highest_ready_priority;
- //以下代碼是獲取當前最高優先級的線程的優先級
- #if RT_THREAD_PRIORITY_MAX == 8
- highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group];
- #else
- register rt_ubase_t number;
- /* find out the highest priority task */
- if (rt_thread_ready_priority_group & 0xff)
- {
- number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff];
- }
- else if (rt_thread_ready_priority_group & 0xff00)
- {
- number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8;
- }
- else if (rt_thread_ready_priority_group & 0xff0000)
- {
- number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16;
- }
- else
- {
- number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24;
- }
- #if RT_THREAD_PRIORITY_MAX > 32
- highest_ready_priority = (number << 3) +
- rt_lowest_bitmap[rt_thread_ready_table[number]];
- #else
- highest_ready_priority = number;
- #endif
- #endif
- /* get switch to thread *///得到最高優先級線程
- to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
- struct rt_thread,
- tlist);
- /* if the destination thread is not the same as current thread */
- if (to_thread != rt_current_thread)//需要線程切換
- {
- rt_current_priority = (rt_uint8_t)highest_ready_priority;//更新一些變量
- from_thread = rt_current_thread;
- rt_current_thread = to_thread;
- RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
- /* switch to new thread */
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
- ("[%d]switch to priority#%d thread:%s\n",
- rt_interrupt_nest,
- highest_ready_priority,
- to_thread->name));
- #ifdef RT_USING_OVERFLOW_CHECK
- _rt_scheduler_stack_check(to_thread);//線程棧溢出檢查
- #endif
- if (rt_interrupt_nest == 0)//如果當前沒有處於中斷嵌套中
- {
- rt_hw_context_switch((rt_uint32_t)&from_thread->sp,//線程切換,此函數爲MCU相關函數,與具體使用的MCU相關,這裏不作介紹
- (rt_uint32_t)&to_thread->sp);
- }
- else//如果當前處於中斷例程中
- {
- RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
- rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,//線程切換,與rt_hw_context_switch類似
- (rt_uint32_t)&to_thread->sp);
- }
- }
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(level);//開中斷
- }
7 進入臨界區
此函數實現上實現的是禁止調度,也就是說,執行了此函數,調度器將不再調度線程,這個從第6章的語句if (rt_scheduler_lock_nest == 0) ...可以看出來。
- /**
- * This function will lock the thread scheduler.
- */
- void rt_enter_critical(void)
- {
- register rt_base_t level;
- /* disable interrupt */
- level = rt_hw_interrupt_disable();//關中斷
- /*
- * the maximal number of nest is RT_UINT16_MAX, which is big
- * enough and does not check here
- */
- rt_scheduler_lock_nest ++;//調度鎖計數器加1
- /* enable interrupt */
- rt_hw_interrupt_enable(level);//開中斷
- }
8 退出臨界區
與進入臨界區對應,此函數實現的是讓之前禁止的調度器重新調度線程,其源碼如下所示:
- /**
- * This function will unlock the thread scheduler.
- */
- void rt_exit_critical(void)
- {
- register rt_base_t level;
- /* disable interrupt */
- level = rt_hw_interrupt_disable();//關中斷
- rt_scheduler_lock_nest --;//調度鎖嵌套計數器減1
- if (rt_scheduler_lock_nest <= 0)//如果調度鎖嵌套計數器小於或等於0,則置其爲0
- {
- rt_scheduler_lock_nest = 0;
- /* enable interrupt */
- rt_hw_interrupt_enable(level);//開中斷
- rt_schedule();//調度線程
- }
- else
- {
- /* enable interrupt */
- rt_hw_interrupt_enable(level);//開中斷
- }
- }
前面源碼中的調度器啓動rt_system_scheduler_start和調度rt_schedule的對應源碼中都有使用位圖來實現獲取當前最高優先級線程對應的優先緩的算法,就不在這裏做詳細介紹,下一章將專門來討論此算法。