idle.c 5.4 KB

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