idle.c 5.6 KB

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