idle.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2006-2021, 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. #ifdef RT_USING_HEAP
  112. /* Return whether there is defunctional thread to be deleted. */
  113. rt_inline int _has_defunct_thread(void)
  114. {
  115. /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
  116. * So the compiler has a good reason that the rt_thread_defunct list does
  117. * not change within rt_thread_idle_excute thus optimize the "while" loop
  118. * into a "if".
  119. *
  120. * So add the volatile qualifier here. */
  121. const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;
  122. return l->next != l;
  123. }
  124. #endif
  125. /**
  126. * @ingroup Thread
  127. *
  128. * This function will perform system background job when system idle.
  129. */
  130. void rt_thread_idle_excute(void)
  131. {
  132. /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
  133. * will do all the cleanups. */
  134. /* disable interrupt */
  135. RT_DEBUG_NOT_IN_INTERRUPT;
  136. #ifdef RT_USING_HEAP
  137. while (1)
  138. {
  139. rt_base_t lock;
  140. rt_thread_t thread;
  141. lock = rt_hw_interrupt_disable();
  142. /* check whether list is empty */
  143. if (!_has_defunct_thread())
  144. {
  145. rt_hw_interrupt_enable(lock);
  146. break;
  147. }
  148. /* get defunct thread */
  149. thread = rt_list_entry(rt_thread_defunct.next,
  150. struct rt_thread,
  151. tlist);
  152. /* remove defunct thread */
  153. rt_list_remove(&(thread->tlist));
  154. /* release thread's stack */
  155. RT_KERNEL_FREE(thread->stack_addr);
  156. /* delete thread object */
  157. rt_object_delete((rt_object_t)thread);
  158. rt_hw_interrupt_enable(lock);
  159. }
  160. #endif
  161. }
  162. extern void rt_system_power_manager(void);
  163. static void rt_thread_idle_entry(void *parameter)
  164. {
  165. #ifdef RT_USING_SMP
  166. if (rt_hw_cpu_id() != 0)
  167. {
  168. while (1)
  169. {
  170. rt_hw_secondary_cpu_idle_exec();
  171. }
  172. }
  173. #endif
  174. while (1)
  175. {
  176. #ifdef RT_USING_IDLE_HOOK
  177. rt_size_t i;
  178. void (*idle_hook)(void);
  179. for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  180. {
  181. idle_hook = idle_hook_list[i];
  182. if (idle_hook != RT_NULL)
  183. {
  184. idle_hook();
  185. }
  186. }
  187. #endif
  188. rt_thread_idle_excute();
  189. #ifdef RT_USING_PM
  190. rt_system_power_manager();
  191. #endif
  192. }
  193. }
  194. /**
  195. * @ingroup SystemInit
  196. *
  197. * This function will initialize idle thread, then start it.
  198. *
  199. * @note this function must be invoked when system init.
  200. */
  201. void rt_thread_idle_init(void)
  202. {
  203. rt_ubase_t i;
  204. char tidle_name[RT_NAME_MAX];
  205. for (i = 0; i < _CPUS_NR; i++)
  206. {
  207. rt_sprintf(tidle_name, "tidle%d", i);
  208. rt_thread_init(&idle[i],
  209. tidle_name,
  210. rt_thread_idle_entry,
  211. RT_NULL,
  212. &rt_thread_stack[i][0],
  213. sizeof(rt_thread_stack[i]),
  214. RT_THREAD_PRIORITY_MAX - 1,
  215. 32);
  216. #ifdef RT_USING_SMP
  217. rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
  218. #endif
  219. /* startup */
  220. rt_thread_startup(&idle[i]);
  221. }
  222. }
  223. /**
  224. * @ingroup Thread
  225. *
  226. * This function will get the handler of the idle thread.
  227. *
  228. */
  229. rt_thread_t rt_thread_idle_gethandler(void)
  230. {
  231. #ifdef RT_USING_SMP
  232. register int id = rt_hw_cpu_id();
  233. #else
  234. register int id = 0;
  235. #endif
  236. return (rt_thread_t)(&idle[id]);
  237. }