scheduler.c 12 KB

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