idle.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifdef RT_USING_MODULE
  60. rt_module_t module;
  61. #endif
  62. struct rt_thread* thread = rt_list_entry(rt_thread_defunct.next, struct rt_thread, tlist);
  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. #ifdef RT_USING_MODULE
  71. module = thread->module_parent;
  72. /* delete thread object */
  73. rt_object_delete((rt_object_t)thread);
  74. if(module != RT_NULL)
  75. {
  76. /* if the thread is module's main thread */
  77. if(module->module_thread == thread)
  78. {
  79. /* detach module's main thread */
  80. module->module_thread = RT_NULL;
  81. }
  82. /* if sub thread list and main thread are null */
  83. if((module->module_thread == RT_NULL) &&
  84. rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) &&
  85. (module->parent.flag & RT_MODULE_FLAG_AUTO_CLEAN))
  86. {
  87. /* unload module */
  88. rt_module_unload(module);
  89. }
  90. }
  91. #endif
  92. }
  93. #endif
  94. }
  95. }
  96. /**
  97. * @addtogroup SystemInit
  98. */
  99. /*@{*/
  100. /**
  101. * This function will initialize idle thread, then start it.
  102. *
  103. * @note this function must be invoked when system init.
  104. */
  105. void rt_thread_idle_init()
  106. {
  107. /* init thread */
  108. rt_thread_init(&idle,
  109. "tidle",
  110. rt_thread_idle_entry, RT_NULL,
  111. &rt_thread_stack[0], sizeof(rt_thread_stack),
  112. RT_THREAD_PRIORITY_MAX - 1, 32);
  113. /* startup */
  114. rt_thread_startup(&idle);
  115. }
  116. /*@}*/