scheduler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * File : scheduler.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-17 Bernard the first version
  13. * 2006-04-28 Bernard fix the scheduler algorthm
  14. * 2006-04-30 Bernard add SCHEDULER_DEBUG
  15. * 2006-05-27 Bernard fix the scheduler algorthm for same priority thread
  16. * schedule
  17. * 2006-06-04 Bernard rewrite the scheduler algorithm
  18. * 2006-08-03 Bernard add hook support
  19. * 2006-09-05 Bernard add 32 priority level support
  20. * 2006-09-24 Bernard add rt_system_scheduler_start function
  21. * 2009-09-16 Bernard fix _rt_scheduler_stack_check
  22. * 2010-04-11 yi.qiu add module feature
  23. * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest
  24. * issue found by kuronca
  25. */
  26. #include <rtthread.h>
  27. #include <rthw.h>
  28. #include "kservice.h"
  29. /* #define SCHEDULER_DEBUG */
  30. static rt_int16_t rt_scheduler_lock_nest;
  31. extern volatile rt_uint8_t rt_interrupt_nest;
  32. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  33. struct rt_thread* rt_current_thread;
  34. rt_uint8_t rt_current_priority;
  35. #if RT_THREAD_PRIORITY_MAX > 32
  36. /* maximun priority level, 256 */
  37. rt_uint32_t rt_thread_ready_priority_group;
  38. rt_uint8_t rt_thread_ready_table[32];
  39. #else
  40. /* maximun priority level, 32 */
  41. rt_uint32_t rt_thread_ready_priority_group;
  42. #endif
  43. #ifdef RT_USING_HEAP
  44. rt_list_t rt_thread_defunct;
  45. #endif
  46. const rt_uint8_t rt_lowest_bitmap[] =
  47. {
  48. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  49. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  50. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  51. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  52. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  53. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  54. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  55. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  56. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  57. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  58. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  59. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  60. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  61. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  62. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  63. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  64. };
  65. #ifdef RT_USING_HOOK
  66. static void (*rt_scheduler_hook)(struct rt_thread* from, struct rt_thread* to);
  67. /**
  68. * @addtogroup Hook
  69. */
  70. /*@{*/
  71. /**
  72. * This function will set a hook function, which will be invoked when thread
  73. * switch happens.
  74. *
  75. * @param hook the hook function
  76. */
  77. void rt_scheduler_sethook(void (*hook)(struct rt_thread* from, struct rt_thread* to))
  78. {
  79. rt_scheduler_hook = hook;
  80. }
  81. /*@}*/
  82. #endif
  83. #ifdef RT_USING_OVERFLOW_CHECK
  84. static void _rt_scheduler_stack_check(struct rt_thread* thread)
  85. {
  86. RT_ASSERT(thread != RT_NULL);
  87. if ( (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
  88. (rt_uint32_t)thread->sp >
  89. (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size )
  90. {
  91. rt_uint32_t level;
  92. rt_kprintf("thread:%s stack overflow\n", thread->name);
  93. level = rt_hw_interrupt_disable();
  94. while (level);
  95. }
  96. else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
  97. {
  98. rt_kprintf("warning: %s stack is close to end of stack address.\n",
  99. thread->name);
  100. }
  101. }
  102. #endif
  103. /**
  104. * @ingroup SystemInit
  105. * This function will init the system scheduler
  106. *
  107. */
  108. void rt_system_scheduler_init(void)
  109. {
  110. register rt_base_t offset;
  111. rt_scheduler_lock_nest = 0;
  112. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  113. {
  114. rt_list_init(&rt_thread_priority_table[offset]);
  115. }
  116. rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
  117. rt_current_thread = RT_NULL;
  118. /* init ready priority group */
  119. rt_thread_ready_priority_group = 0;
  120. #if RT_THREAD_PRIORITY_MAX > 32
  121. /* init ready table */
  122. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  123. #endif
  124. #ifdef RT_USING_HEAP
  125. /* init thread defunct */
  126. rt_list_init(&rt_thread_defunct);
  127. #endif
  128. }
  129. /**
  130. * @ingroup SystemInit
  131. * This function will startup scheduler. It will select one thread
  132. * with the highest priority level, then switch to it.
  133. */
  134. void rt_system_scheduler_start()
  135. {
  136. register struct rt_thread *to_thread;
  137. register rt_ubase_t highest_ready_priority;
  138. #if RT_THREAD_PRIORITY_MAX == 8
  139. highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group];
  140. #else
  141. register rt_ubase_t number;
  142. /* find out the highest priority task */
  143. if (rt_thread_ready_priority_group & 0xff)
  144. {
  145. number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff];
  146. }
  147. else if (rt_thread_ready_priority_group & 0xff00)
  148. {
  149. number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8;
  150. }
  151. else if (rt_thread_ready_priority_group & 0xff0000)
  152. {
  153. number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16;
  154. }
  155. else
  156. {
  157. number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24;
  158. }
  159. #if RT_THREAD_PRIORITY_MAX > 32
  160. highest_ready_priority = (number << 3) + rt_lowest_bitmap[rt_thread_ready_table[number]];
  161. #else
  162. highest_ready_priority = number;
  163. #endif
  164. #endif
  165. /* get switch to thread */
  166. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  167. struct rt_thread, tlist);
  168. rt_current_thread = to_thread;
  169. /* switch to new thread */
  170. rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
  171. /* never come back */
  172. }
  173. /**
  174. * @addtogroup Thread
  175. */
  176. /*@{*/
  177. /**
  178. * This function will perform one schedule. It will select one thread
  179. * with the highest priority level, then switch to it.
  180. */
  181. void rt_schedule()
  182. {
  183. rt_base_t level;
  184. struct rt_thread *to_thread;
  185. struct rt_thread *from_thread;
  186. /* disable interrupt */
  187. level = rt_hw_interrupt_disable();
  188. /* check the scheduler is enabled or not */
  189. if (rt_scheduler_lock_nest == 0)
  190. {
  191. register rt_ubase_t highest_ready_priority;
  192. #if RT_THREAD_PRIORITY_MAX == 8
  193. highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group];
  194. #else
  195. register rt_ubase_t number;
  196. /* find out the highest priority task */
  197. if (rt_thread_ready_priority_group & 0xff)
  198. {
  199. number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff];
  200. }
  201. else if (rt_thread_ready_priority_group & 0xff00)
  202. {
  203. number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8;
  204. }
  205. else if (rt_thread_ready_priority_group & 0xff0000)
  206. {
  207. number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16;
  208. }
  209. else
  210. {
  211. number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24;
  212. }
  213. #if RT_THREAD_PRIORITY_MAX > 32
  214. highest_ready_priority = (number << 3) + rt_lowest_bitmap[rt_thread_ready_table[number]];
  215. #else
  216. highest_ready_priority = number;
  217. #endif
  218. #endif
  219. /* get switch to thread */
  220. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  221. struct rt_thread, tlist);
  222. /* if the destination thread is not the same as current thread */
  223. if (to_thread != rt_current_thread)
  224. {
  225. rt_current_priority = highest_ready_priority;
  226. from_thread = rt_current_thread;
  227. rt_current_thread = to_thread;
  228. #ifdef RT_USING_MODULE
  229. rt_module_set ((rt_current_thread->module_parent != RT_NULL) ?
  230. rt_current_thread->module_parent : RT_NULL);
  231. #endif
  232. #ifdef RT_USING_HOOK
  233. if (rt_scheduler_hook != RT_NULL) rt_scheduler_hook(from_thread, to_thread);
  234. #endif
  235. /* switch to new thread */
  236. #ifdef SCHEDULER_DEBUG
  237. rt_kprintf("[%d]switch to priority#%d thread:%s\n", rt_interrupt_nest,
  238. highest_ready_priority, to_thread->name);
  239. #endif
  240. #ifdef RT_USING_OVERFLOW_CHECK
  241. _rt_scheduler_stack_check(to_thread);
  242. #endif
  243. if (rt_interrupt_nest == 0)
  244. {
  245. rt_hw_context_switch((rt_uint32_t)&from_thread->sp, (rt_uint32_t)&to_thread->sp);
  246. }
  247. else
  248. {
  249. #ifdef SCHEDULER_DEBUG
  250. rt_kprintf("switch in interrupt\n");
  251. #endif
  252. rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
  253. (rt_uint32_t)&to_thread->sp);
  254. }
  255. }
  256. }
  257. /* enable interrupt */
  258. rt_hw_interrupt_enable(level);
  259. }
  260. /**
  261. * This function will insert a thread to system ready queue. The state of
  262. * thread will be set as READY and remove from suspend queue.
  263. *
  264. * @param thread the thread to be inserted
  265. * @note Please do not invoke this function in user application.
  266. */
  267. void rt_schedule_insert_thread(struct rt_thread* thread)
  268. {
  269. register rt_base_t temp;
  270. RT_ASSERT(thread != RT_NULL);
  271. /* disable interrupt */
  272. temp = rt_hw_interrupt_disable();
  273. /* change stat */
  274. thread->stat = RT_THREAD_READY;
  275. /* insert thread to ready list */
  276. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  277. &(thread->tlist));
  278. /* set priority mask */
  279. #ifdef SCHEDULER_DEBUG
  280. #if RT_THREAD_PRIORITY_MAX <= 32
  281. rt_kprintf("insert thread, the priority: %d\n", thread->current_priority);
  282. #else
  283. rt_kprintf("insert thread, the priority: %d 0x%x %d\n", thread->number, thread->number_mask, thread->high_mask);
  284. #endif
  285. #endif
  286. #if RT_THREAD_PRIORITY_MAX > 32
  287. rt_thread_ready_table[thread->number] |= thread->high_mask;
  288. #endif
  289. rt_thread_ready_priority_group |= thread->number_mask;
  290. /* enable interrupt */
  291. rt_hw_interrupt_enable(temp);
  292. }
  293. /**
  294. * This function will remove a thread from system ready queue.
  295. *
  296. * @param thread the thread to be removed
  297. *
  298. * @note Please do not invoke this function in user application.
  299. */
  300. void rt_schedule_remove_thread(struct rt_thread* thread)
  301. {
  302. register rt_base_t temp;
  303. RT_ASSERT(thread != RT_NULL);
  304. /* disable interrupt */
  305. temp = rt_hw_interrupt_disable();
  306. #ifdef SCHEDULER_DEBUG
  307. #if RT_THREAD_PRIORITY_MAX <= 32
  308. rt_kprintf("remove thread, the priority: %d\n", thread->current_priority);
  309. #else
  310. rt_kprintf("remove thread, the priority: %d 0x%x %d\n", thread->number,
  311. thread->number_mask, thread->high_mask);
  312. #endif
  313. #endif
  314. /* remove thread from ready list */
  315. rt_list_remove(&(thread->tlist));
  316. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  317. {
  318. #if RT_THREAD_PRIORITY_MAX > 32
  319. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  320. if (rt_thread_ready_table[thread->number] == 0)
  321. {
  322. rt_thread_ready_priority_group &= ~thread->number_mask;
  323. }
  324. #else
  325. rt_thread_ready_priority_group &= ~thread->number_mask;
  326. #endif
  327. }
  328. /* enable interrupt */
  329. rt_hw_interrupt_enable(temp);
  330. }
  331. /**
  332. * This function will lock the thread scheduler.
  333. */
  334. void rt_enter_critical()
  335. {
  336. register rt_base_t level;
  337. /* disable interrupt */
  338. level = rt_hw_interrupt_disable();
  339. /* the maximal number of nest is RT_UINT16_MAX, which is big
  340. * enough and does not check here */
  341. rt_scheduler_lock_nest++;
  342. /* enable interrupt */
  343. rt_hw_interrupt_enable(level);
  344. }
  345. /**
  346. * This function will unlock the thread scheduler.
  347. */
  348. void rt_exit_critical()
  349. {
  350. register rt_base_t level;
  351. /* disable interrupt */
  352. level = rt_hw_interrupt_disable();
  353. rt_scheduler_lock_nest --;
  354. if (rt_scheduler_lock_nest <= 0)
  355. {
  356. rt_scheduler_lock_nest = 0;
  357. /* enable interrupt */
  358. rt_hw_interrupt_enable(level);
  359. rt_schedule();
  360. }
  361. else
  362. {
  363. /* enable interrupt */
  364. rt_hw_interrupt_enable(level);
  365. }
  366. }
  367. /*@}*/