idle.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-23 Bernard the first version
  13. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  14. * 2012-12-29 Bernard fix compiling warning.
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #ifndef IDLE_THREAD_STACK_SIZE
  19. #if defined (RT_USING_HOOK) || defined(RT_USING_HEAP)
  20. #define IDLE_THREAD_STACK_SIZE 256
  21. #else
  22. #define IDLE_THREAD_STACK_SIZE 128
  23. #endif
  24. #endif
  25. static struct rt_thread idle;
  26. ALIGN(RT_ALIGN_SIZE)
  27. static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
  28. extern rt_list_t rt_thread_defunct;
  29. #ifdef RT_USING_HOOK
  30. /**
  31. * @addtogroup Hook
  32. */
  33. /*@{*/
  34. static void (*rt_thread_idle_hook)();
  35. /**
  36. * This function will set a hook function to idle thread loop.
  37. *
  38. * @param hook the specified hook function
  39. *
  40. * @note the hook function must be simple and never be blocked or suspend.
  41. */
  42. void rt_thread_idle_sethook(void (*hook)(void))
  43. {
  44. rt_thread_idle_hook = hook;
  45. }
  46. /*@}*/
  47. #endif
  48. /**
  49. * @ingroup Thread
  50. *
  51. * This function will perform system background job when system idle.
  52. */
  53. void rt_thread_idle_excute(void)
  54. {
  55. /* check the defunct thread list */
  56. if (!rt_list_isempty(&rt_thread_defunct))
  57. {
  58. rt_base_t lock;
  59. rt_thread_t thread;
  60. #ifdef RT_USING_MODULE
  61. rt_module_t module = RT_NULL;
  62. #endif
  63. RT_DEBUG_NOT_IN_INTERRUPT;
  64. /* disable interrupt */
  65. lock = rt_hw_interrupt_disable();
  66. /* re-check whether list is empty */
  67. if (!rt_list_isempty(&rt_thread_defunct))
  68. {
  69. /* get defunct thread */
  70. thread = rt_list_entry(rt_thread_defunct.next,
  71. struct rt_thread,
  72. tlist);
  73. #ifdef RT_USING_MODULE
  74. /* get thread's parent module */
  75. module = (rt_module_t)thread->module_id;
  76. /* if the thread is module's main thread */
  77. if (module != RT_NULL && module->module_thread == thread)
  78. {
  79. /* detach module's main thread */
  80. module->module_thread = RT_NULL;
  81. }
  82. #endif
  83. /* remove defunct thread */
  84. rt_list_remove(&(thread->tlist));
  85. /* invoke thread cleanup */
  86. if (thread->cleanup != RT_NULL)
  87. thread->cleanup(thread);
  88. /* if it's a system object, not delete it */
  89. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  90. {
  91. /* enable interrupt */
  92. rt_hw_interrupt_enable(lock);
  93. return;
  94. }
  95. }
  96. else
  97. {
  98. /* enable interrupt */
  99. rt_hw_interrupt_enable(lock);
  100. /* may the defunct thread list is removed by others, just return */
  101. return;
  102. }
  103. /* enable interrupt */
  104. rt_hw_interrupt_enable(lock);
  105. #ifdef RT_USING_HEAP
  106. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  107. /* the thread belongs to an application module */
  108. if (thread->flags & RT_OBJECT_FLAG_MODULE)
  109. rt_module_free((rt_module_t)thread->module_id, thread->stack_addr);
  110. else
  111. #endif
  112. /* release thread's stack */
  113. RT_KERNEL_FREE(thread->stack_addr);
  114. /* delete thread object */
  115. rt_object_delete((rt_object_t)thread);
  116. #endif
  117. #ifdef RT_USING_MODULE
  118. if (module != RT_NULL)
  119. {
  120. extern rt_err_t rt_module_destroy(rt_module_t module);
  121. /* if sub thread list and main thread are all empty */
  122. if ((module->module_thread == RT_NULL) &&
  123. rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list))
  124. {
  125. module->nref --;
  126. }
  127. /* destroy module */
  128. if (module->nref == 0)
  129. rt_module_destroy(module);
  130. }
  131. #endif
  132. }
  133. }
  134. static void rt_thread_idle_entry(void *parameter)
  135. {
  136. while (1)
  137. {
  138. #ifdef RT_USING_HOOK
  139. if (rt_thread_idle_hook != RT_NULL)
  140. rt_thread_idle_hook();
  141. #endif
  142. rt_thread_idle_excute();
  143. }
  144. }
  145. /**
  146. * @ingroup SymstemInit
  147. *
  148. * This function will initialize idle thread, then start it.
  149. *
  150. * @note this function must be invoked when system init.
  151. */
  152. void rt_thread_idle_init(void)
  153. {
  154. /* initialize thread */
  155. rt_thread_init(&idle,
  156. "tidle",
  157. rt_thread_idle_entry,
  158. RT_NULL,
  159. &rt_thread_stack[0],
  160. sizeof(rt_thread_stack),
  161. RT_THREAD_PRIORITY_MAX - 1,
  162. 32);
  163. /* startup */
  164. rt_thread_startup(&idle);
  165. }