idle.c 6.3 KB

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