idle.c 6.6 KB

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