idle.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /* if sub thread list and main thread are null */
  94. if((module->module_thread == RT_NULL) &&
  95. rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) &&
  96. (module->module_info->module_type == RT_Module_Class_APP))
  97. {
  98. /* unload module */
  99. rt_module_unload(module);
  100. }
  101. }
  102. #endif
  103. /* release thread's stack */
  104. rt_free(thread->stack_addr);
  105. /* delete thread object */
  106. rt_object_delete((rt_object_t)thread);
  107. }
  108. #endif
  109. }
  110. static void rt_thread_idle_entry(void* parameter)
  111. {
  112. while (1)
  113. {
  114. #ifdef RT_USING_HOOK
  115. /* if there is an idle thread hook */
  116. if (rt_thread_idle_hook != RT_NULL) rt_thread_idle_hook();
  117. #endif
  118. rt_thread_idle_excute();
  119. }
  120. }
  121. /**
  122. * @addtogroup SystemInit
  123. */
  124. /*@{*/
  125. /**
  126. * This function will initialize idle thread, then start it.
  127. *
  128. * @note this function must be invoked when system init.
  129. */
  130. void rt_thread_idle_init()
  131. {
  132. /* init thread */
  133. rt_thread_init(&idle,
  134. "tidle",
  135. rt_thread_idle_entry, RT_NULL,
  136. &rt_thread_stack[0], sizeof(rt_thread_stack),
  137. RT_THREAD_PRIORITY_MAX - 1, 32);
  138. /* startup */
  139. rt_thread_startup(&idle);
  140. }
  141. /*@}*/