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