rt-thread线程调度器源码分析

1 前言

RT-Thread中提供的线程调度器是基于全抢占式优先级的调度,在系统中除了中断处理函数、调度器上锁部分的代码和禁止中断的代码是不可抢占的之外,系统的其他部分都是可以抢占的,包括线程调度器自身.系统总共支持256个优先级(0 ~ 255,数值越小的优先级越高,0为最高优先级,255分配给空闲线程使用,一般用户不使用。在一些资源比较紧张的系统中,可以根据情况选择只支持8个或32个优先级的系统配置)。在系统中,当有比当前线程优先级还要高的线程就绪时,当前线程将立刻被换出,高优先级线程抢占处理机进行执行。

2 线程优先级管理系统

rt-thread采用一个数组来实现线程优先级管理系统,如下图所示,RT-Thread调度器实现中包含一组,总共256个优先级队列数组(如果系统最大支持32个优先级,那么这里将是32个优先级队列数组),每个优先级队列采用双向环形链表的方式链接,255优先级队列中一般只包含一个idle线程。

其源码定义如下:

[cpp]  view plain  copy
  1. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];  

2 调度器初始化

[cpp]  view plain  copy
  1. /** 
  2.  * @ingroup SystemInit 
  3.  * This function will initialize the system scheduler 
  4.  */  
  5. void rt_system_scheduler_init(void)  
  6. {  
  7.     register rt_base_t offset;  
  8.   
  9.     rt_scheduler_lock_nest = 0;//调度器嵌套锁计数器设为0  
  10.   
  11.     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",  
  12.                                       RT_THREAD_PRIORITY_MAX));  
  13.   
  14.     for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)//所有优先级对应的线程链表初始化  
  15.     {  
  16.         rt_list_init(&rt_thread_priority_table[offset]);  
  17.     }  
  18.   
  19.     rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;//rt_current_prority为全局变量,初始化  
  20.     rt_current_thread = RT_NULL;//全局变量rt_current_thread初始化为空  
  21.   
  22.     /* initialize ready priority group */  
  23.     rt_thread_ready_priority_group = 0;//全局变量rt_thread_ready_priority_group初始化为0  
  24.   
  25. #if RT_THREAD_PRIORITY_MAX > 32  
  26.     /* initialize ready table */  
  27.     rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));  
  28. #endif  
  29.   
  30.     /* initialize thread defunct */  
  31.     rt_list_init(&rt_thread_defunct);//初始化全局空闲线程处理的回调线程链表,rt_thread_defunct为这线程链表,只在系统空闲时被空闲线程操作  
  32. }  

3 启动线程调度器

[cpp]  view plain  copy
  1. /** 
  2.  * @ingroup SystemInit 
  3.  * This function will startup scheduler. It will select one thread 
  4.  * with the highest priority level, then switch to it. 
  5.  */  
  6. void rt_system_scheduler_start(void)  
  7. {  
  8.     register struct rt_thread *to_thread;  
  9.     register rt_ubase_t highest_ready_priority;  
  10.     //以下代码是查找出新高优先级的线程  
  11. #if RT_THREAD_PRIORITY_MAX == 8  
  12.     highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group];  
  13. #else  
  14.     register rt_ubase_t number;  
  15.     /* find out the highest priority task */  
  16.     if (rt_thread_ready_priority_group & 0xff)  
  17.     {  
  18.         number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff];  
  19.     }  
  20.     else if (rt_thread_ready_priority_group & 0xff00)  
  21.     {  
  22.         number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8;  
  23.     }  
  24.     else if (rt_thread_ready_priority_group & 0xff0000)  
  25.     {  
  26.         number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16;  
  27.     }  
  28.     else  
  29.     {  
  30.         number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24;  
  31.     }  
  32.   
  33. #if RT_THREAD_PRIORITY_MAX > 32  
  34.     highest_ready_priority = (number << 3) +  
  35.                              rt_lowest_bitmap[rt_thread_ready_table[number]];  
  36. #else  
  37.     highest_ready_priority = number;  
  38. #endif  
  39. #endif  
  40.   
  41.     /* get switch to thread *///得到线程  
  42.     to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,  
  43.                               struct rt_thread,  
  44.                               tlist);  
  45.   
  46.     rt_current_thread = to_thread;//设置当前线程  
  47.   
  48.     /* switch to new thread */  
  49.     rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);//此函数是与MCU相关的函数,实现切换到目的线程的功能  
  50.   
  51.     /* never come back */  
  52. }  


4 往调度器添加线程

