idle.c 3.9 KB

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