idle.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "kservice.h"
  17. #if defined (RT_USING_HOOK) || defined(RT_USING_HEAP)
  18. #define IDLE_THREAD_STACK_SIZE 256
  19. #else
  20. #define IDLE_THREAD_STACK_SIZE 128
  21. #endif
  22. static struct rt_thread idle;
  23. ALIGN(RT_ALIGN_SIZE)
  24. static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
  25. #ifdef RT_USING_HEAP
  26. extern rt_list_t rt_thread_defunct;
  27. #endif
  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. * This function will do some things when system idle.
  49. */
  50. void rt_thread_idle_excute(void)
  51. {
  52. #ifdef RT_USING_HEAP
  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;
  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. /* get thread's parent module */
  69. #ifdef RT_USING_MODULE
  70. module = thread->module_parent;
  71. #endif
  72. /* remove defunct thread */
  73. rt_list_remove(&(thread->tlist));
  74. }
  75. else
  76. {
  77. /* enable interrupt */
  78. rt_hw_interrupt_enable(lock);
  79. /* may the defunct thread list is removed by others, just return */
  80. return;
  81. }
  82. /* enable interrupt */
  83. rt_hw_interrupt_enable(lock);
  84. #ifdef RT_USING_MODULE
  85. if(module != RT_NULL)
  86. {
  87. /* if the thread is module's main thread */
  88. if(module->module_thread == thread)
  89. {
  90. /* detach module's main thread */
  91. module->module_thread = RT_NULL;
  92. }
  93. }
  94. #endif
  95. /* release thread's stack */
  96. rt_free(thread->stack_addr);
  97. /* delete thread object */
  98. rt_object_delete((rt_object_t)thread);
  99. #ifdef RT_USING_MODULE
  100. if(module != RT_NULL)
  101. {
  102. /* if sub thread list and main thread are all empty */
  103. if((module->module_thread == RT_NULL) &&
  104. rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) )
  105. {
  106. /* unload module */
  107. rt_module_unload(module);
  108. }
  109. }
  110. }
  111. #endif
  112. #endif
  113. }
  114. static void rt_thread_idle_entry(void* parameter)
  115. {
  116. while (1)
  117. {
  118. #ifdef RT_USING_HOOK
  119. /* if there is an idle thread hook */
  120. if (rt_thread_idle_hook != RT_NULL) rt_thread_idle_hook();
  121. #endif
  122. rt_thread_idle_excute();
  123. }
  124. }
  125. /**
  126. * @addtogroup SystemInit
  127. */
  128. /*@{*/
  129. /**
  130. * This function will initialize idle thread, then start it.
  131. *
  132. * @note this function must be invoked when system init.
  133. */
  134. void rt_thread_idle_init()
  135. {
  136. /* init thread */
  137. rt_thread_init(&idle,
  138. "tidle",
  139. rt_thread_idle_entry, RT_NULL,
  140. &rt_thread_stack[0], sizeof(rt_thread_stack),
  141. RT_THREAD_PRIORITY_MAX - 1, 32);
  142. /* startup */
  143. rt_thread_startup(&idle);
  144. }
  145. /*@}*/