idle.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. */
  29. #include <rthw.h>
  30. #include <rtthread.h>
  31. #if defined (RT_USING_HOOK)
  32. #ifndef RT_USING_IDLE_HOOK
  33. #define RT_USING_IDLE_HOOK
  34. #endif
  35. #endif
  36. #ifndef IDLE_THREAD_STACK_SIZE
  37. #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
  38. #define IDLE_THREAD_STACK_SIZE 256
  39. #else
  40. #define IDLE_THREAD_STACK_SIZE 128
  41. #endif
  42. #endif
  43. static struct rt_thread idle;
  44. ALIGN(RT_ALIGN_SIZE)
  45. static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
  46. extern rt_list_t rt_thread_defunct;
  47. #ifdef RT_USING_IDLE_HOOK
  48. static void (*rt_thread_idle_hook)();
  49. /**
  50. * @ingroup Hook
  51. * This function sets a hook function to idle thread loop. When the system performs
  52. * idle loop, this hook function should be invoked.
  53. *
  54. * @param hook the specified hook function
  55. *
  56. * @note the hook function must be simple and never be blocked or suspend.
  57. */
  58. void rt_thread_idle_sethook(void (*hook)(void))
  59. {
  60. rt_thread_idle_hook = hook;
  61. }
  62. #endif
  63. /* Return whether there is defunctional thread to be deleted. */
  64. rt_inline int _has_defunct_thread(void)
  65. {
  66. /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
  67. * So the compiler has a good reason that the rt_thread_defunct list does
  68. * not change within rt_thread_idle_excute thus optimize the "while" loop
  69. * into a "if".
  70. *
  71. * So add the volatile qualifier here. */
  72. const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;
  73. return l->next != l;
  74. }
  75. /**
  76. * @ingroup Thread
  77. *
  78. * This function will perform system background job when system idle.
  79. */
  80. void rt_thread_idle_excute(void)
  81. {
  82. /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
  83. * will do all the cleanups. */
  84. while (_has_defunct_thread())
  85. {
  86. rt_base_t lock;
  87. rt_thread_t thread;
  88. #ifdef RT_USING_MODULE
  89. rt_module_t module = RT_NULL;
  90. #endif
  91. RT_DEBUG_NOT_IN_INTERRUPT;
  92. /* disable interrupt */
  93. lock = rt_hw_interrupt_disable();
  94. /* re-check whether list is empty */
  95. if (_has_defunct_thread())
  96. {
  97. /* get defunct thread */
  98. thread = rt_list_entry(rt_thread_defunct.next,
  99. struct rt_thread,
  100. tlist);
  101. #ifdef RT_USING_MODULE
  102. /* get thread's parent module */
  103. module = (rt_module_t)thread->module_id;
  104. /* if the thread is module's main thread */
  105. if (module != RT_NULL && module->module_thread == thread)
  106. {
  107. /* detach module's main thread */
  108. module->module_thread = RT_NULL;
  109. }
  110. #endif
  111. /* remove defunct thread */
  112. rt_list_remove(&(thread->tlist));
  113. /* invoke thread cleanup */
  114. if (thread->cleanup != RT_NULL)
  115. thread->cleanup(thread);
  116. #ifdef RT_USING_SIGNALS
  117. rt_thread_free_sig(thread);
  118. #endif
  119. /* if it's a system object, not delete it */
  120. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  121. {
  122. /* enable interrupt */
  123. rt_hw_interrupt_enable(lock);
  124. return;
  125. }
  126. }
  127. else
  128. {
  129. /* enable interrupt */
  130. rt_hw_interrupt_enable(lock);
  131. /* may the defunct thread list is removed by others, just return */
  132. return;
  133. }
  134. /* enable interrupt */
  135. rt_hw_interrupt_enable(lock);
  136. #ifdef RT_USING_HEAP
  137. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  138. /* the thread belongs to an application module */
  139. if (thread->flags & RT_OBJECT_FLAG_MODULE)
  140. rt_module_free((rt_module_t)thread->module_id, thread->stack_addr);
  141. else
  142. #endif
  143. /* release thread's stack */
  144. RT_KERNEL_FREE(thread->stack_addr);
  145. /* delete thread object */
  146. rt_object_delete((rt_object_t)thread);
  147. #endif
  148. #ifdef RT_USING_MODULE
  149. if (module != RT_NULL)
  150. {
  151. extern rt_err_t rt_module_destroy(rt_module_t module);
  152. /* if sub thread list and main thread are all empty */
  153. if ((module->module_thread == RT_NULL) &&
  154. rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list))
  155. {
  156. module->nref --;
  157. }
  158. /* destroy module */
  159. if (module->nref == 0)
  160. rt_module_destroy(module);
  161. }
  162. #endif
  163. }
  164. }
  165. static void rt_thread_idle_entry(void *parameter)
  166. {
  167. while (1)
  168. {
  169. #ifdef RT_USING_IDLE_HOOK
  170. if (rt_thread_idle_hook != RT_NULL)
  171. {
  172. rt_thread_idle_hook();
  173. }
  174. #endif
  175. rt_thread_idle_excute();
  176. }
  177. }
  178. /**
  179. * @ingroup SystemInit
  180. *
  181. * This function will initialize idle thread, then start it.
  182. *
  183. * @note this function must be invoked when system init.
  184. */
  185. void rt_thread_idle_init(void)
  186. {
  187. /* initialize thread */
  188. rt_thread_init(&idle,
  189. "tidle",
  190. rt_thread_idle_entry,
  191. RT_NULL,
  192. &rt_thread_stack[0],
  193. sizeof(rt_thread_stack),
  194. RT_THREAD_PRIORITY_MAX - 1,
  195. 32);
  196. /* startup */
  197. rt_thread_startup(&idle);
  198. }
  199. /**
  200. * @ingroup Thread
  201. *
  202. * This function will get the handler of the idle thread.
  203. *
  204. */
  205. rt_thread_t rt_thread_idle_gethandler(void)
  206. {
  207. return (rt_thread_t)(&idle);
  208. }