scheduler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. static rt_int16_t rt_scheduler_lock_nest;
  28. extern volatile rt_uint8_t rt_interrupt_nest;
  29. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  30. struct rt_thread *rt_current_thread;
  31. rt_uint8_t rt_current_priority;
  32. #if RT_THREAD_PRIORITY_MAX > 32
  33. /* Maximum priority level, 256 */
  34. rt_uint32_t rt_thread_ready_priority_group;
  35. rt_uint8_t rt_thread_ready_table[32];
  36. #else
  37. /* Maximum priority level, 32 */
  38. rt_uint32_t rt_thread_ready_priority_group;
  39. #endif
  40. rt_list_t rt_thread_defunct;
  41. #ifdef RT_USING_HOOK
  42. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  43. /**
  44. * @addtogroup Hook
  45. */
  46. /**@{*/
  47. /**
  48. * This function will set a hook function, which will be invoked when thread
  49. * switch happens.
  50. *
  51. * @param hook the hook function
  52. */
  53. void
  54. rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  55. {
  56. rt_scheduler_hook = hook;
  57. }
  58. /**@}*/
  59. #endif
  60. #ifdef RT_USING_OVERFLOW_CHECK
  61. static void _rt_scheduler_stack_check(struct rt_thread *thread)
  62. {
  63. RT_ASSERT(thread != RT_NULL);
  64. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
  65. (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
  66. (rt_uint32_t)thread->sp >
  67. (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size)
  68. {
  69. rt_uint32_t level;
  70. rt_kprintf("thread:%s stack overflow\n", thread->name);
  71. #ifdef RT_USING_FINSH
  72. {
  73. extern long list_thread(void);
  74. list_thread();
  75. }
  76. #endif
  77. level = rt_hw_interrupt_disable();
  78. while (level);
  79. }
  80. #if defined(ARCH_CPU_STACK_GROWS_UPWARD)
  81. else if ((rt_uint32_t)thread->sp > ((rt_uint32_t)thread->stack_addr + thread->stack_size))
  82. {
  83. rt_kprintf("warning: %s stack is close to the top of stack address.\n",
  84. thread->name);
  85. }
  86. #else
  87. else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
  88. {
  89. rt_kprintf("warning: %s stack is close to the bottom of stack address.\n",
  90. thread->name);
  91. }
  92. #endif
  93. }
  94. #endif
  95. /**
  96. * @ingroup SystemInit
  97. * This function will initialize the system scheduler
  98. */
  99. void rt_system_scheduler_init(void)
  100. {
  101. register rt_base_t offset;
  102. rt_scheduler_lock_nest = 0;
  103. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
  104. RT_THREAD_PRIORITY_MAX));
  105. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  106. {
  107. rt_list_init(&rt_thread_priority_table[offset]);
  108. }
  109. rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
  110. rt_current_thread = RT_NULL;
  111. /* initialize ready priority group */
  112. rt_thread_ready_priority_group = 0;
  113. #if RT_THREAD_PRIORITY_MAX > 32
  114. /* initialize ready table */
  115. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  116. #endif
  117. /* initialize thread defunct */
  118. rt_list_init(&rt_thread_defunct);
  119. }
  120. /**
  121. * @ingroup SystemInit
  122. * This function will startup scheduler. It will select one thread
  123. * with the highest priority level, then switch to it.
  124. */
  125. void rt_system_scheduler_start(void)
  126. {
  127. register struct rt_thread *to_thread;
  128. register rt_ubase_t highest_ready_priority;
  129. #if RT_THREAD_PRIORITY_MAX > 32
  130. register rt_ubase_t number;
  131. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  132. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  133. #else
  134. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  135. #endif
  136. /* get switch to thread */
  137. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  138. struct rt_thread,
  139. tlist);
  140. rt_current_thread = to_thread;
  141. /* switch to new thread */
  142. rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
  143. /* never come back */
  144. }
  145. /**
  146. * @addtogroup Thread
  147. */
  148. /**@{*/
  149. /**
  150. * This function will perform one schedule. It will select one thread
  151. * with the highest priority level, then switch to it.
  152. */
  153. void rt_schedule(void)
  154. {
  155. rt_base_t level;
  156. struct rt_thread *to_thread;
  157. struct rt_thread *from_thread;
  158. /* disable interrupt */
  159. level = rt_hw_interrupt_disable();
  160. /* check the scheduler is enabled or not */
  161. if (rt_scheduler_lock_nest == 0)
  162. {
  163. register rt_ubase_t highest_ready_priority;
  164. #if RT_THREAD_PRIORITY_MAX <= 32
  165. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  166. #else
  167. register rt_ubase_t number;
  168. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  169. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  170. #endif
  171. /* get switch to thread */
  172. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  173. struct rt_thread,
  174. tlist);
  175. /* if the destination thread is not the same as current thread */
  176. if (to_thread != rt_current_thread)
  177. {
  178. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  179. from_thread = rt_current_thread;
  180. rt_current_thread = to_thread;
  181. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  182. /* switch to new thread */
  183. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  184. ("[%d]switch to priority#%d "
  185. "thread:%.*s(sp:0x%p), "
  186. "from thread:%.*s(sp: 0x%p)\n",
  187. rt_interrupt_nest, highest_ready_priority,
  188. RT_NAME_MAX, to_thread->name, to_thread->sp,
  189. RT_NAME_MAX, from_thread->name, from_thread->sp));
  190. #ifdef RT_USING_OVERFLOW_CHECK
  191. _rt_scheduler_stack_check(to_thread);
  192. #endif
  193. if (rt_interrupt_nest == 0)
  194. {
  195. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  196. rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
  197. (rt_uint32_t)&to_thread->sp);
  198. /* enable interrupt */
  199. rt_hw_interrupt_enable(level);
  200. #ifdef RT_USING_SIGNALS
  201. /* check signal status */
  202. rt_thread_handle_sig(RT_TRUE);
  203. #endif
  204. }
  205. else
  206. {
  207. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  208. rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
  209. (rt_uint32_t)&to_thread->sp);
  210. /* enable interrupt */
  211. rt_hw_interrupt_enable(level);
  212. }
  213. }
  214. else
  215. {
  216. /* enable interrupt */
  217. rt_hw_interrupt_enable(level);
  218. }
  219. }
  220. else
  221. {
  222. /* enable interrupt */
  223. rt_hw_interrupt_enable(level);
  224. }
  225. }
  226. /*
  227. * This function will insert a thread to system ready queue. The state of
  228. * thread will be set as READY and remove from suspend queue.
  229. *
  230. * @param thread the thread to be inserted
  231. * @note Please do not invoke this function in user application.
  232. */
  233. void rt_schedule_insert_thread(struct rt_thread *thread)
  234. {
  235. register rt_base_t temp;
  236. RT_ASSERT(thread != RT_NULL);
  237. /* disable interrupt */
  238. temp = rt_hw_interrupt_disable();
  239. /* change stat */
  240. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  241. /* insert thread to ready list */
  242. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  243. &(thread->tlist));
  244. /* set priority mask */
  245. #if RT_THREAD_PRIORITY_MAX <= 32
  246. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  247. RT_NAME_MAX, thread->name, thread->current_priority));
  248. #else
  249. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  250. ("insert thread[%.*s], the priority: %d 0x%x %d\n",
  251. RT_NAME_MAX,
  252. thread->name,
  253. thread->number,
  254. thread->number_mask,
  255. thread->high_mask));
  256. #endif
  257. #if RT_THREAD_PRIORITY_MAX > 32
  258. rt_thread_ready_table[thread->number] |= thread->high_mask;
  259. #endif
  260. rt_thread_ready_priority_group |= thread->number_mask;
  261. /* enable interrupt */
  262. rt_hw_interrupt_enable(temp);
  263. }
  264. /*
  265. * This function will remove a thread from system ready queue.
  266. *
  267. * @param thread the thread to be removed
  268. *
  269. * @note Please do not invoke this function in user application.
  270. */
  271. void rt_schedule_remove_thread(struct rt_thread *thread)
  272. {
  273. register rt_base_t temp;
  274. RT_ASSERT(thread != RT_NULL);
  275. /* disable interrupt */
  276. temp = rt_hw_interrupt_disable();
  277. #if RT_THREAD_PRIORITY_MAX <= 32
  278. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  279. RT_NAME_MAX, thread->name,
  280. thread->current_priority));
  281. #else
  282. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  283. ("remove thread[%.*s], the priority: %d 0x%x %d\n",
  284. RT_NAME_MAX,
  285. thread->name,
  286. thread->number,
  287. thread->number_mask,
  288. thread->high_mask));
  289. #endif
  290. /* remove thread from ready list */
  291. rt_list_remove(&(thread->tlist));
  292. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  293. {
  294. #if RT_THREAD_PRIORITY_MAX > 32
  295. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  296. if (rt_thread_ready_table[thread->number] == 0)
  297. {
  298. rt_thread_ready_priority_group &= ~thread->number_mask;
  299. }
  300. #else
  301. rt_thread_ready_priority_group &= ~thread->number_mask;
  302. #endif
  303. }
  304. /* enable interrupt */
  305. rt_hw_interrupt_enable(temp);
  306. }
  307. /**
  308. * This function will lock the thread scheduler.
  309. */
  310. void rt_enter_critical(void)
  311. {
  312. register rt_base_t level;
  313. /* disable interrupt */
  314. level = rt_hw_interrupt_disable();
  315. /*
  316. * the maximal number of nest is RT_UINT16_MAX, which is big
  317. * enough and does not check here
  318. */
  319. rt_scheduler_lock_nest ++;
  320. /* enable interrupt */
  321. rt_hw_interrupt_enable(level);
  322. }
  323. RTM_EXPORT(rt_enter_critical);
  324. /**
  325. * This function will unlock the thread scheduler.
  326. */
  327. void rt_exit_critical(void)
  328. {
  329. register rt_base_t level;
  330. /* disable interrupt */
  331. level = rt_hw_interrupt_disable();
  332. rt_scheduler_lock_nest --;
  333. if (rt_scheduler_lock_nest <= 0)
  334. {
  335. rt_scheduler_lock_nest = 0;
  336. /* enable interrupt */
  337. rt_hw_interrupt_enable(level);
  338. rt_schedule();
  339. }
  340. else
  341. {
  342. /* enable interrupt */
  343. rt_hw_interrupt_enable(level);
  344. }
  345. }
  346. RTM_EXPORT(rt_exit_critical);
  347. /**
  348. * Get the scheduler lock level
  349. *
  350. * @return the level of the scheduler lock. 0 means unlocked.
  351. */
  352. rt_uint16_t rt_critical_level(void)
  353. {
  354. return rt_scheduler_lock_nest;
  355. }
  356. RTM_EXPORT(rt_critical_level);
  357. /**@}*/