[cpp]  view plain  copy
  1. /* 
  2.  * This function will insert a thread to system ready queue. The state of 
  3.  * thread will be set as READY and remove from suspend queue. 
  4.  * 
  5.  * @param thread the thread to be inserted 
  6.  * @note Please do not invoke this function in user application. 
  7.  */  
  8. void rt_schedule_insert_thread(struct rt_thread *thread)  
  9. {  
  10.     register rt_base_t temp;  
  11.   
  12.     RT_ASSERT(thread != RT_NULL);  
  13.   
  14.     /* disable interrupt */  
  15.     temp = rt_hw_interrupt_disable();//关中断  
  16.   
  17.     /* change stat */  
  18.     thread->stat = RT_THREAD_READY;  
  19.   
  20.     /* insert thread to ready list */  
  21.     rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),//往当前优先级链表中添加线程节点  
  22.                           &(thread->tlist));  
  23.   
  24.     /* set priority mask */  
  25. #if RT_THREAD_PRIORITY_MAX <= 32  
  26.     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%s], the priority: %d\n",   
  27.                                       thread->name, thread->current_priority));  
  28. #else  
  29.     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,  
  30.                  ("insert thread[%s], the priority: %d 0x%x %d\n",   
  31.                   thread->name,  
  32.                   thread->number,  
  33.                   thread->number_mask,  
  34.                   thread->high_mask));  
  35. #endif  
  36.   
  37. #if RT_THREAD_PRIORITY_MAX > 32  
  38.     rt_thread_ready_table[thread->number] |= thread->high_mask;  
  39. #endif  
  40.     rt_thread_ready_priority_group |= thread->number_mask;  
  41.   
  42.     /* enable interrupt */  
  43.     rt_hw_interrupt_enable(temp);//开中断  
  44. }  

5 将线程从调度器中移除

[cpp]  view plain  copy
  1. /* 
  2.  * This function will remove a thread from system ready queue. 
  3.  * 
  4.  * @param thread the thread to be removed 
  5.  * 
  6.  * @note Please do not invoke this function in user application. 
  7.  */  
  8. void rt_schedule_remove_thread(struct rt_thread *thread)  
  9. {  
  10.     register rt_base_t temp;  
  11.   
  12.     RT_ASSERT(thread != RT_NULL);  
  13.   
  14.     /* disable interrupt */  
  15.     temp = rt_hw_interrupt_disable();//关中断  
  16.   
  17. #if RT_THREAD_PRIORITY_MAX <= 32  
  18.     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%s], the priority: %d\n",   
  19.                                       thread->name, thread->current_priority));  
  20. #else  
  21.     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,  
  22.                  ("remove thread[%s], the priority: %d 0x%x %d\n",   
  23.                   thread->name,  
  24.                   thread->number,  
  25.                   thread->number_mask,  
  26.                   thread->high_mask));  
  27. #endif  
  28.   
  29.     /* remove thread from ready list */  
  30.     rt_list_remove(&(thread->tlist));//从队列中移除  
  31.     if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))//如果当前优先级链表已空  
  32.     {  
  33. #if RT_THREAD_PRIORITY_MAX > 32  
  34.         rt_thread_ready_table[thread->number] &= ~thread->high_mask;  
  35.         if (rt_thread_ready_table[thread->number] == 0)  
  36.         {  
  37.             rt_thread_ready_priority_group &= ~thread->number_mask;  
  38.         }  
  39. #else  
  40.         rt_thread_ready_priority_group &= ~thread->number_mask;  
  41. #endif  
  42.     }  
  43.   
  44.     /* enable interrupt */  
  45.     rt_hw_interrupt_enable(temp);//开中断  
  46. }  

6 线程调度

