scheduler_up.c 16 KB

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