idle.c 6.6 KB

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