idle.c 9.1 KB

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