• Rt-Thread 5-调度


    目录

    1、rt_system_scheduler_init()函数

    2、rt_system_scheduler_start()函数 

    3、rt_schedule_insert_thread()函数

    4、rt_schedule_remove_thread()函数

    5、rt_schedule()函数

    6、_rt_scheduler_stack_check()函数

    7、rt_enter_critical()函数

    8、rt_exit_critical()函数

    9、rt_critical_level()函数

    10、rt_scheduler_sethook()函数 


    1、rt_system_scheduler_init()函数

    此函数用于初始化系统调度器。

    1)初始化线程优先级链表数组

    2)初始化当前线程优先级,当前线程控制块,线程就绪优先级组

    3)当最大线程数大于32时,初始化线程就绪表

    4)初始化线程僵尸链表

    1. void rt_system_scheduler_init(void)
    2. {
    3. register rt_base_t offset;
    4. rt_scheduler_lock_nest = 0;
    5. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
    6. RT_THREAD_PRIORITY_MAX));
    7. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
    8. {
    9. rt_list_init(&rt_thread_priority_table[offset]); //初始化线程优先级链表数组
    10. }
    11. rt_current_priority = RT_THREAD_PRIORITY_MAX - 1; //初始化当前线程优先级
    12. rt_current_thread = RT_NULL; //初始化当前线程控制块
    13. rt_thread_ready_priority_group = 0; //初始化线程就绪优先级组
    14. #if RT_THREAD_PRIORITY_MAX > 32 //最大线程数大于32
    15. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); //初始化线程就绪表
    16. #endif
    17. rt_list_init(&rt_thread_defunct); //初始化线程僵尸链表
    18. }

    2、rt_system_scheduler_start()函数 

    此函数用于启动调度器。

    1)计算最高就绪优先级

    2)获取要切换的线程

    3)切换到新的线程运行

    1. void rt_system_scheduler_start(void)
    2. {
    3. register struct rt_thread *to_thread;
    4. register rt_ubase_t highest_ready_priority;
    5. /* 计算最高就绪优先级 */
    6. #if RT_THREAD_PRIORITY_MAX > 32
    7. register rt_ubase_t number;
    8. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
    9. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
    10. #else
    11. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
    12. #endif
    13. /* 获取要切换的线程 */
    14. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
    15. struct rt_thread,
    16. tlist);
    17. rt_current_thread = to_thread;
    18. /* 切换到新线程 */
    19. rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
    20. /* never come back */
    21. }

    3、rt_schedule_insert_thread()函数

    此函数用于将线程加入到调度器。

    1)改变线程状态为RT_THREAD_READY

    2)将线程插入到就绪链链表

    3)设置优先级就绪相关变量(rt_thread_ready_table和rt_thread_ready_priority_group )

    1. void rt_schedule_insert_thread(struct rt_thread *thread)
    2. {
    3. register rt_base_t temp;
    4. RT_ASSERT(thread != RT_NULL);
    5. temp = rt_hw_interrupt_disable(); //关中断
    6. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK); //改变线程状态为RT_THREAD_READY
    7. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
    8. &(thread->tlist)); //将线程插入到就绪链链表
    9. /* 打印信息 */
    10. #if RT_THREAD_PRIORITY_MAX <= 32
    11. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
    12. RT_NAME_MAX, thread->name, thread->current_priority));
    13. #else
    14. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
    15. ("insert thread[%.*s], the priority: %d 0x%x %d\n",
    16. RT_NAME_MAX,
    17. thread->name,
    18. thread->number,
    19. thread->number_mask,
    20. thread->high_mask));
    21. #endif
    22. /* 设置优先级就绪相关变量 */
    23. #if RT_THREAD_PRIORITY_MAX > 32
    24. rt_thread_ready_table[thread->number] |= thread->high_mask;
    25. #endif
    26. rt_thread_ready_priority_group |= thread->number_mask;
    27. rt_hw_interrupt_enable(temp); //开中断
    28. }

    4、rt_schedule_remove_thread()函数

    此函数用于从调度器移除线程。

    1)从就绪链表移除线程节点

    2)如果当前优先级没有其他就绪的线程,则取消该优先级的就绪

    1. void rt_schedule_remove_thread(struct rt_thread *thread)
    2. {
    3. register rt_base_t temp;
    4. RT_ASSERT(thread != RT_NULL);
    5. temp = rt_hw_interrupt_disable(); //关中断
    6. /* 打印信息 */
    7. #if RT_THREAD_PRIORITY_MAX <= 32
    8. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
    9. RT_NAME_MAX, thread->name,
    10. thread->current_priority));
    11. #else
    12. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
    13. ("remove thread[%.*s], the priority: %d 0x%x %d\n",
    14. RT_NAME_MAX,
    15. thread->name,
    16. thread->number,
    17. thread->number_mask,
    18. thread->high_mask));
    19. #endif
    20. rt_list_remove(&(thread->tlist)); //从就绪链表移除线程节点
    21. /* 如果当前优先级没有其他就绪的线程,则取消该优先级的就绪 */
    22. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
    23. {
    24. #if RT_THREAD_PRIORITY_MAX > 32
    25. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
    26. if (rt_thread_ready_table[thread->number] == 0)
    27. {
    28. rt_thread_ready_priority_group &= ~thread->number_mask;
    29. }
    30. #else
    31. rt_thread_ready_priority_group &= ~thread->number_mask;
    32. #endif
    33. }
    34. rt_hw_interrupt_enable(temp); //开中断
    35. }

    5、rt_schedule()函数

    此函数用于进行一次调度。

    1)检查是否可以调度

    2)计算最高就绪优先级

    3)获取要切换的线程

    4)调用钩子回调函数

    5)如果使能栈检查,调用_rt_scheduler_stack_check()函数

    6)进行上下文切换

    1. void rt_schedule(void)
    2. {
    3. rt_base_t level;
    4. struct rt_thread *to_thread;
    5. struct rt_thread *from_thread;
    6. level = rt_hw_interrupt_disable(); //关中断
    7. if (rt_scheduler_lock_nest == 0) //检查是否可以调度
    8. {
    9. register rt_ubase_t highest_ready_priority;
    10. /* 计算最高就绪优先级 */
    11. #if RT_THREAD_PRIORITY_MAX <= 32
    12. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
    13. #else
    14. register rt_ubase_t number;
    15. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
    16. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
    17. #endif
    18. /* 获取要切换的线程 */
    19. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
    20. struct rt_thread,
    21. tlist);
    22. if (to_thread != rt_current_thread) //目标线程不等于当前线程
    23. {
    24. /* 设置当前优先级,当前线程控制块 */
    25. rt_current_priority = (rt_uint8_t)highest_ready_priority;
    26. from_thread = rt_current_thread;
    27. rt_current_thread = to_thread;
    28. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); //调用回调函数
    29. /* 打印信息 */
    30. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
    31. ("[%d]switch to priority#%d "
    32. "thread:%.*s(sp:0x%p), "
    33. "from thread:%.*s(sp: 0x%p)\n",
    34. rt_interrupt_nest, highest_ready_priority,
    35. RT_NAME_MAX, to_thread->name, to_thread->sp,
    36. RT_NAME_MAX, from_thread->name, from_thread->sp));
    37. #ifdef RT_USING_OVERFLOW_CHECK
    38. _rt_scheduler_stack_check(to_thread);
    39. #endif
    40. /* 进行上下文切换 */
    41. if (rt_interrupt_nest == 0)
    42. {
    43. rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
    44. (rt_ubase_t)&to_thread->sp);
    45. rt_hw_interrupt_enable(level); //开中断
    46. return ;
    47. }
    48. else
    49. {
    50. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
    51. rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
    52. (rt_ubase_t)&to_thread->sp);
    53. }
    54. }
    55. }
    56. rt_hw_interrupt_enable(level); //开中断
    57. }

    6、_rt_scheduler_stack_check()函数

    此函数用于检查栈的情况。

    1. static void _rt_scheduler_stack_check(struct rt_thread *thread)
    2. {
    3. RT_ASSERT(thread != RT_NULL);
    4. #if defined(ARCH_CPU_STACK_GROWS_UPWARD)
    5. if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
    6. #else
    7. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
    8. #endif
    9. (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
    10. (rt_ubase_t)thread->sp >
    11. (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
    12. {
    13. rt_ubase_t level;
    14. rt_kprintf("thread:%s stack overflow\n", thread->name);
    15. level = rt_hw_interrupt_disable();
    16. while (level);
    17. }
    18. #if defined(ARCH_CPU_STACK_GROWS_UPWARD)
    19. else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
    20. {
    21. rt_kprintf("warning: %s stack is close to the top of stack address.\n",
    22. thread->name);
    23. }
    24. #else
    25. else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
    26. {
    27. rt_kprintf("warning: %s stack is close to end of stack address.\n",
    28. thread->name);
    29. }
    30. #endif
    31. }

    7、rt_enter_critical()函数

    当需要进入临界区时,调用此函数。

    1. void rt_enter_critical(void)
    2. {
    3. register rt_base_t level;
    4. level = rt_hw_interrupt_disable(); //关中断
    5. rt_scheduler_lock_nest ++; //调度锁加1
    6. rt_hw_interrupt_enable(level); //开中断
    7. }

    8、rt_exit_critical()函数

    当需要退出临界区时,调用此函数。

    1. void rt_exit_critical(void)
    2. {
    3. register rt_base_t level;
    4. level = rt_hw_interrupt_disable(); //关中断
    5. rt_scheduler_lock_nest --;
    6. if (rt_scheduler_lock_nest <= 0)
    7. {
    8. rt_scheduler_lock_nest = 0;
    9. rt_hw_interrupt_enable(level); //开中断
    10. if (rt_current_thread)
    11. {
    12. rt_schedule(); //调度
    13. }
    14. }
    15. else
    16. {
    17. rt_hw_interrupt_enable(level); //开中断
    18. }
    19. }

    9、rt_critical_level()函数

    此函数用于返回rt_scheduler_lock_nest

    1. rt_uint16_t rt_critical_level(void)
    2. {
    3. return rt_scheduler_lock_nest;
    4. }

    10、rt_scheduler_sethook()函数 

    设置调度锁回调钩子函数。

    1. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
    2. {
    3. rt_scheduler_hook = hook;
    4. }

  • 相关阅读:
    渗透测试流程是什么?7个步骤给你讲清楚!
    WebSocket编程golang
    websocket接口测试
    对话腾讯天琴董治:聊聊元宇宙与AI技术驱动虚拟人
    安装 DolphinDB Python API
    利用Helm在K8S上部署 PolarDB-X 集群(详细步骤--亲测!!!)
    树莓派4b装系统到运行 Blazor Linux 本地程序全记录
    Codeforces Round 848 (Div. 2)C
    Unity小技巧——清空所有事件中订阅的方法
    Python 和 Ruby 谁是最好的Web开发语言?
  • 原文地址:https://blog.csdn.net/qq_37932504/article/details/126477511