scheduler.c 10 KB

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