idle.c 9.1 KB

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