scheduler.c 11 KB

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