idle.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-23 Bernard the first version
  9. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  10. * 2012-12-29 Bernard fix compiling warning.
  11. * 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no
  12. * dead thread.
  13. * 2016-08-09 ArdaFu add method to get the handler of the idle thread.
  14. * 2018-02-07 Bernard lock scheduler to protect tid->cleanup.
  15. * 2018-07-14 armink add idle hook list
  16. * 2018-11-22 Jesven add per cpu idle task
  17. * combine the code of primary and secondary cpu
  18. */
  19. #include <rthw.h>
  20. #include <rtthread.h>
  21. #ifdef RT_USING_MODULE
  22. #include <dlmodule.h>
  23. #endif /* RT_USING_MODULE */
  24. #ifdef RT_USING_HOOK
  25. #ifndef RT_USING_IDLE_HOOK
  26. #define RT_USING_IDLE_HOOK
  27. #endif /* RT_USING_IDLE_HOOK */
  28. #endif /* RT_USING_HOOK */
  29. #ifndef IDLE_THREAD_STACK_SIZE
  30. #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
  31. #define IDLE_THREAD_STACK_SIZE 256
  32. #else
  33. #define IDLE_THREAD_STACK_SIZE 128
  34. #endif /* (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP) */
  35. #endif /* IDLE_THREAD_STACK_SIZE */
  36. #ifdef RT_USING_SMP
  37. #define _CPUS_NR RT_CPUS_NR
  38. #else
  39. #define _CPUS_NR 1
  40. #endif /* RT_USING_SMP */
  41. static rt_list_t _rt_thread_defunct = RT_LIST_OBJECT_INIT(_rt_thread_defunct);;
  42. static struct rt_thread idle[_CPUS_NR];
  43. ALIGN(RT_ALIGN_SIZE)
  44. static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];
  45. #ifdef RT_USING_SMP
  46. #ifndef SYSTEM_THREAD_STACK_SIZE
  47. #define SYSTEM_THREAD_STACK_SIZE IDLE_THREAD_STACK_SIZE
  48. #endif
  49. static struct rt_thread rt_system_thread;
  50. ALIGN(RT_ALIGN_SIZE)
  51. static rt_uint8_t rt_system_stack[SYSTEM_THREAD_STACK_SIZE];
  52. static struct rt_semaphore system_sem;
  53. #endif
  54. #ifdef RT_USING_IDLE_HOOK
  55. #ifndef RT_IDLE_HOOK_LIST_SIZE
  56. #define RT_IDLE_HOOK_LIST_SIZE 4
  57. #endif /* RT_IDLE_HOOK_LIST_SIZE */
  58. static void (*idle_hook_list[RT_IDLE_HOOK_LIST_SIZE])(void);
  59. /**
  60. * @brief This function sets a hook function to idle thread loop. When the system performs
  61. * idle loop, this hook function should be invoked.
  62. *
  63. * @param hook the specified hook function.
  64. *
  65. * @return RT_EOK: set OK.
  66. * -RT_EFULL: hook list is full.
  67. *
  68. * @note the hook function must be simple and never be blocked or suspend.
  69. */
  70. rt_err_t rt_thread_idle_sethook(void (*hook)(void))
  71. {
  72. rt_size_t i;
  73. rt_base_t level;
  74. rt_err_t ret = -RT_EFULL;
  75. /* disable interrupt */
  76. level = rt_hw_interrupt_disable();
  77. for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  78. {
  79. if (idle_hook_list[i] == RT_NULL)
  80. {
  81. idle_hook_list[i] = hook;
  82. ret = RT_EOK;
  83. break;
  84. }
  85. }
  86. /* enable interrupt */
  87. rt_hw_interrupt_enable(level);
  88. return ret;
  89. }
  90. /**
  91. * @brief delete the idle hook on hook list.
  92. *
  93. * @param hook the specified hook function.
  94. *
  95. * @return RT_EOK: delete OK.
  96. * -RT_ENOSYS: hook was not found.
  97. */
  98. rt_err_t rt_thread_idle_delhook(void (*hook)(void))
  99. {
  100. rt_size_t i;
  101. rt_base_t level;
  102. rt_err_t ret = -RT_ENOSYS;
  103. /* disable interrupt */
  104. level = rt_hw_interrupt_disable();
  105. for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  106. {
  107. if (idle_hook_list[i] == hook)
  108. {
  109. idle_hook_list[i] = RT_NULL;
  110. ret = RT_EOK;
  111. break;
  112. }
  113. }
  114. /* enable interrupt */
  115. rt_hw_interrupt_enable(level);
  116. return ret;
  117. }
  118. #endif /* RT_USING_IDLE_HOOK */
  119. #ifdef RT_USING_MODULE
  120. /* Return whether there is defunctional thread to be deleted. */
  121. rt_inline int _idle_has_defunct_thread(void)
  122. {
  123. /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
  124. * So the compiler has a good reason that the _rt_thread_defunct list does
  125. * not change within rt_thread_defunct_exceute thus optimize the "while" loop
  126. * into a "if".
  127. *
  128. * So add the volatile qualifier here. */
  129. const volatile rt_list_t *l = (const volatile rt_list_t *)&_rt_thread_defunct;
  130. return l->next != l;
  131. }
  132. #endif /* RT_USING_MODULE */
  133. /**
  134. * @brief Enqueue a thread to defunct queue.
  135. *
  136. * @note It must be called between rt_hw_interrupt_disable and rt_hw_interrupt_enable
  137. */
  138. void rt_thread_defunct_enqueue(rt_thread_t thread)
  139. {
  140. rt_list_insert_after(&_rt_thread_defunct, &thread->tlist);
  141. #ifdef RT_USING_SMP
  142. rt_sem_release(&system_sem);
  143. #endif
  144. }
  145. /**
  146. * @brief Dequeue a thread from defunct queue.
  147. *
  148. * @note It must be called between rt_hw_interrupt_disable and rt_hw_interrupt_enable.
  149. */
  150. rt_thread_t rt_thread_defunct_dequeue(void)
  151. {
  152. rt_thread_t thread = RT_NULL;
  153. rt_list_t *l = &_rt_thread_defunct;
  154. if (l->next != l)
  155. {
  156. thread = rt_list_entry(l->next,
  157. struct rt_thread,
  158. tlist);
  159. rt_list_remove(&(thread->tlist));
  160. }
  161. return thread;
  162. }
  163. /**
  164. * @brief This function will perform system background job when system idle.
  165. */
  166. static void rt_defunct_execute(void)
  167. {
  168. /* Loop until there is no dead thread. So one call to rt_defunct_execute
  169. * will do all the cleanups. */
  170. while (1)
  171. {
  172. rt_base_t lock;
  173. rt_thread_t thread;
  174. void (*cleanup)(struct rt_thread *tid);
  175. #ifdef RT_USING_MODULE
  176. struct rt_dlmodule *module = RT_NULL;
  177. #endif
  178. RT_DEBUG_NOT_IN_INTERRUPT;
  179. /* disable interrupt */
  180. lock = rt_hw_interrupt_disable();
  181. #ifdef RT_USING_MODULE
  182. /* check whether list is empty */
  183. if (!_idle_has_defunct_thread())
  184. {
  185. rt_hw_interrupt_enable(lock);
  186. break;
  187. }
  188. /* get defunct thread */
  189. thread = rt_list_entry(_rt_thread_defunct.next,
  190. struct rt_thread,
  191. tlist);
  192. module = (struct rt_dlmodule*)thread->module_id;
  193. if (module)
  194. {
  195. dlmodule_destroy(module);
  196. }
  197. /* remove defunct thread */
  198. rt_list_remove(&(thread->tlist));
  199. #else
  200. thread = rt_thread_defunct_dequeue();
  201. if (!thread)
  202. {
  203. rt_hw_interrupt_enable(lock);
  204. break;
  205. }
  206. #endif
  207. /* invoke thread cleanup */
  208. cleanup = thread->cleanup;
  209. if (cleanup != RT_NULL)
  210. {
  211. rt_hw_interrupt_enable(lock);
  212. cleanup(thread);
  213. lock = rt_hw_interrupt_disable();
  214. }
  215. #ifdef RT_USING_SIGNALS
  216. rt_thread_free_sig(thread);
  217. #endif
  218. /* if it's a system object, not delete it */
  219. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  220. {
  221. /* detach this object */
  222. rt_object_detach((rt_object_t)thread);
  223. /* enable interrupt */
  224. rt_hw_interrupt_enable(lock);
  225. }
  226. else
  227. {
  228. rt_hw_interrupt_enable(lock);
  229. #ifdef RT_USING_HEAP
  230. /* release thread's stack */
  231. RT_KERNEL_FREE(thread->stack_addr);
  232. /* delete thread object */
  233. rt_object_delete((rt_object_t)thread);
  234. #endif
  235. }
  236. }
  237. }
  238. extern void rt_system_power_manager(void);
  239. static void rt_thread_idle_entry(void *parameter)
  240. {
  241. #ifdef RT_USING_SMP
  242. if (rt_hw_cpu_id() != 0)
  243. {
  244. while (1)
  245. {
  246. rt_hw_secondary_cpu_idle_exec();
  247. }
  248. }
  249. #endif /* RT_USING_SMP */
  250. while (1)
  251. {
  252. #ifdef RT_USING_IDLE_HOOK
  253. rt_size_t i;
  254. void (*idle_hook)(void);
  255. for (i = 0; i < RT_IDLE_HOOK_LIST_SIZE; i++)
  256. {
  257. idle_hook = idle_hook_list[i];
  258. if (idle_hook != RT_NULL)
  259. {
  260. idle_hook();
  261. }
  262. }
  263. #endif /* RT_USING_IDLE_HOOK */
  264. #ifndef RT_USING_SMP
  265. rt_defunct_execute();
  266. #endif /* RT_USING_SMP */
  267. #ifdef RT_USING_PM
  268. rt_system_power_manager();
  269. #endif /* RT_USING_PM */
  270. }
  271. }
  272. #ifdef RT_USING_SMP
  273. static void rt_thread_system_entry(void *parameter)
  274. {
  275. while (1)
  276. {
  277. rt_sem_take(&system_sem, RT_WAITING_FOREVER);
  278. rt_defunct_execute();
  279. }
  280. }
  281. #endif
  282. /**
  283. * @brief This function will initialize idle thread, then start it.
  284. *
  285. * @note this function must be invoked when system init.
  286. */
  287. void rt_thread_idle_init(void)
  288. {
  289. rt_ubase_t i;
  290. char tidle_name[RT_NAME_MAX];
  291. for (i = 0; i < _CPUS_NR; i++)
  292. {
  293. rt_sprintf(tidle_name, "tidle%d", i);
  294. rt_thread_init(&idle[i],
  295. tidle_name,
  296. rt_thread_idle_entry,
  297. RT_NULL,
  298. &rt_thread_stack[i][0],
  299. sizeof(rt_thread_stack[i]),
  300. RT_THREAD_PRIORITY_MAX - 1,
  301. 32);
  302. #ifdef RT_USING_SMP
  303. rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
  304. #endif /* RT_USING_SMP */
  305. /* startup */
  306. rt_thread_startup(&idle[i]);
  307. }
  308. #ifdef RT_USING_SMP
  309. RT_ASSERT(RT_THREAD_PRIORITY_MAX > 2);
  310. rt_sem_init(&system_sem, "defunct", 1, RT_IPC_FLAG_FIFO);
  311. /* create defunct thread */
  312. rt_thread_init(&rt_system_thread,
  313. "tsystem",
  314. rt_thread_system_entry,
  315. RT_NULL,
  316. rt_system_stack,
  317. sizeof(rt_system_stack),
  318. RT_THREAD_PRIORITY_MAX - 2,
  319. 32);
  320. /* startup */
  321. rt_thread_startup(&rt_system_thread);
  322. #endif
  323. }
  324. /**
  325. * @brief This function will get the handler of the idle thread.
  326. */
  327. rt_thread_t rt_thread_idle_gethandler(void)
  328. {
  329. #ifdef RT_USING_SMP
  330. register int id = rt_hw_cpu_id();
  331. #else
  332. register int id = 0;
  333. #endif /* RT_USING_SMP */
  334. return (rt_thread_t)(&idle[id]);
  335. }