[cpp]  view plain  copy
  1. /** 
  2.  * @addtogroup Thread 
  3.  */  
  4.   
  5. /*@{*/  
  6.   
  7. /** 
  8.  * This function will perform one schedule. It will select one thread 
  9.  * with the highest priority level, then switch to it. 
  10.  */  
  11. void rt_schedule(void)  
  12. {  
  13.     rt_base_t level;  
  14.     struct rt_thread *to_thread;  
  15.     struct rt_thread *from_thread;  
  16.   
  17.     /* disable interrupt */  
  18.     level = rt_hw_interrupt_disable();//关中断  
  19.   
  20.     /* check the scheduler is enabled or not */  
  21.     if (rt_scheduler_lock_nest == 0)//当前不处于线程嵌套中  
  22.     {  
  23.         register rt_ubase_t highest_ready_priority;  
  24.         //以下代码是获取当前最高优先级的线程的优先级  
  25. #if RT_THREAD_PRIORITY_MAX == 8  
  26.         highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group];  
  27. #else  
  28.         register rt_ubase_t number;  
  29.         /* find out the highest priority task */  
  30.         if (rt_thread_ready_priority_group & 0xff)  
  31.         {  
  32.             number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff];  
  33.         }  
  34.         else if (rt_thread_ready_priority_group & 0xff00)  
  35.         {  
  36.             number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8;  
  37.         }  
  38.         else if (rt_thread_ready_priority_group & 0xff0000)  
  39.         {  
  40.             number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16;  
  41.         }  
  42.         else  
  43.         {  
  44.             number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24;  
  45.         }  
  46.   
  47. #if RT_THREAD_PRIORITY_MAX > 32  
  48.         highest_ready_priority = (number << 3) +  
  49.                                  rt_lowest_bitmap[rt_thread_ready_table[number]];  
  50. #else  
  51.         highest_ready_priority = number;  
  52. #endif  
  53. #endif  
  54.         /* get switch to thread *///得到最高优先级线程  
  55.         to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,  
  56.                                   struct rt_thread,  
  57.                                   tlist);  
  58.   
  59.         /* if the destination thread is not the same as current thread */  
  60.         if (to_thread != rt_current_thread)//需要线程切换  
  61.         {  
  62.             rt_current_priority = (rt_uint8_t)highest_ready_priority;//更新一些变量  
  63.             from_thread         = rt_current_thread;  
  64.             rt_current_thread   = to_thread;  
  65.   
  66.             RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));  
  67.   
  68.             /* switch to new thread */  
  69.             RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,  
  70.                          ("[%d]switch to priority#%d thread:%s\n",  
  71.                           rt_interrupt_nest,  
  72.                           highest_ready_priority,  
  73.                           to_thread->name));  
  74.   
  75. #ifdef RT_USING_OVERFLOW_CHECK  
  76.             _rt_scheduler_stack_check(to_thread);//线程栈溢出检查  
  77. #endif  
  78.   
  79.             if (rt_interrupt_nest == 0)//如果当前没有处于中断嵌套中  
  80.             {  
  81.                 rt_hw_context_switch((rt_uint32_t)&from_thread->sp,//线程切换,此函数为MCU相关函数,与具体使用的MCU相关,这里不作介绍  
  82.                                      (rt_uint32_t)&to_thread->sp);  
  83.             }  
  84.             else//如果当前处于中断例程中  
  85.             {  
  86.                 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));  
  87.   
  88.                 rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,//线程切换,与rt_hw_context_switch类似  
  89.                                                (rt_uint32_t)&to_thread->sp);  
  90.             }  
  91.         }  
  92.     }  
  93.   
  94.     /* enable interrupt */  
  95.     rt_hw_interrupt_enable(level);//开中断  
  96. }  

7 进入临界区

此函数实现上实现的是禁止调度,也就是说,执行了此函数,调度器将不再调度线程,这个从第6章的语句if (rt_scheduler_lock_nest == 0) ...可以看出来。

[cpp]  view plain  copy
  1. /** 
  2.  * This function will lock the thread scheduler. 
  3.  */  
  4. void rt_enter_critical(void)  
  5. {  
  6.     register rt_base_t level;  
  7.   
  8.     /* disable interrupt */  
  9.     level = rt_hw_interrupt_disable();//关中断  
  10.   
  11.     /* 
  12.      * the maximal number of nest is RT_UINT16_MAX, which is big 
  13.      * enough and does not check here 
  14.      */  
  15.     rt_scheduler_lock_nest ++;//调度锁计数器加1  
  16.   
  17.     /* enable interrupt */  
  18.     rt_hw_interrupt_enable(level);//开中断  
  19. }  

8 退出临界区

与进入临界区对应,此函数实现的是让之前禁止的调度器重新调度线程,其源码如下所示:

[cpp]  view plain  copy
  1. /** 
  2.  * This function will unlock the thread scheduler. 
  3.  */  
  4. void rt_exit_critical(void)  
  5. {  
  6.     register rt_base_t level;  
  7.   
  8.     /* disable interrupt */  
  9.     level = rt_hw_interrupt_disable();//关中断  
  10.   
  11.     rt_scheduler_lock_nest --;//调度锁嵌套计数器减1  
  12.   
  13.     if (rt_scheduler_lock_nest <= 0)//如果调度锁嵌套计数器小于或等于0,则置其为0  
  14.     {  
  15.         rt_scheduler_lock_nest = 0;  
  16.         /* enable interrupt */  
  17.         rt_hw_interrupt_enable(level);//开中断  
  18.   
  19.         rt_schedule();//调度线程  
  20.     }  
  21.     else  
  22.     {  
  23.         /* enable interrupt */  
  24.         rt_hw_interrupt_enable(level);//开中断  
  25.     }  
  26. }  

前面源码中的调度器启动rt_system_scheduler_start和调度rt_schedule的对应源码中都有使用位图来实现获取当前最高优先级线程对应的优先缓的算法,就不在这里做详细介绍,下一章将专门来讨论此算法。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值