idle.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * File : idle.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-23 Bernard the first version
  23. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  24. * 2012-12-29 Bernard fix compiling warning.
  25. * 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no
  26. * dead thread.
  27. * 2016-08-09 ArdaFu add method to get the handler of the idle thread.
  28. * 2018-02-07 Bernard lock scheduler to protect tid->cleanup.
  29. */
  30. #include <rthw.h>
  31. #include <rtthread.h>
  32. #if defined (RT_USING_HOOK)
  33. #ifndef RT_USING_IDLE_HOOK
  34. #define RT_USING_IDLE_HOOK
  35. #endif
  36. #endif
  37. #ifndef IDLE_THREAD_STACK_SIZE
  38. #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
  39. #define IDLE_THREAD_STACK_SIZE 256
  40. #else
  41. #define IDLE_THREAD_STACK_SIZE 128
  42. #endif
  43. #endif
  44. static struct rt_thread idle;
  45. ALIGN(RT_ALIGN_SIZE)
  46. static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
  47. extern rt_list_t rt_thread_defunct;
  48. #ifdef RT_USING_IDLE_HOOK
  49. static void (*rt_thread_idle_hook)();
  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. * @note the hook function must be simple and never be blocked or suspend.
  58. */
  59. void rt_thread_idle_sethook(void (*hook)(void))
  60. {
  61. rt_thread_idle_hook = hook;
  62. }
  63. #endif
  64. /* Return whether there is defunctional thread to be deleted. */
  65. rt_inline int _has_defunct_thread(void)
  66. {
  67. /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
  68. * So the compiler has a good reason that the rt_thread_defunct list does
  69. * not change within rt_thread_idle_excute thus optimize the "while" loop
  70. * into a "if".
  71. *
  72. * So add the volatile qualifier here. */
  73. const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;
  74. return l->next != l;
  75. }
  76. /**
  77. * @ingroup Thread
  78. *
  79. * This function will perform system background job when system idle.
  80. */
  81. void rt_thread_idle_excute(void)
  82. {
  83. /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
  84. * will do all the cleanups. */
  85. while (_has_defunct_thread())
  86. {
  87. rt_base_t lock;
  88. rt_thread_t thread;
  89. #ifdef RT_USING_MODULE
  90. rt_module_t module = RT_NULL;
  91. #endif
  92. RT_DEBUG_NOT_IN_INTERRUPT;
  93. /* disable interrupt */
  94. lock = rt_hw_interrupt_disable();
  95. /* re-check whether list is empty */
  96. if (_has_defunct_thread())
  97. {
  98. /* get defunct thread */
  99. thread = rt_list_entry(rt_thread_defunct.next,
  100. struct rt_thread,
  101. tlist);
  102. #ifdef RT_USING_MODULE
  103. /* get thread's parent module */
  104. module = (rt_module_t)thread->module_id;
  105. /* if the thread is module's main thread */
  106. if (module != RT_NULL && module->module_thread == thread)
  107. {
  108. /* detach module's main thread */
  109. module->module_thread = RT_NULL;
  110. }
  111. #endif
  112. /* remove defunct thread */
  113. rt_list_remove(&(thread->tlist));
  114. /* lock scheduler to prevent scheduling in cleanup function. */
  115. rt_enter_critical();
  116. /* invoke thread cleanup */
  117. if (thread->cleanup != RT_NULL)
  118. thread->cleanup(thread);
  119. #ifdef RT_USING_SIGNALS
  120. rt_thread_free_sig(thread);
  121. #endif
  122. /* if it's a system object, not delete it */
  123. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  124. {
  125. /* unlock scheduler */
  126. rt_exit_critical();
  127. /* enable interrupt */
  128. rt_hw_interrupt_enable(lock);
  129. return;
  130. }
  131. /* unlock scheduler */
  132. rt_exit_critical();
  133. }
  134. else
  135. {
  136. /* enable interrupt */
  137. rt_hw_interrupt_enable(lock);
  138. /* may the defunct thread list is removed by others, just return */
  139. return;
  140. }
  141. /* enable interrupt */
  142. rt_hw_interrupt_enable(lock);
  143. #ifdef RT_USING_HEAP
  144. /* release thread's stack */
  145. RT_KERNEL_FREE(thread->stack_addr);
  146. /* delete thread object */
  147. rt_object_delete((rt_object_t)thread);
  148. #endif
  149. }
  150. }
  151. static void rt_thread_idle_entry(void *parameter)
  152. {
  153. while (1)
  154. {
  155. #ifdef RT_USING_IDLE_HOOK
  156. if (rt_thread_idle_hook != RT_NULL)
  157. {
  158. rt_thread_idle_hook();
  159. }
  160. #endif
  161. rt_thread_idle_excute();
  162. }
  163. }
  164. /**
  165. * @ingroup SystemInit
  166. *
  167. * This function will initialize idle thread, then start it.
  168. *
  169. * @note this function must be invoked when system init.
  170. */
  171. void rt_thread_idle_init(void)
  172. {
  173. /* initialize thread */
  174. rt_thread_init(&idle,
  175. "tidle",
  176. rt_thread_idle_entry,
  177. RT_NULL,
  178. &rt_thread_stack[0],
  179. sizeof(rt_thread_stack),
  180. RT_THREAD_PRIORITY_MAX - 1,
  181. 32);
  182. /* startup */
  183. rt_thread_startup(&idle);
  184. }
  185. /**
  186. * @ingroup Thread
  187. *
  188. * This function will get the handler of the idle thread.
  189. *
  190. */
  191. rt_thread_t rt_thread_idle_gethandler(void)
  192. {
  193. return (rt_thread_t)(&idle);
  194. }