scheduler_up.c 17 KB

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