Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /* detach this object */
  163. rt_object_detach((rt_object_t)thread);
  164. /* unlock scheduler */
  165. rt_exit_critical();
  166. /* enable interrupt */
  167. rt_hw_interrupt_enable(lock);
  168. return;
  169. }
  170. /* unlock scheduler */
  171. rt_exit_critical();
  172. }
  173. else
  174. {
  175. /* enable interrupt */
  176. rt_hw_interrupt_enable(lock);
  177. /* may the defunct thread list is removed by others, just return */
  178. return;
  179. }
  180. /* enable interrupt */
  181. rt_hw_interrupt_enable(lock);
  182. #ifdef RT_USING_HEAP
  183. /* release thread's stack */
  184. RT_KERNEL_FREE(thread->stack_addr);
  185. /* delete thread object */
  186. rt_object_delete((rt_object_t)thread);
  187. #endif
  188. }
  189. }
  190. extern void rt_system_power_manager(void);
  191. static void rt_thread_idle_entry(void *parameter)
  192. {
  193. while (1)
  194. {
  195. #ifdef RT_USING_IDLE_HOOK
  196. rt_size_t i;
  197. for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
  198. {
  199. if (idle_hook_list[i] != RT_NULL)
  200. {
  201. idle_hook_list[i]();
  202. }
  203. }
  204. #endif
  205. rt_thread_idle_excute();
  206. #ifdef RT_USING_PM
  207. rt_system_power_manager();
  208. #endif
  209. }
  210. }
  211. /**
  212. * @ingroup SystemInit
  213. *
  214. * This function will initialize idle thread, then start it.
  215. *
  216. * @note this function must be invoked when system init.
  217. */
  218. void rt_thread_idle_init(void)
  219. {
  220. /* initialize thread */
  221. rt_thread_init(&idle,
  222. "tidle",
  223. rt_thread_idle_entry,
  224. RT_NULL,
  225. &rt_thread_stack[0],
  226. sizeof(rt_thread_stack),
  227. RT_THREAD_PRIORITY_MAX - 1,
  228. 32);
  229. /* startup */
  230. rt_thread_startup(&idle);
  231. }
  232. /**
  233. * @ingroup Thread
  234. *
  235. * This function will get the handler of the idle thread.
  236. *
  237. */
  238. rt_thread_t rt_thread_idle_gethandler(void)
  239. {
  240. return (rt_thread_t)(&idle);
  241. }