scheduler.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-17 Bernard the first version
  23. * 2006-04-28 Bernard fix the scheduler algorthm
  24. * 2006-04-30 Bernard add SCHEDULER_DEBUG
  25. * 2006-05-27 Bernard fix the scheduler algorthm for same priority
  26. * thread schedule
  27. * 2006-06-04 Bernard rewrite the scheduler algorithm
  28. * 2006-08-03 Bernard add hook support
  29. * 2006-09-05 Bernard add 32 priority level support
  30. * 2006-09-24 Bernard add rt_system_scheduler_start function
  31. * 2009-09-16 Bernard fix _rt_scheduler_stack_check
  32. * 2010-04-11 yi.qiu add module feature
  33. * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest
  34. * issue found by kuronca
  35. * 2010-12-13 Bernard add defunct list initialization even if not use heap.
  36. * 2011-05-10 Bernard clean scheduler debug log.
  37. */
  38. #include <rtthread.h>
  39. #include <rthw.h>
  40. static rt_int16_t rt_scheduler_lock_nest;
  41. extern volatile rt_uint8_t rt_interrupt_nest;
  42. extern int __rt_ffs(int value);
  43. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  44. struct rt_thread *rt_current_thread;
  45. rt_uint8_t rt_current_priority;
  46. #if RT_THREAD_PRIORITY_MAX > 32
  47. /* Maximum priority level, 256 */
  48. rt_uint32_t rt_thread_ready_priority_group;
  49. rt_uint8_t rt_thread_ready_table[32];
  50. #else
  51. /* Maximum priority level, 32 */
  52. rt_uint32_t rt_thread_ready_priority_group;
  53. #endif
  54. rt_list_t rt_thread_defunct;
  55. #ifdef RT_USING_HOOK
  56. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  57. /**
  58. * @addtogroup Hook
  59. */
  60. /*@{*/
  61. /**
  62. * This function will set a hook function, which will be invoked when thread
  63. * switch happens.
  64. *
  65. * @param hook the hook function
  66. */
  67. void
  68. rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  69. {
  70. rt_scheduler_hook = hook;
  71. }
  72. /*@}*/
  73. #endif
  74. #ifdef RT_USING_OVERFLOW_CHECK
  75. static void _rt_scheduler_stack_check(struct rt_thread *thread)
  76. {
  77. RT_ASSERT(thread != RT_NULL);
  78. if ((rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
  79. (rt_uint32_t)thread->sp >
  80. (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size)
  81. {
  82. rt_uint32_t level;
  83. rt_kprintf("thread:%s stack overflow\n", thread->name);
  84. #ifdef RT_USING_FINSH
  85. {
  86. extern long list_thread(void);
  87. list_thread();
  88. }
  89. #endif
  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 initialize the system scheduler
  103. */
  104. void rt_system_scheduler_init(void)
  105. {
  106. register rt_base_t offset;
  107. rt_scheduler_lock_nest = 0;
  108. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
  109. RT_THREAD_PRIORITY_MAX));
  110. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  111. {
  112. rt_list_init(&rt_thread_priority_table[offset]);
  113. }
  114. rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
  115. rt_current_thread = RT_NULL;
  116. /* initialize ready priority group */
  117. rt_thread_ready_priority_group = 0;
  118. #if RT_THREAD_PRIORITY_MAX > 32
  119. /* initialize ready table */
  120. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  121. #endif
  122. /* initialize thread defunct */
  123. rt_list_init(&rt_thread_defunct);
  124. }
  125. /**
  126. * @ingroup SystemInit
  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(void)
  131. {
  132. register struct rt_thread *to_thread;
  133. register rt_ubase_t highest_ready_priority;
  134. #if RT_THREAD_PRIORITY_MAX > 32
  135. register rt_ubase_t number;
  136. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  137. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  138. #else
  139. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  140. #endif
  141. /* get switch to thread */
  142. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  143. struct rt_thread,
  144. tlist);
  145. rt_current_thread = to_thread;
  146. /* switch to new thread */
  147. rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
  148. /* never come back */
  149. }
  150. /**
  151. * @addtogroup Thread
  152. */
  153. /*@{*/
  154. /**
  155. * This function will perform one schedule. It will select one thread
  156. * with the highest priority level, then switch to it.
  157. */
  158. void rt_schedule(void)
  159. {
  160. rt_base_t level;
  161. struct rt_thread *to_thread;
  162. struct rt_thread *from_thread;
  163. /* disable interrupt */
  164. level = rt_hw_interrupt_disable();
  165. /* check the scheduler is enabled or not */
  166. if (rt_scheduler_lock_nest == 0)
  167. {
  168. register rt_ubase_t highest_ready_priority;
  169. #if RT_THREAD_PRIORITY_MAX <= 32
  170. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  171. #else
  172. register rt_ubase_t number;
  173. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  174. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  175. #endif
  176. /* get switch to thread */
  177. to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  178. struct rt_thread,
  179. tlist);
  180. /* if the destination thread is not the same as current thread */
  181. if (to_thread != rt_current_thread)
  182. {
  183. rt_current_priority = (rt_uint8_t)highest_ready_priority;
  184. from_thread = rt_current_thread;
  185. rt_current_thread = to_thread;
  186. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
  187. /* switch to new thread */
  188. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  189. ("[%d]switch to priority#%d "
  190. "thread:%.*s(sp:0x%p), "
  191. "from thread:%.*s(sp: 0x%p)\n",
  192. rt_interrupt_nest, highest_ready_priority,
  193. RT_NAME_MAX, to_thread->name, to_thread->sp,
  194. RT_NAME_MAX, from_thread->name, from_thread->sp));
  195. #ifdef RT_USING_OVERFLOW_CHECK
  196. _rt_scheduler_stack_check(to_thread);
  197. #endif
  198. if (rt_interrupt_nest == 0)
  199. {
  200. rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
  201. (rt_uint32_t)&to_thread->sp);
  202. }
  203. else
  204. {
  205. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
  206. rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
  207. (rt_uint32_t)&to_thread->sp);
  208. }
  209. }
  210. }
  211. /* enable interrupt */
  212. rt_hw_interrupt_enable(level);
  213. }
  214. /*
  215. * This function will insert a thread to system ready queue. The state of
  216. * thread will be set as READY and remove from suspend queue.
  217. *
  218. * @param thread the thread to be inserted
  219. * @note Please do not invoke this function in user application.
  220. */
  221. void rt_schedule_insert_thread(struct rt_thread *thread)
  222. {
  223. register rt_base_t temp;
  224. RT_ASSERT(thread != RT_NULL);
  225. /* disable interrupt */
  226. temp = rt_hw_interrupt_disable();
  227. /* change stat */
  228. thread->stat = RT_THREAD_READY;
  229. /* insert thread to ready list */
  230. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  231. &(thread->tlist));
  232. /* set priority mask */
  233. #if RT_THREAD_PRIORITY_MAX <= 32
  234. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
  235. RT_NAME_MAX, thread->name, thread->current_priority));
  236. #else
  237. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  238. ("insert thread[%.*s], the priority: %d 0x%x %d\n",
  239. RT_NAME_MAX,
  240. thread->name,
  241. thread->number,
  242. thread->number_mask,
  243. thread->high_mask));
  244. #endif
  245. #if RT_THREAD_PRIORITY_MAX > 32
  246. rt_thread_ready_table[thread->number] |= thread->high_mask;
  247. #endif
  248. rt_thread_ready_priority_group |= thread->number_mask;
  249. /* enable interrupt */
  250. rt_hw_interrupt_enable(temp);
  251. }
  252. /*
  253. * This function will remove a thread from system ready queue.
  254. *
  255. * @param thread the thread to be removed
  256. *
  257. * @note Please do not invoke this function in user application.
  258. */
  259. void rt_schedule_remove_thread(struct rt_thread *thread)
  260. {
  261. register rt_base_t temp;
  262. RT_ASSERT(thread != RT_NULL);
  263. /* disable interrupt */
  264. temp = rt_hw_interrupt_disable();
  265. #if RT_THREAD_PRIORITY_MAX <= 32
  266. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
  267. RT_NAME_MAX, thread->name,
  268. thread->current_priority));
  269. #else
  270. RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  271. ("remove thread[%.*s], the priority: %d 0x%x %d\n",
  272. RT_NAME_MAX,
  273. thread->name,
  274. thread->number,
  275. thread->number_mask,
  276. thread->high_mask));
  277. #endif
  278. /* remove thread from ready list */
  279. rt_list_remove(&(thread->tlist));
  280. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  281. {
  282. #if RT_THREAD_PRIORITY_MAX > 32
  283. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  284. if (rt_thread_ready_table[thread->number] == 0)
  285. {
  286. rt_thread_ready_priority_group &= ~thread->number_mask;
  287. }
  288. #else
  289. rt_thread_ready_priority_group &= ~thread->number_mask;
  290. #endif
  291. }
  292. /* enable interrupt */
  293. rt_hw_interrupt_enable(temp);
  294. }
  295. /**
  296. * This function will lock the thread scheduler.
  297. */
  298. void rt_enter_critical(void)
  299. {
  300. register rt_base_t level;
  301. /* disable interrupt */
  302. level = rt_hw_interrupt_disable();
  303. /*
  304. * the maximal number of nest is RT_UINT16_MAX, which is big
  305. * enough and does not check here
  306. */
  307. rt_scheduler_lock_nest ++;
  308. /* enable interrupt */
  309. rt_hw_interrupt_enable(level);
  310. }
  311. /**
  312. * This function will unlock the thread scheduler.
  313. */
  314. void rt_exit_critical(void)
  315. {
  316. register rt_base_t level;
  317. /* disable interrupt */
  318. level = rt_hw_interrupt_disable();
  319. rt_scheduler_lock_nest --;
  320. if (rt_scheduler_lock_nest <= 0)
  321. {
  322. rt_scheduler_lock_nest = 0;
  323. /* enable interrupt */
  324. rt_hw_interrupt_enable(level);
  325. rt_schedule();
  326. }
  327. else
  328. {
  329. /* enable interrupt */
  330. rt_hw_interrupt_enable(level);
  331. }
  332. }
  333. /*@}*/