idle.c 4.0 KB

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