scheduler_up.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-17 Bernard the first version
  9. * 2006-04-28 Bernard fix the scheduler algorthm
  10. * 2006-04-30 Bernard add SCHEDULER_DEBUG
  11. * 2006-05-27 Bernard fix the scheduler algorthm for same priority
  12. * thread schedule
  13. * 2006-06-04 Bernard rewrite the scheduler algorithm
  14. * 2006-08-03 Bernard add hook support
  15. * 2006-09-05 Bernard add 32 priority level support
  16. * 2006-09-24 Bernard add rt_system_scheduler_start function
  17. * 2009-09-16 Bernard fix _rt_scheduler_stack_check
  18. * 2010-04-11 yi.qiu add module feature
  19. * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest
  20. * issue found by kuronca
  21. * 2010-12-13 Bernard add defunct list initialization even if not use heap.
  22. * 2011-05-10 Bernard clean scheduler debug log.
  23. * 2013-12-21 Grissiom add rt_critical_level
  24. * 2018-11-22 Jesven remove the current task from ready queue
  25. * add per cpu ready queue
  26. * add _scheduler_get_highest_priority_thread to find highest priority task
  27. * rt_schedule_insert_thread won't insert current task to ready queue
  28. * in smp version, rt_hw_context_switch_interrupt maybe switch to
  29. * new task directly
  30. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to scheduler.c
  31. * 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c
  32. * 2023-10-17 ChuShicheng Modify the timing of clearing RT_THREAD_STAT_YIELD flag bits
  33. */
  34. #include <rtthread.h>
  35. #include <rthw.h>
  36. #define DBG_TAG "kernel.scheduler"
  37. #define DBG_LVL DBG_INFO
  38. #include <rtdbg.h>
  39. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  40. rt_uint32_t rt_thread_ready_priority_group;
  41. #if RT_THREAD_PRIORITY_MAX > 32
  42. /* Maximum priority level, 256 */
  43. rt_uint8_t rt_thread_ready_table[32];
  44. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  45. extern volatile rt_uint8_t rt_interrupt_nest;
  46. static rt_int16_t rt_scheduler_lock_nest;
  47. struct rt_thread *rt_current_thread = RT_NULL;
  48. rt_uint8_t rt_current_priority;
  49. #ifndef __on_rt_scheduler_hook
  50. #define __on_rt_scheduler_hook(from, to) __ON_HOOK_ARGS(rt_scheduler_hook, (from, to))
  51. #endif
  52. #ifndef __on_rt_scheduler_switch_hook
  53. #define __on_rt_scheduler_switch_hook(tid) __ON_HOOK_ARGS(rt_scheduler_switch_hook, (tid))
  54. #endif
  55. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  56. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  57. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  58. /**
  59. * @addtogroup Hook
  60. */
  61. /**@{*/
  62. /**
  63. * @brief This function will set a hook function, which will be invoked when thread
  64. * switch happens.
  65. *
  66. * @param hook is the hook function.
  67. */
  68. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  69. {
  70. rt_scheduler_hook = hook;
  71. }
  72. /**
  73. * @brief This function will set a hook function, which will be invoked when context
  74. * switch happens.
  75. *
  76. * @param hook is the hook function.
  77. */
  78. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  79. {
  80. rt_scheduler_switch_hook = hook;
  81. }
  82. /**@}*/
  83. #endif /* RT_USING_HOOK */
  84. #ifdef RT_USING_OVERFLOW_CHECK
  85. static void _scheduler_stack_check(struct rt_thread *thread)
  86. {
  87. RT_ASSERT(thread != RT_NULL);
  88. #ifdef RT_USING_SMART
  89. #ifndef ARCH_MM_MMU
  90. struct rt_lwp *lwp = thread ? (struct rt_lwp *)thread->lwp : 0;
  91. /* if stack pointer locate in user data section skip stack check. */
  92. if (lwp && ((rt_uint32_t)thread->sp > (rt_uint32_t)lwp->data_entry &&
  93. (rt_uint32_t)thread->sp <= (rt_uint32_t)lwp->data_entry + (rt_uint32_t)lwp->data_size))
  94. {
  95. return;
  96. }
  97. #endif /* not defined ARCH_MM_MMU */
  98. #endif /* RT_USING_SMART */
  99. #ifndef RT_USING_HW_STACK_GUARD
  100. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  101. if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
  102. #else
  103. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
  104. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  105. (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
  106. (rt_ubase_t)thread->sp >
  107. (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
  108. {
  109. rt_base_t level;
  110. rt_kprintf("thread:%s stack overflow\n", thread->parent.name);
  111. level = rt_hw_interrupt_disable();
  112. while (level);
  113. }
  114. #endif
  115. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  116. #ifndef RT_USING_HW_STACK_GUARD
  117. else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
  118. #else
  119. if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
  120. #endif
  121. {
  122. rt_kprintf("warning: %s stack is close to the top of stack address.\n",
  123. thread->parent.name);
  124. }
  125. #else
  126. #ifndef RT_USING_HW_STACK_GUARD
  127. else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
  128. #else
  129. if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
  130. #endif
  131. {
  132. rt_kprintf("warning: %s stack is close to end of stack address.\n",
  133. thread->parent.name);
  134. }
  135. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  136. }
  137. #endif /* RT_USING_OVERFLOW_CHECK */
  138. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  139. {
  140. struct rt_thread *highest_priority_thread;
  141. rt_ubase_t highest_ready_priority;
  142. #if RT_THREAD_PRIORITY_MAX > 32
  143. rt_ubase_t number;
  144. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  145. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  146. #else
  147. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  148. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  149. /* get highest ready priority thread */
  150. highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  151. struct rt_thread,
  152. tlist);
  153. *highest_prio = highest_ready_priority;
  154. return highest_priority_thread;
  155. }
  156. /**
  157. * @brief This function will initialize the system scheduler.
  158. */
  159. void rt_system_scheduler_init(void)
  160. {
  161. rt_base_t offset;
  162. rt_scheduler_lock_nest = 0;
  163. LOG_D("start scheduler: max priority 0x%02x",
  164. RT_THREAD_PRIORITY_MAX);
  165. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  166. {
  167. rt_list_init(&rt_thread_priority_table[offset]);
  168. }
  169. /* initialize ready priority group */
  170. rt_thread_ready_priority_group = 0;
  171. #if RT_THREAD_PRIORITY_MAX > 32
  172. /* initialize ready table */
  173. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  174. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  175. }
  176. /**
  177. * @brief This function will startup the scheduler. It will select one thread
  178. * with the highest priority level, then switch to it.
  179. */
  180. void rt_system_scheduler_start(void)
  181. {
  182. struct rt_thread *to_thread;
  183. rt_ubase_t highest_ready_priority;
  184. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  185. rt_current_thread = to_thread;
  186. rt_schedule_remove_thread(to_thread);
  187. to_thread->stat = RT_THREAD_RUNNING;
  188. /* switch to new thread */
  189. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp);
  190. /* never come back */
  191. }
  192. /**
  193. * @addtogroup Thread
  194. * @cond
  195. */
  196. /**@{*/
  197. /**
  198. * @brief This function will perform scheduling once. It will select one thread
  199. * with the highest priority, and switch to it immediately.
  200. */
  201. void rt_schedule(void)
  202. {
  203. rt_base_t level;
  204. struct rt_thread *to_thread;
  205. struct rt_thread *from_thread;
  206. /* disable interrupt */
  207. level = rt_hw_interrupt_disable();
  208. /* check the scheduler is enabled or not */
  209. if (rt_scheduler_lock_nest == 0)
  210. {
  211. rt_ubase_t highest_ready_priority;
  212. if (rt_thread_ready_priority_group != 0)
  213. {
  214. /* need_insert_from_thread: need to insert from_thread to ready queue */
  215. int need_insert_from_thread = 0;
  216. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  217. if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  218. {
  219. if (rt_current_thread->current_priority < highest_ready_priority)
  220. {
  221. to_thread = rt_current_thread;
  222. }
  223. else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  224. {
  225. to_thread = rt_current_thread;
  226. }
  227. else
  228. {
  229. need_insert_from_thread = 1;
  230. }
  231. }
  232. if (to_thread != rt_current_thread)
  233. {
  234. /* if the destination thread is not the same as current thread */
  235. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  236. from_thread = rt_current_thread;
  237. rt_current_thread = to_thread;
  238. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  239. if (need_insert_from_thread)
  240. {
  241. rt_schedule_insert_thread(from_thread);
  242. }
  243. if ((from_thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  244. {
  245. from_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  246. }
  247. rt_schedule_remove_thread(to_thread);
  248. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  249. /* switch to new thread */
  250. LOG_D("[%d]switch to priority#%d "
  251. "thread:%.*s(sp:0x%08x), "
  252. "from thread:%.*s(sp: 0x%08x)",
  253. rt_interrupt_nest, highest_ready_priority,
  254. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  255. RT_NAME_MAX, from_thread->parent.name, from_thread->sp);
  256. #ifdef RT_USING_OVERFLOW_CHECK
  257. _scheduler_stack_check(to_thread);
  258. #endif /* RT_USING_OVERFLOW_CHECK */
  259. if (rt_interrupt_nest == 0)
  260. {
  261. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  262. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
  263. rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  264. (rt_ubase_t)&to_thread->sp);
  265. /* enable interrupt */
  266. rt_hw_interrupt_enable(level);
  267. #ifdef RT_USING_SIGNALS
  268. /* check stat of thread for signal */
  269. level = rt_hw_interrupt_disable();
  270. if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  271. {
  272. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  273. rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  274. rt_hw_interrupt_enable(level);
  275. /* check signal status */
  276. rt_thread_handle_sig(RT_TRUE);
  277. }
  278. else
  279. {
  280. rt_hw_interrupt_enable(level);
  281. }
  282. #endif /* RT_USING_SIGNALS */
  283. goto __exit;
  284. }
  285. else
  286. {
  287. LOG_D("switch in interrupt");
  288. rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  289. (rt_ubase_t)&to_thread->sp, from_thread, to_thread);
  290. }
  291. }
  292. else
  293. {
  294. rt_schedule_remove_thread(rt_current_thread);
  295. rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
  296. }
  297. }
  298. }
  299. /* enable interrupt */
  300. rt_hw_interrupt_enable(level);
  301. __exit:
  302. return;
  303. }
  304. /**
  305. * @brief This function will insert a thread to the system ready queue. The state of
  306. * thread will be set as READY and the thread will be removed from suspend queue.
  307. *
  308. * @param thread is the thread to be inserted.
  309. *
  310. * @note Please do not invoke this function in user application.
  311. */
  312. void rt_schedule_insert_thread(struct rt_thread *thread)
  313. {
  314. rt_base_t level;
  315. RT_ASSERT(thread != RT_NULL);
  316. /* disable interrupt */
  317. level = rt_hw_interrupt_disable();
  318. /* it's current thread, it should be RUNNING thread */
  319. if (thread == rt_current_thread)
  320. {
  321. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  322. goto __exit;
  323. }
  324. /* READY thread, insert to ready queue */
  325. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  326. /* there is no time slices left(YIELD), inserting thread before ready list*/
  327. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  328. {
  329. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  330. &(thread->tlist));
  331. }
  332. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  333. else
  334. {
  335. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  336. &(thread->tlist));
  337. }
  338. LOG_D("insert thread[%.*s], the priority: %d",
  339. RT_NAME_MAX, thread->parent.name, thread->current_priority);
  340. /* set priority mask */
  341. #if RT_THREAD_PRIORITY_MAX > 32
  342. rt_thread_ready_table[thread->number] |= thread->high_mask;
  343. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  344. rt_thread_ready_priority_group |= thread->number_mask;
  345. __exit:
  346. /* enable interrupt */
  347. rt_hw_interrupt_enable(level);
  348. }
  349. /**
  350. * @brief This function will remove a thread from system ready queue.
  351. *
  352. * @param thread is the thread to be removed.
  353. *
  354. * @note Please do not invoke this function in user application.
  355. */
  356. void rt_schedule_remove_thread(struct rt_thread *thread)
  357. {
  358. rt_base_t level;
  359. RT_ASSERT(thread != RT_NULL);
  360. /* disable interrupt */
  361. level = rt_hw_interrupt_disable();
  362. LOG_D("remove thread[%.*s], the priority: %d",
  363. RT_NAME_MAX, thread->parent.name,
  364. thread->current_priority);
  365. /* remove thread from ready list */
  366. rt_list_remove(&(thread->tlist));
  367. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  368. {
  369. #if RT_THREAD_PRIORITY_MAX > 32
  370. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  371. if (rt_thread_ready_table[thread->number] == 0)
  372. {
  373. rt_thread_ready_priority_group &= ~thread->number_mask;
  374. }
  375. #else
  376. rt_thread_ready_priority_group &= ~thread->number_mask;
  377. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  378. }
  379. /* enable interrupt */
  380. rt_hw_interrupt_enable(level);
  381. }
  382. /**
  383. * @brief This function will lock the thread scheduler.
  384. */
  385. void rt_enter_critical(void)
  386. {
  387. rt_base_t level;
  388. /* disable interrupt */
  389. level = rt_hw_interrupt_disable();
  390. /*
  391. * the maximal number of nest is RT_UINT16_MAX, which is big
  392. * enough and does not check here
  393. */
  394. rt_scheduler_lock_nest ++;
  395. /* enable interrupt */
  396. rt_hw_interrupt_enable(level);
  397. }
  398. RTM_EXPORT(rt_enter_critical);
  399. /**
  400. * @brief This function will unlock the thread scheduler.
  401. */
  402. void rt_exit_critical(void)
  403. {
  404. rt_base_t level;
  405. /* disable interrupt */
  406. level = rt_hw_interrupt_disable();
  407. rt_scheduler_lock_nest --;
  408. if (rt_scheduler_lock_nest <= 0)
  409. {
  410. rt_scheduler_lock_nest = 0;
  411. /* enable interrupt */
  412. rt_hw_interrupt_enable(level);
  413. if (rt_current_thread)
  414. {
  415. /* if scheduler is started, do a schedule */
  416. rt_schedule();
  417. }
  418. }
  419. else
  420. {
  421. /* enable interrupt */
  422. rt_hw_interrupt_enable(level);
  423. }
  424. }
  425. RTM_EXPORT(rt_exit_critical);
  426. /**
  427. * @brief Get the scheduler lock level.
  428. *
  429. * @return the level of the scheduler lock. 0 means unlocked.
  430. */
  431. rt_uint16_t rt_critical_level(void)
  432. {
  433. return rt_scheduler_lock_nest;
  434. }
  435. RTM_EXPORT(rt_critical_level);
  436. /**@}*/
  437. /**@endcond*/