scheduler.c 12 KB

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