scheduler.c 12 KB

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