scheduler_up.c 16 KB

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