idle.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_IDLE_HOOK_LIST_SIZE
  47. #define RT_IDLE_HOOK_LIST_SIZE 4
  48. #endif
  49. static void (*idle_hook_list[RT_IDLE_HOOK_LIST_SIZE])(void);
  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_IDLE_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_IDLE_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. /* detach this object */
  170. rt_object_detach((rt_object_t)thread);
  171. /* unlock scheduler */
  172. rt_exit_critical();
  173. /* enable interrupt */
  174. rt_hw_interrupt_enable(lock);
  175. return;
  176. }
  177. /* unlock scheduler */
  178. rt_exit_critical();
  179. }
  180. else
  181. {
  182. /* enable interrupt */
  183. rt_hw_interrupt_enable(lock);
  184. /* may the defunct thread list is removed by others, just return */
  185. return;
  186. }
  187. /* enable interrupt */
  188. rt_hw_interrupt_enable(lock);
  189. #ifdef RT_USING_HEAP
  190. /* release thread's stack */
  191. RT_KERNEL_FREE(thread->stack_addr);
  192. /* delete thread object */
  193. rt_object_delete((rt_object_t)thread);
  194. #endif
  195. }
  196. }
  197. extern void rt_system_power_manager(void);
  198. static void rt_thread_idle_entry(void *parameter)
  199. {
  200. #ifdef RT_USING_SMP
  201. if (rt_hw_cpu_id() != 0)
  202. {
  203. while (1)
  204. {
  205. rt_hw_secondary_cpu_idle_exec();
  206. }
  207. }
  208. #endif
  209. while (1)
  210. {
  211. #ifdef RT_USING_IDLE_HOOK
  212. rt_size_t i;
  213. for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  214. {
  215. if (idle_hook_list[i] != RT_NULL)
  216. {
  217. idle_hook_list[i]();
  218. }
  219. }
  220. #endif
  221. rt_thread_idle_excute();
  222. #ifdef RT_USING_PM
  223. rt_system_power_manager();
  224. #endif
  225. }
  226. }
  227. /**
  228. * @ingroup SystemInit
  229. *
  230. * This function will initialize idle thread, then start it.
  231. *
  232. * @note this function must be invoked when system init.
  233. */
  234. void rt_thread_idle_init(void)
  235. {
  236. rt_ubase_t i;
  237. char tidle_name[RT_NAME_MAX];
  238. for (i = 0; i < _CPUS_NR; i++)
  239. {
  240. rt_sprintf(tidle_name, "tidle%d", i);
  241. rt_thread_init(&idle[i],
  242. tidle_name,
  243. rt_thread_idle_entry,
  244. RT_NULL,
  245. &rt_thread_stack[i][0],
  246. sizeof(rt_thread_stack[i]),
  247. RT_THREAD_PRIORITY_MAX - 1,
  248. 32);
  249. #ifdef RT_USING_SMP
  250. rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
  251. #endif
  252. /* startup */
  253. rt_thread_startup(&idle[i]);
  254. }
  255. }
  256. /**
  257. * @ingroup Thread
  258. *
  259. * This function will get the handler of the idle thread.
  260. *
  261. */
  262. rt_thread_t rt_thread_idle_gethandler(void)
  263. {
  264. #ifdef RT_USING_SMP
  265. register int id = rt_hw_cpu_id();
  266. #else
  267. register int id = 0;
  268. #endif
  269. return (rt_thread_t)(&idle[id]);
  270. }