idle.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /* 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. /* disable interrupt */
  133. RT_DEBUG_NOT_IN_INTERRUPT;
  134. #ifdef RT_USING_HEAP
  135. while (1)
  136. {
  137. rt_base_t lock;
  138. rt_thread_t thread;
  139. lock = rt_hw_interrupt_disable();
  140. /* check whether list is empty */
  141. if (!_has_defunct_thread())
  142. {
  143. rt_hw_interrupt_enable(lock);
  144. break;
  145. }
  146. /* get defunct thread */
  147. thread = rt_list_entry(rt_thread_defunct.next,
  148. struct rt_thread,
  149. tlist);
  150. /* remove defunct thread */
  151. rt_list_remove(&(thread->tlist));
  152. /* release thread's stack */
  153. RT_KERNEL_FREE(thread->stack_addr);
  154. /* delete thread object */
  155. rt_object_delete((rt_object_t)thread);
  156. rt_hw_interrupt_enable(lock);
  157. }
  158. #endif
  159. }
  160. extern void rt_system_power_manager(void);
  161. static void rt_thread_idle_entry(void *parameter)
  162. {
  163. #ifdef RT_USING_SMP
  164. if (rt_hw_cpu_id() != 0)
  165. {
  166. while (1)
  167. {
  168. rt_hw_secondary_cpu_idle_exec();
  169. }
  170. }
  171. #endif
  172. while (1)
  173. {
  174. #ifdef RT_USING_IDLE_HOOK
  175. rt_size_t i;
  176. for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  177. {
  178. if (idle_hook_list[i] != RT_NULL)
  179. {
  180. idle_hook_list[i]();
  181. }
  182. }
  183. #endif
  184. rt_thread_idle_excute();
  185. #ifdef RT_USING_PM
  186. rt_system_power_manager();
  187. #endif
  188. }
  189. }
  190. /**
  191. * @ingroup SystemInit
  192. *
  193. * This function will initialize idle thread, then start it.
  194. *
  195. * @note this function must be invoked when system init.
  196. */
  197. void rt_thread_idle_init(void)
  198. {
  199. rt_ubase_t i;
  200. char tidle_name[RT_NAME_MAX];
  201. for (i = 0; i < _CPUS_NR; i++)
  202. {
  203. rt_sprintf(tidle_name, "tidle%d", i);
  204. rt_thread_init(&idle[i],
  205. tidle_name,
  206. rt_thread_idle_entry,
  207. RT_NULL,
  208. &rt_thread_stack[i][0],
  209. sizeof(rt_thread_stack[i]),
  210. RT_THREAD_PRIORITY_MAX - 1,
  211. 32);
  212. #ifdef RT_USING_SMP
  213. rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
  214. #endif
  215. /* startup */
  216. rt_thread_startup(&idle[i]);
  217. }
  218. }
  219. /**
  220. * @ingroup Thread
  221. *
  222. * This function will get the handler of the idle thread.
  223. *
  224. */
  225. rt_thread_t rt_thread_idle_gethandler(void)
  226. {
  227. #ifdef RT_USING_SMP
  228. register int id = rt_hw_cpu_id();
  229. #else
  230. register int id = 0;
  231. #endif
  232. return (rt_thread_t)(&idle[id]);
  233. }