scheduler_up.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  50. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  51. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  52. /**
  53. * @addtogroup Hook
  54. */
  55. /**@{*/
  56. /**
  57. * @brief This function will set a hook function, which will be invoked when thread
  58. * switch happens.
  59. *
  60. * @param hook is the hook function.
  61. */
  62. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  63. {
  64. rt_scheduler_hook = hook;
  65. }
  66. /**
  67. * @brief This function will set a hook function, which will be invoked when context
  68. * switch happens.
  69. *
  70. * @param hook is the hook function.
  71. */
  72. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  73. {
  74. rt_scheduler_switch_hook = hook;
  75. }
  76. /**@}*/
  77. #endif /* RT_USING_HOOK */
  78. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  79. {
  80. struct rt_thread *highest_priority_thread;
  81. rt_ubase_t highest_ready_priority;
  82. #if RT_THREAD_PRIORITY_MAX > 32
  83. rt_ubase_t number;
  84. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  85. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  86. #else
  87. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  88. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  89. /* get highest ready priority thread */
  90. highest_priority_thread = RT_THREAD_LIST_NODE_ENTRY(rt_thread_priority_table[highest_ready_priority].next);
  91. *highest_prio = highest_ready_priority;
  92. return highest_priority_thread;
  93. }
  94. rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
  95. {
  96. rt_base_t level;
  97. if (!plvl)
  98. return -RT_EINVAL;
  99. level = rt_hw_interrupt_disable();
  100. *plvl = level;
  101. return RT_EOK;
  102. }
  103. rt_err_t rt_sched_unlock(rt_sched_lock_level_t level)
  104. {
  105. rt_hw_interrupt_enable(level);
  106. return RT_EOK;
  107. }
  108. rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
  109. {
  110. if (rt_thread_self())
  111. {
  112. /* if scheduler is available */
  113. rt_schedule();
  114. }
  115. rt_hw_interrupt_enable(level);
  116. return RT_EOK;
  117. }
  118. /**
  119. * @brief This function will initialize the system scheduler.
  120. */
  121. void rt_system_scheduler_init(void)
  122. {
  123. rt_base_t offset;
  124. rt_scheduler_lock_nest = 0;
  125. LOG_D("start scheduler: max priority 0x%02x",
  126. RT_THREAD_PRIORITY_MAX);
  127. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  128. {
  129. rt_list_init(&rt_thread_priority_table[offset]);
  130. }
  131. /* initialize ready priority group */
  132. rt_thread_ready_priority_group = 0;
  133. #if RT_THREAD_PRIORITY_MAX > 32
  134. /* initialize ready table */
  135. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  136. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  137. }
  138. /**
  139. * @brief This function will startup the scheduler. It will select one thread
  140. * with the highest priority level, then switch to it.
  141. */
  142. void rt_system_scheduler_start(void)
  143. {
  144. struct rt_thread *to_thread;
  145. rt_ubase_t highest_ready_priority;
  146. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  147. rt_current_thread = to_thread;
  148. rt_sched_remove_thread(to_thread);
  149. RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING;
  150. /* switch to new thread */
  151. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp);
  152. /* never come back */
  153. }
  154. /**
  155. * @addtogroup Thread
  156. * @cond
  157. */
  158. /**@{*/
  159. /**
  160. * @brief This function will perform scheduling once. It will select one thread
  161. * with the highest priority, and switch to it immediately.
  162. */
  163. void rt_schedule(void)
  164. {
  165. rt_base_t level;
  166. struct rt_thread *to_thread;
  167. struct rt_thread *from_thread;
  168. /* disable interrupt */
  169. level = rt_hw_interrupt_disable();
  170. /* check the scheduler is enabled or not */
  171. if (rt_scheduler_lock_nest == 0)
  172. {
  173. rt_ubase_t highest_ready_priority;
  174. if (rt_thread_ready_priority_group != 0)
  175. {
  176. /* need_insert_from_thread: need to insert from_thread to ready queue */
  177. int need_insert_from_thread = 0;
  178. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  179. if ((RT_SCHED_CTX(rt_current_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  180. {
  181. if (RT_SCHED_PRIV(rt_current_thread).current_priority < highest_ready_priority)
  182. {
  183. to_thread = rt_current_thread;
  184. }
  185. else if (RT_SCHED_PRIV(rt_current_thread).current_priority == highest_ready_priority && (RT_SCHED_CTX(rt_current_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  186. {
  187. to_thread = rt_current_thread;
  188. }
  189. else
  190. {
  191. need_insert_from_thread = 1;
  192. }
  193. }
  194. if (to_thread != rt_current_thread)
  195. {
  196. /* if the destination thread is not the same as current thread */
  197. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  198. from_thread = rt_current_thread;
  199. rt_current_thread = to_thread;
  200. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  201. if (need_insert_from_thread)
  202. {
  203. rt_sched_insert_thread(from_thread);
  204. }
  205. if ((RT_SCHED_CTX(from_thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  206. {
  207. RT_SCHED_CTX(from_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK;
  208. }
  209. rt_sched_remove_thread(to_thread);
  210. RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK);
  211. /* switch to new thread */
  212. LOG_D("[%d]switch to priority#%d "
  213. "thread:%.*s(sp:0x%08x), "
  214. "from thread:%.*s(sp: 0x%08x)",
  215. rt_interrupt_nest, highest_ready_priority,
  216. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  217. RT_NAME_MAX, from_thread->parent.name, from_thread->sp);
  218. RT_SCHEDULER_STACK_CHECK(to_thread);
  219. if (rt_interrupt_nest == 0)
  220. {
  221. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  222. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));
  223. rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  224. (rt_ubase_t)&to_thread->sp);
  225. /* enable interrupt */
  226. rt_hw_interrupt_enable(level);
  227. #ifdef RT_USING_SIGNALS
  228. /* check stat of thread for signal */
  229. level = rt_hw_interrupt_disable();
  230. if (RT_SCHED_CTX(rt_current_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING)
  231. {
  232. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  233. RT_SCHED_CTX(rt_current_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  234. rt_hw_interrupt_enable(level);
  235. /* check signal status */
  236. rt_thread_handle_sig(RT_TRUE);
  237. }
  238. else
  239. {
  240. rt_hw_interrupt_enable(level);
  241. }
  242. #endif /* RT_USING_SIGNALS */
  243. goto __exit;
  244. }
  245. else
  246. {
  247. LOG_D("switch in interrupt");
  248. rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  249. (rt_ubase_t)&to_thread->sp, from_thread, to_thread);
  250. }
  251. }
  252. else
  253. {
  254. rt_sched_remove_thread(rt_current_thread);
  255. RT_SCHED_CTX(rt_current_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(rt_current_thread).stat & ~RT_THREAD_STAT_MASK);
  256. }
  257. }
  258. }
  259. /* enable interrupt */
  260. rt_hw_interrupt_enable(level);
  261. __exit:
  262. return;
  263. }
  264. /* Normally, there isn't anyone racing with us so this operation is lockless */
  265. void rt_sched_thread_startup(struct rt_thread *thread)
  266. {
  267. #if RT_THREAD_PRIORITY_MAX > 32
  268. RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */
  269. RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number;
  270. RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */
  271. #else
  272. RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority;
  273. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  274. /* change thread stat, so we can resume it */
  275. RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND;
  276. }
  277. void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority)
  278. {
  279. rt_list_init(&RT_THREAD_LIST_NODE(thread));
  280. /* priority init */
  281. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  282. RT_SCHED_PRIV(thread).init_priority = priority;
  283. RT_SCHED_PRIV(thread).current_priority = priority;
  284. /* don't add to scheduler queue as init thread */
  285. RT_SCHED_PRIV(thread).number_mask = 0;
  286. #if RT_THREAD_PRIORITY_MAX > 32
  287. RT_SCHED_PRIV(thread).number = 0;
  288. RT_SCHED_PRIV(thread).high_mask = 0;
  289. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  290. /* tick init */
  291. RT_SCHED_PRIV(thread).init_tick = tick;
  292. RT_SCHED_PRIV(thread).remaining_tick = tick;
  293. }
  294. /**
  295. * @brief This function will insert a thread to the system ready queue. The state of
  296. * thread will be set as READY and the thread will be removed from suspend queue.
  297. *
  298. * @param thread is the thread to be inserted.
  299. *
  300. * @note Please do not invoke this function in user application.
  301. */
  302. void rt_sched_insert_thread(struct rt_thread *thread)
  303. {
  304. rt_base_t level;
  305. RT_ASSERT(thread != RT_NULL);
  306. /* disable interrupt */
  307. level = rt_hw_interrupt_disable();
  308. /* it's current thread, it should be RUNNING thread */
  309. if (thread == rt_current_thread)
  310. {
  311. RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  312. goto __exit;
  313. }
  314. /* READY thread, insert to ready queue */
  315. RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  316. /* there is no time slices left(YIELD), inserting thread before ready list*/
  317. if((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  318. {
  319. rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]),
  320. &RT_THREAD_LIST_NODE(thread));
  321. }
  322. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  323. else
  324. {
  325. rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]),
  326. &RT_THREAD_LIST_NODE(thread));
  327. }
  328. LOG_D("insert thread[%.*s], the priority: %d",
  329. RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(rt_current_thread).current_priority);
  330. /* set priority mask */
  331. #if RT_THREAD_PRIORITY_MAX > 32
  332. rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask;
  333. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  334. rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask;
  335. __exit:
  336. /* enable interrupt */
  337. rt_hw_interrupt_enable(level);
  338. }
  339. /**
  340. * @brief This function will remove a thread from system ready queue.
  341. *
  342. * @param thread is the thread to be removed.
  343. *
  344. * @note Please do not invoke this function in user application.
  345. */
  346. void rt_sched_remove_thread(struct rt_thread *thread)
  347. {
  348. rt_base_t level;
  349. RT_ASSERT(thread != RT_NULL);
  350. /* disable interrupt */
  351. level = rt_hw_interrupt_disable();
  352. LOG_D("remove thread[%.*s], the priority: %d",
  353. RT_NAME_MAX, thread->parent.name,
  354. RT_SCHED_PRIV(rt_current_thread).current_priority);
  355. /* remove thread from ready list */
  356. rt_list_remove(&RT_THREAD_LIST_NODE(thread));
  357. if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority])))
  358. {
  359. #if RT_THREAD_PRIORITY_MAX > 32
  360. rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask;
  361. if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0)
  362. {
  363. rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  364. }
  365. #else
  366. rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  367. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  368. }
  369. /* enable interrupt */
  370. rt_hw_interrupt_enable(level);
  371. }
  372. #ifdef RT_DEBUGING_CRITICAL
  373. static volatile int _critical_error_occurred = 0;
  374. void rt_exit_critical_safe(rt_base_t critical_level)
  375. {
  376. rt_base_t level;
  377. /* disable interrupt */
  378. level = rt_hw_interrupt_disable();
  379. if (!_critical_error_occurred)
  380. {
  381. if (critical_level != rt_scheduler_lock_nest)
  382. {
  383. int dummy = 1;
  384. _critical_error_occurred = 1;
  385. rt_kprintf("%s: un-compatible critical level\n" \
  386. "\tCurrent %d\n\tCaller %d\n",
  387. __func__, rt_scheduler_lock_nest,
  388. critical_level);
  389. rt_backtrace();
  390. while (dummy) ;
  391. }
  392. }
  393. rt_hw_interrupt_enable(level);
  394. rt_exit_critical();
  395. }
  396. #else /* !RT_DEBUGING_CRITICAL */
  397. void rt_exit_critical_safe(rt_base_t critical_level)
  398. {
  399. rt_exit_critical();
  400. }
  401. #endif/* RT_DEBUGING_CRITICAL */
  402. RTM_EXPORT(rt_exit_critical_safe);
  403. /**
  404. * @brief This function will lock the thread scheduler.
  405. */
  406. rt_base_t rt_enter_critical(void)
  407. {
  408. rt_base_t level;
  409. rt_base_t critical_level;
  410. /* disable interrupt */
  411. level = rt_hw_interrupt_disable();
  412. /*
  413. * the maximal number of nest is RT_UINT16_MAX, which is big
  414. * enough and does not check here
  415. */
  416. rt_scheduler_lock_nest ++;
  417. critical_level = rt_scheduler_lock_nest;
  418. /* enable interrupt */
  419. rt_hw_interrupt_enable(level);
  420. return critical_level;
  421. }
  422. RTM_EXPORT(rt_enter_critical);
  423. /**
  424. * @brief This function will unlock the thread scheduler.
  425. */
  426. void rt_exit_critical(void)
  427. {
  428. rt_base_t level;
  429. /* disable interrupt */
  430. level = rt_hw_interrupt_disable();
  431. rt_scheduler_lock_nest --;
  432. if (rt_scheduler_lock_nest <= 0)
  433. {
  434. rt_scheduler_lock_nest = 0;
  435. /* enable interrupt */
  436. rt_hw_interrupt_enable(level);
  437. if (rt_current_thread)
  438. {
  439. /* if scheduler is started, do a schedule */
  440. rt_schedule();
  441. }
  442. }
  443. else
  444. {
  445. /* enable interrupt */
  446. rt_hw_interrupt_enable(level);
  447. }
  448. }
  449. RTM_EXPORT(rt_exit_critical);
  450. /**
  451. * @brief Get the scheduler lock level.
  452. *
  453. * @return the level of the scheduler lock. 0 means unlocked.
  454. */
  455. rt_uint16_t rt_critical_level(void)
  456. {
  457. return rt_scheduler_lock_nest;
  458. }
  459. RTM_EXPORT(rt_critical_level);
  460. rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu)
  461. {
  462. return -RT_EINVAL;
  463. }
  464. /**@}*/
  465. /**@endcond*/