scheduler.c 12 KB

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