rt-thread的调度函数

[复制链接]
3725|1
 楼主| keer_zu 发表于 2021-8-11 13:25 | 显示全部楼层 |阅读模式
   

  1. void rt_schedule(void)
  2. {
  3.     rt_base_t level;
  4.     struct rt_thread *to_thread;
  5.     struct rt_thread *from_thread;

  6.     /* disable interrupt */
  7.     level = rt_hw_interrupt_disable();

  8.     /* check the scheduler is enabled or not */
  9.     if (rt_scheduler_lock_nest == 0)
  10.     {
  11.         rt_ubase_t highest_ready_priority;

  12.         if (rt_thread_ready_priority_group != 0)
  13.         {
  14.             /* need_insert_from_thread: need to insert from_thread to ready queue */
  15.             int need_insert_from_thread = 0;

  16.             to_thread = _get_highest_priority_thread(&highest_ready_priority);

  17.             if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  18.             {
  19.                 if (rt_current_thread->current_priority < highest_ready_priority)
  20.                 {
  21.                     to_thread = rt_current_thread;
  22.                 }
  23.                 else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  24.                 {
  25.                     to_thread = rt_current_thread;
  26.                 }
  27.                 else
  28.                 {
  29.                     need_insert_from_thread = 1;
  30.                 }
  31.                 rt_current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  32.             }

  33.             if (to_thread != rt_current_thread)
  34.             {
  35.                 /* if the destination thread is not the same as current thread */
  36.                 rt_current_priority = (rt_uint8_t)highest_ready_priority;
  37.                 from_thread         = rt_current_thread;
  38.                 rt_current_thread   = to_thread;

  39.                 RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));

  40.                 if (need_insert_from_thread)
  41.                 {
  42.                     rt_schedule_insert_thread(from_thread);
  43.                 }

  44.                 rt_schedule_remove_thread(to_thread);
  45.                 to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);

  46.                 /* switch to new thread */
  47.                 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  48.                         ("[%d]switch to priority#%d "
  49.                          "thread:%.*s(sp:0x%08x), "
  50.                          "from thread:%.*s(sp: 0x%08x)\n",
  51.                          rt_interrupt_nest, highest_ready_priority,
  52.                          RT_NAME_MAX, to_thread->name, to_thread->sp,
  53.                          RT_NAME_MAX, from_thread->name, from_thread->sp));

  54. #ifdef RT_USING_OVERFLOW_CHECK
  55.                 _rt_scheduler_stack_check(to_thread);
  56. #endif

  57.                 if (rt_interrupt_nest == 0)
  58.                 {
  59.                     extern void rt_thread_handle_sig(rt_bool_t clean_state);

  60.                     rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  61.                             (rt_ubase_t)&to_thread->sp);

  62.                     /* enable interrupt */
  63.                     rt_hw_interrupt_enable(level);

  64. #ifdef RT_USING_SIGNALS
  65.                     /* check stat of thread for signal */
  66.                     level = rt_hw_interrupt_disable();
  67.                     if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  68.                     {
  69.                         extern void rt_thread_handle_sig(rt_bool_t clean_state);

  70.                         rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;

  71.                         rt_hw_interrupt_enable(level);

  72.                         /* check signal status */
  73.                         rt_thread_handle_sig(RT_TRUE);
  74.                     }
  75.                     else
  76.                     {
  77.                         rt_hw_interrupt_enable(level);
  78.                     }
  79. #endif
  80.                     goto __exit;
  81.                 }
  82.                 else
  83.                 {
  84.                     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));

  85.                     rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  86.                             (rt_ubase_t)&to_thread->sp);
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 rt_schedule_remove_thread(rt_current_thread);
  92.                 rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
  93.             }
  94.         }
  95.     }

  96.     /* enable interrupt */
  97.     rt_hw_interrupt_enable(level);

  98. __exit:
  99.     return;
  100. }




评论

2014-03-24  发表于 2021-8-11 16:19
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12949

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部