idle.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
  24. #ifdef RT_USING_HEAP
  25. extern rt_list_t rt_thread_defunct;
  26. #endif
  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. static void rt_thread_idle_entry(void* parameter)
  47. {
  48. while (1)
  49. {
  50. #ifdef RT_USING_HOOK
  51. /* if there is an idle thread hook */
  52. if (rt_thread_idle_hook != RT_NULL) rt_thread_idle_hook();
  53. #endif
  54. #ifdef RT_USING_HEAP
  55. /* check the defunct thread list */
  56. if (!rt_list_isempty(&rt_thread_defunct))
  57. {
  58. rt_base_t lock;
  59. struct rt_thread* thread = rt_list_entry(rt_thread_defunct.next, struct rt_thread, tlist);
  60. #ifdef RT_USING_MODULE
  61. rt_module_t module = thread->module_parent;
  62. #endif
  63. /* disable interrupt */
  64. lock = rt_hw_interrupt_disable();
  65. rt_list_remove(&(thread->tlist));
  66. /* enable interrupt */
  67. rt_hw_interrupt_enable(lock);
  68. /* release thread's stack */
  69. rt_free(thread->stack_addr);
  70. /* delete thread object */
  71. rt_object_delete((rt_object_t)thread);
  72. #ifdef RT_USING_MODULE
  73. if(module != RT_NULL)
  74. {
  75. /* if the thread is module's main thread */
  76. if(module->module_thread == thread)
  77. {
  78. /* detach module's main thread */
  79. module->module_thread = RT_NULL;
  80. }
  81. /* if sub thread list and main thread are null */
  82. if((module->module_thread == RT_NULL) &&
  83. rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) &&
  84. (module->parent.flag & RT_MODULE_FLAG_AUTO_CLEAN))
  85. {
  86. /* unload module */
  87. rt_module_unload(module);
  88. }
  89. }
  90. #endif
  91. }
  92. #endif
  93. }
  94. }
  95. /**
  96. * @addtogroup SystemInit
  97. */
  98. /*@{*/
  99. /**
  100. * This function will initialize idle thread, then start it.
  101. *
  102. * @note this function must be invoked when system init.
  103. */
  104. void rt_thread_idle_init()
  105. {
  106. /* init thread */
  107. rt_thread_init(&idle,
  108. "tidle",
  109. rt_thread_idle_entry, RT_NULL,
  110. &rt_thread_stack[0], sizeof(rt_thread_stack),
  111. RT_THREAD_PRIORITY_MAX - 1, 32);
  112. /* startup */
  113. rt_thread_startup(&idle);
  114. }
  115. /*@}*/