scheduler.c 12 KB

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