idle.c 6.2 KB

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