idle.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-23 Bernard the first version
  9. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  10. * 2012-12-29 Bernard fix compiling warning.
  11. * 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no
  12. * dead thread.
  13. * 2016-08-09 ArdaFu add method to get the handler of the idle thread.
  14. * 2018-02-07 Bernard lock scheduler to protect tid->cleanup.
  15. * 2018-07-14 armink add idle hook list
  16. * 2018-11-22 Jesven add per cpu idle task
  17. * combine the code of primary and secondary cpu
  18. */
  19. #include <rthw.h>
  20. #include <rtthread.h>
  21. #ifdef RT_USING_MODULE
  22. #include <dlmodule.h>
  23. #endif
  24. #if defined (RT_USING_HOOK)
  25. #ifndef RT_USING_IDLE_HOOK
  26. #define RT_USING_IDLE_HOOK
  27. #endif
  28. #endif
  29. #ifndef IDLE_THREAD_STACK_SIZE
  30. #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
  31. #define IDLE_THREAD_STACK_SIZE 256
  32. #else
  33. #define IDLE_THREAD_STACK_SIZE 128
  34. #endif
  35. #endif
  36. #ifdef RT_USING_SMP
  37. #define _CPUS_NR RT_CPUS_NR
  38. #else
  39. #define _CPUS_NR 1
  40. #endif
  41. extern rt_list_t rt_thread_defunct;
  42. static struct rt_thread idle[_CPUS_NR];
  43. ALIGN(RT_ALIGN_SIZE)
  44. static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];
  45. #ifdef RT_USING_IDLE_HOOK
  46. #ifndef RT_IDEL_HOOK_LIST_SIZE
  47. #define RT_IDEL_HOOK_LIST_SIZE 4
  48. #endif
  49. static void (*idle_hook_list[RT_IDEL_HOOK_LIST_SIZE])();
  50. /**
  51. * @ingroup Hook
  52. * This function sets a hook function to idle thread loop. When the system performs
  53. * idle loop, this hook function should be invoked.
  54. *
  55. * @param hook the specified hook function
  56. *
  57. * @return RT_EOK: set OK
  58. * -RT_EFULL: hook list is full
  59. *
  60. * @note the hook function must be simple and never be blocked or suspend.
  61. */
  62. rt_err_t rt_thread_idle_sethook(void (*hook)(void))
  63. {
  64. rt_size_t i;
  65. rt_base_t level;
  66. rt_err_t ret = -RT_EFULL;
  67. /* disable interrupt */
  68. level = rt_hw_interrupt_disable();
  69. for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
  70. {
  71. if (idle_hook_list[i] == RT_NULL)
  72. {
  73. idle_hook_list[i] = hook;
  74. ret = RT_EOK;
  75. break;
  76. }
  77. }
  78. /* enable interrupt */
  79. rt_hw_interrupt_enable(level);
  80. return ret;
  81. }
  82. /**
  83. * delete the idle hook on hook list
  84. *
  85. * @param hook the specified hook function
  86. *
  87. * @return RT_EOK: delete OK
  88. * -RT_ENOSYS: hook was not found
  89. */
  90. rt_err_t rt_thread_idle_delhook(void (*hook)(void))
  91. {
  92. rt_size_t i;
  93. rt_base_t level;
  94. rt_err_t ret = -RT_ENOSYS;
  95. /* disable interrupt */
  96. level = rt_hw_interrupt_disable();
  97. for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
  98. {
  99. if (idle_hook_list[i] == hook)
  100. {
  101. idle_hook_list[i] = RT_NULL;
  102. ret = RT_EOK;
  103. break;
  104. }
  105. }
  106. /* enable interrupt */
  107. rt_hw_interrupt_enable(level);
  108. return ret;
  109. }
  110. #endif
  111. /* Return whether there is defunctional thread to be deleted. */
  112. rt_inline int _has_defunct_thread(void)
  113. {
  114. /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
  115. * So the compiler has a good reason that the rt_thread_defunct list does
  116. * not change within rt_thread_idle_excute thus optimize the "while" loop
  117. * into a "if".
  118. *
  119. * So add the volatile qualifier here. */
  120. const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;
  121. return l->next != l;
  122. }
  123. /**
  124. * @ingroup Thread
  125. *
  126. * This function will perform system background job when system idle.
  127. */
  128. void rt_thread_idle_excute(void)
  129. {
  130. /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
  131. * will do all the cleanups. */
  132. while (_has_defunct_thread())
  133. {
  134. rt_base_t lock;
  135. rt_thread_t thread;
  136. #ifdef RT_USING_MODULE
  137. struct rt_dlmodule *module = RT_NULL;
  138. #endif
  139. RT_DEBUG_NOT_IN_INTERRUPT;
  140. /* disable interrupt */
  141. lock = rt_hw_interrupt_disable();
  142. /* re-check whether list is empty */
  143. if (_has_defunct_thread())
  144. {
  145. /* get defunct thread */
  146. thread = rt_list_entry(rt_thread_defunct.next,
  147. struct rt_thread,
  148. tlist);
  149. #ifdef RT_USING_MODULE
  150. module = (struct rt_dlmodule*)thread->module_id;
  151. if (module)
  152. {
  153. dlmodule_destroy(module);
  154. }
  155. #endif
  156. /* remove defunct thread */
  157. rt_list_remove(&(thread->tlist));
  158. /* lock scheduler to prevent scheduling in cleanup function. */
  159. rt_enter_critical();
  160. /* invoke thread cleanup */
  161. if (thread->cleanup != RT_NULL)
  162. thread->cleanup(thread);
  163. #ifdef RT_USING_SIGNALS
  164. rt_thread_free_sig(thread);
  165. #endif
  166. /* if it's a system object, not delete it */
  167. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  168. {
  169. /* unlock scheduler */
  170. rt_exit_critical();
  171. /* enable interrupt */
  172. rt_hw_interrupt_enable(lock);
  173. return;
  174. }
  175. /* unlock scheduler */
  176. rt_exit_critical();
  177. }
  178. else
  179. {
  180. /* enable interrupt */
  181. rt_hw_interrupt_enable(lock);
  182. /* may the defunct thread list is removed by others, just return */
  183. return;
  184. }
  185. /* enable interrupt */
  186. rt_hw_interrupt_enable(lock);
  187. #ifdef RT_USING_HEAP
  188. /* release thread's stack */
  189. RT_KERNEL_FREE(thread->stack_addr);
  190. /* delete thread object */
  191. rt_object_delete((rt_object_t)thread);
  192. #endif
  193. }
  194. }
  195. static void rt_thread_idle_entry(void *parameter)
  196. {
  197. #ifdef RT_USING_SMP
  198. if (rt_hw_cpu_id() != 0)
  199. {
  200. while (1)
  201. {
  202. rt_hw_secondary_cpu_idle_exec();
  203. }
  204. }
  205. #endif
  206. while (1)
  207. {
  208. #ifdef RT_USING_IDLE_HOOK
  209. rt_size_t i;
  210. for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
  211. {
  212. if (idle_hook_list[i] != RT_NULL)
  213. {
  214. idle_hook_list[i]();
  215. }
  216. }
  217. #endif
  218. rt_thread_idle_excute();
  219. }
  220. }
  221. /**
  222. * @ingroup SystemInit
  223. *
  224. * This function will initialize idle thread, then start it.
  225. *
  226. * @note this function must be invoked when system init.
  227. */
  228. void rt_thread_idle_init(void)
  229. {
  230. int i;
  231. char tidle_name[RT_NAME_MAX];
  232. for (i = 0; i < _CPUS_NR; i++)
  233. {
  234. rt_sprintf(tidle_name, "tidle%d", i);
  235. rt_thread_init(&idle[i],
  236. tidle_name,
  237. rt_thread_idle_entry,
  238. RT_NULL,
  239. &rt_thread_stack[i][0],
  240. sizeof(rt_thread_stack[i]),
  241. RT_THREAD_PRIORITY_MAX - 1,
  242. 32);
  243. #ifdef RT_USING_SMP
  244. rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
  245. #endif
  246. /* startup */
  247. rt_thread_startup(&idle[i]);
  248. }
  249. }
  250. /**
  251. * @ingroup Thread
  252. *
  253. * This function will get the handler of the idle thread.
  254. *
  255. */
  256. rt_thread_t rt_thread_idle_gethandler(void)
  257. {
  258. #ifdef RT_USING_SMP
  259. register int id = rt_hw_cpu_id();
  260. #else
  261. register int id = 0;
  262. #endif
  263. return (rt_thread_t)(&idle[id]);
  264. }