thread.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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-28 Bernard first version
  9. * 2006-04-29 Bernard implement thread timer
  10. * 2006-04-30 Bernard added THREAD_DEBUG
  11. * 2006-05-27 Bernard fixed the rt_thread_yield bug
  12. * 2006-06-03 Bernard fixed the thread timer init bug
  13. * 2006-08-10 Bernard fixed the timer bug in thread_sleep
  14. * 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach
  15. * 2006-09-03 Bernard implement rt_thread_detach
  16. * 2008-02-16 Bernard fixed the rt_thread_timeout bug
  17. * 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to
  18. * RT_EOK.
  19. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  20. * 2011-09-01 Bernard fixed rt_thread_exit issue when the current
  21. * thread preempted, which reported by Jiaxing Lee.
  22. * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.
  23. * 2012-12-29 Bernard fixed compiling warning.
  24. * 2016-08-09 ArdaFu add thread suspend and resume hook.
  25. * 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach
  26. * bug when thread has not startup.
  27. * 2018-11-22 Jesven yield is same to rt_schedule
  28. * add support for tasks bound to cpu
  29. * 2021-02-24 Meco Man rearrange rt_thread_control() - schedule the thread when close it
  30. * 2021-11-15 THEWON Remove duplicate work between idle and _thread_exit
  31. * 2021-12-27 Meco Man remove .init_priority
  32. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to thread.c
  33. * 2022-01-24 THEWON let rt_thread_sleep return thread->error when using signal
  34. * 2022-10-15 Bernard add nested mutex feature
  35. */
  36. #include <rthw.h>
  37. #include <rtthread.h>
  38. #include <stddef.h>
  39. #define DBG_TAG "kernel.thread"
  40. #ifdef RT_DEBUG_THREAD
  41. #define DBG_LVL DBG_LOG
  42. #else
  43. #define DBG_LVL DBG_WARNING
  44. #endif /* defined (RT_DEBUG_THREAD) */
  45. #include <rtdbg.h>
  46. #ifndef __on_rt_thread_inited_hook
  47. #define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
  48. #endif
  49. #ifndef __on_rt_thread_suspend_hook
  50. #define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
  51. #endif
  52. #ifndef __on_rt_thread_resume_hook
  53. #define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
  54. #endif
  55. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  56. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  57. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  58. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  59. /**
  60. * @brief This function sets a hook function when the system suspend a thread.
  61. *
  62. * @note The hook function must be simple and never be blocked or suspend.
  63. *
  64. * @param hook is the specified hook function.
  65. */
  66. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  67. {
  68. rt_thread_suspend_hook = hook;
  69. }
  70. /**
  71. * @brief This function sets a hook function when the system resume a thread.
  72. *
  73. * @note The hook function must be simple and never be blocked or suspend.
  74. *
  75. * @param hook is the specified hook function.
  76. */
  77. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  78. {
  79. rt_thread_resume_hook = hook;
  80. }
  81. /**
  82. * @brief This function sets a hook function when a thread is initialized.
  83. *
  84. * @param hook is the specified hook function.
  85. */
  86. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  87. {
  88. rt_thread_inited_hook = hook;
  89. }
  90. #endif /* defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) */
  91. static void _thread_exit(void)
  92. {
  93. struct rt_thread *thread;
  94. rt_base_t level;
  95. /* get current thread */
  96. thread = rt_thread_self();
  97. /* disable interrupt */
  98. level = rt_hw_interrupt_disable();
  99. /* remove from schedule */
  100. rt_schedule_remove_thread(thread);
  101. /* remove it from timer list */
  102. rt_timer_detach(&thread->thread_timer);
  103. /* change stat */
  104. thread->stat = RT_THREAD_CLOSE;
  105. /* insert to defunct thread list */
  106. rt_thread_defunct_enqueue(thread);
  107. /* enable interrupt */
  108. rt_hw_interrupt_enable(level);
  109. /* switch to next task */
  110. rt_schedule();
  111. }
  112. /**
  113. * @brief This function is the timeout function for thread, normally which is invoked
  114. * when thread is timeout to wait some resource.
  115. *
  116. * @param parameter is the parameter of thread timeout function
  117. */
  118. static void _thread_timeout(void *parameter)
  119. {
  120. struct rt_thread *thread;
  121. rt_base_t level;
  122. thread = (struct rt_thread *)parameter;
  123. /* parameter check */
  124. RT_ASSERT(thread != RT_NULL);
  125. RT_ASSERT((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK);
  126. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  127. /* disable interrupt */
  128. level = rt_hw_interrupt_disable();
  129. /* set error number */
  130. thread->error = -RT_ETIMEOUT;
  131. /* remove from suspend list */
  132. rt_list_remove(&(thread->tlist));
  133. /* insert to schedule ready list */
  134. rt_schedule_insert_thread(thread);
  135. /* enable interrupt */
  136. rt_hw_interrupt_enable(level);
  137. /* do schedule */
  138. rt_schedule();
  139. }
  140. static rt_err_t _thread_init(struct rt_thread *thread,
  141. const char *name,
  142. void (*entry)(void *parameter),
  143. void *parameter,
  144. void *stack_start,
  145. rt_uint32_t stack_size,
  146. rt_uint8_t priority,
  147. rt_uint32_t tick)
  148. {
  149. /* init thread list */
  150. rt_list_init(&(thread->tlist));
  151. #ifdef RT_USING_SMART
  152. thread->wakeup.func = RT_NULL;
  153. #endif
  154. thread->entry = (void *)entry;
  155. thread->parameter = parameter;
  156. /* stack init */
  157. thread->stack_addr = stack_start;
  158. thread->stack_size = stack_size;
  159. /* init thread stack */
  160. rt_memset(thread->stack_addr, '#', thread->stack_size);
  161. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  162. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  163. (void *)((char *)thread->stack_addr),
  164. (void *)_thread_exit);
  165. #else
  166. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  167. (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
  168. (void *)_thread_exit);
  169. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  170. /* priority init */
  171. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  172. thread->init_priority = priority;
  173. thread->current_priority = priority;
  174. thread->number_mask = 0;
  175. #ifdef RT_USING_MUTEX
  176. rt_list_init(&thread->taken_object_list);
  177. thread->pending_object = RT_NULL;
  178. #endif
  179. #ifdef RT_USING_EVENT
  180. thread->event_set = 0;
  181. thread->event_info = 0;
  182. #endif /* RT_USING_EVENT */
  183. #if RT_THREAD_PRIORITY_MAX > 32
  184. thread->number = 0;
  185. thread->high_mask = 0;
  186. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  187. /* tick init */
  188. thread->init_tick = tick;
  189. thread->remaining_tick = tick;
  190. /* error and flags */
  191. thread->error = RT_EOK;
  192. thread->stat = RT_THREAD_INIT;
  193. #ifdef RT_USING_SMP
  194. /* not bind on any cpu */
  195. thread->bind_cpu = RT_CPUS_NR;
  196. thread->oncpu = RT_CPU_DETACHED;
  197. /* lock init */
  198. thread->scheduler_lock_nest = 0;
  199. thread->cpus_lock_nest = 0;
  200. thread->critical_lock_nest = 0;
  201. #endif /* RT_USING_SMP */
  202. /* initialize cleanup function and user data */
  203. thread->cleanup = 0;
  204. thread->user_data = 0;
  205. /* initialize thread timer */
  206. rt_timer_init(&(thread->thread_timer),
  207. thread->parent.name,
  208. _thread_timeout,
  209. thread,
  210. 0,
  211. RT_TIMER_FLAG_ONE_SHOT);
  212. /* initialize signal */
  213. #ifdef RT_USING_SIGNALS
  214. thread->sig_mask = 0x00;
  215. thread->sig_pending = 0x00;
  216. #ifndef RT_USING_SMP
  217. thread->sig_ret = RT_NULL;
  218. #endif /* RT_USING_SMP */
  219. thread->sig_vectors = RT_NULL;
  220. thread->si_list = RT_NULL;
  221. #endif /* RT_USING_SIGNALS */
  222. #ifdef RT_USING_SMART
  223. thread->lwp = RT_NULL;
  224. rt_list_init(&(thread->sibling));
  225. rt_memset(&thread->signal, 0, sizeof(lwp_sigset_t));
  226. rt_memset(&thread->signal_mask, 0, sizeof(lwp_sigset_t));
  227. thread->signal_mask_bak = 0;
  228. thread->signal_in_process = 0;
  229. rt_memset(&thread->user_ctx, 0, sizeof thread->user_ctx);
  230. #endif
  231. #ifdef RT_USING_CPU_USAGE
  232. thread->duration_tick = 0;
  233. #endif /* RT_USING_CPU_USAGE */
  234. #ifdef RT_USING_PTHREADS
  235. thread->pthread_data = RT_NULL;
  236. #endif /* RT_USING_PTHREADS */
  237. #ifdef RT_USING_MODULE
  238. thread->parent.module_id = 0;
  239. #endif /* RT_USING_MODULE */
  240. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  241. return RT_EOK;
  242. }
  243. /**
  244. * @addtogroup Thread
  245. */
  246. /**@{*/
  247. /**
  248. * @brief This function will initialize a thread. It's used to initialize a
  249. * static thread object.
  250. *
  251. * @param thread is the static thread object.
  252. *
  253. * @param name is the name of thread, which shall be unique.
  254. *
  255. * @param entry is the entry function of thread.
  256. *
  257. * @param parameter is the parameter of thread enter function.
  258. *
  259. * @param stack_start is the start address of thread stack.
  260. *
  261. * @param stack_size is the size of thread stack.
  262. *
  263. * @param priority is the priority of thread.
  264. *
  265. * @param tick is the time slice if there are same priority thread.
  266. *
  267. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  268. * If the return value is any other values, it means this operation failed.
  269. */
  270. rt_err_t rt_thread_init(struct rt_thread *thread,
  271. const char *name,
  272. void (*entry)(void *parameter),
  273. void *parameter,
  274. void *stack_start,
  275. rt_uint32_t stack_size,
  276. rt_uint8_t priority,
  277. rt_uint32_t tick)
  278. {
  279. /* parameter check */
  280. RT_ASSERT(thread != RT_NULL);
  281. RT_ASSERT(stack_start != RT_NULL);
  282. /* initialize thread object */
  283. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  284. return _thread_init(thread,
  285. name,
  286. entry,
  287. parameter,
  288. stack_start,
  289. stack_size,
  290. priority,
  291. tick);
  292. }
  293. RTM_EXPORT(rt_thread_init);
  294. /**
  295. * @brief This function will return self thread object.
  296. *
  297. * @return The self thread object.
  298. */
  299. rt_thread_t rt_thread_self(void)
  300. {
  301. #ifdef RT_USING_SMP
  302. rt_base_t lock;
  303. rt_thread_t self;
  304. lock = rt_hw_local_irq_disable();
  305. self = rt_cpu_self()->current_thread;
  306. rt_hw_local_irq_enable(lock);
  307. return self;
  308. #else
  309. extern rt_thread_t rt_current_thread;
  310. return rt_current_thread;
  311. #endif /* RT_USING_SMP */
  312. }
  313. RTM_EXPORT(rt_thread_self);
  314. /**
  315. * @brief This function will start a thread and put it to system ready queue.
  316. *
  317. * @param thread is the thread to be started.
  318. *
  319. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  320. * If the return value is any other values, it means this operation failed.
  321. */
  322. rt_err_t rt_thread_startup(rt_thread_t thread)
  323. {
  324. /* parameter check */
  325. RT_ASSERT(thread != RT_NULL);
  326. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  327. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  328. /* calculate priority attribute */
  329. #if RT_THREAD_PRIORITY_MAX > 32
  330. thread->number = thread->current_priority >> 3; /* 5bit */
  331. thread->number_mask = 1L << thread->number;
  332. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  333. #else
  334. thread->number_mask = 1L << thread->current_priority;
  335. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  336. LOG_D("startup a thread:%s with priority:%d",
  337. thread->parent.name, thread->current_priority);
  338. /* change thread stat */
  339. thread->stat = RT_THREAD_SUSPEND;
  340. /* then resume it */
  341. rt_thread_resume(thread);
  342. if (rt_thread_self() != RT_NULL)
  343. {
  344. /* do a scheduling */
  345. rt_schedule();
  346. }
  347. return RT_EOK;
  348. }
  349. RTM_EXPORT(rt_thread_startup);
  350. /**
  351. * @brief This function will detach a thread. The thread object will be removed from
  352. * thread queue and detached/deleted from the system object management.
  353. *
  354. * @param thread is the thread to be deleted.
  355. *
  356. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  357. * If the return value is any other values, it means this operation failed.
  358. */
  359. rt_err_t rt_thread_detach(rt_thread_t thread)
  360. {
  361. rt_base_t level;
  362. /* parameter check */
  363. RT_ASSERT(thread != RT_NULL);
  364. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  365. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  366. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  367. return RT_EOK;
  368. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  369. {
  370. /* remove from schedule */
  371. rt_schedule_remove_thread(thread);
  372. }
  373. /* disable interrupt */
  374. level = rt_hw_interrupt_disable();
  375. /* release thread timer */
  376. rt_timer_detach(&(thread->thread_timer));
  377. /* change stat */
  378. thread->stat = RT_THREAD_CLOSE;
  379. #ifdef RT_USING_MUTEX
  380. if ((thread->pending_object) &&
  381. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  382. {
  383. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  384. rt_mutex_drop_thread(mutex, thread);
  385. thread->pending_object = RT_NULL;
  386. }
  387. #endif
  388. /* insert to defunct thread list */
  389. rt_thread_defunct_enqueue(thread);
  390. /* enable interrupt */
  391. rt_hw_interrupt_enable(level);
  392. return RT_EOK;
  393. }
  394. RTM_EXPORT(rt_thread_detach);
  395. #ifdef RT_USING_HEAP
  396. /**
  397. * @brief This function will create a thread object and allocate thread object memory.
  398. * and stack.
  399. *
  400. * @param name is the name of thread, which shall be unique.
  401. *
  402. * @param entry is the entry function of thread.
  403. *
  404. * @param parameter is the parameter of thread enter function.
  405. *
  406. * @param stack_size is the size of thread stack.
  407. *
  408. * @param priority is the priority of thread.
  409. *
  410. * @param tick is the time slice if there are same priority thread.
  411. *
  412. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  413. * If the return value is RT_NULL, it means this operation failed.
  414. */
  415. rt_thread_t rt_thread_create(const char *name,
  416. void (*entry)(void *parameter),
  417. void *parameter,
  418. rt_uint32_t stack_size,
  419. rt_uint8_t priority,
  420. rt_uint32_t tick)
  421. {
  422. struct rt_thread *thread;
  423. void *stack_start;
  424. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  425. name);
  426. if (thread == RT_NULL)
  427. return RT_NULL;
  428. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  429. if (stack_start == RT_NULL)
  430. {
  431. /* allocate stack failure */
  432. rt_object_delete((rt_object_t)thread);
  433. return RT_NULL;
  434. }
  435. _thread_init(thread,
  436. name,
  437. entry,
  438. parameter,
  439. stack_start,
  440. stack_size,
  441. priority,
  442. tick);
  443. return thread;
  444. }
  445. RTM_EXPORT(rt_thread_create);
  446. /**
  447. * @brief This function will delete a thread. The thread object will be removed from
  448. * thread queue and deleted from system object management in the idle thread.
  449. *
  450. * @param thread is the thread to be deleted.
  451. *
  452. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  453. * If the return value is any other values, it means this operation failed.
  454. */
  455. rt_err_t rt_thread_delete(rt_thread_t thread)
  456. {
  457. rt_base_t level;
  458. /* parameter check */
  459. RT_ASSERT(thread != RT_NULL);
  460. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  461. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  462. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  463. return RT_EOK;
  464. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  465. {
  466. /* remove from schedule */
  467. rt_schedule_remove_thread(thread);
  468. }
  469. /* disable interrupt */
  470. level = rt_hw_interrupt_disable();
  471. /* release thread timer */
  472. rt_timer_detach(&(thread->thread_timer));
  473. /* change stat */
  474. thread->stat = RT_THREAD_CLOSE;
  475. #ifdef RT_USING_MUTEX
  476. if ((thread->pending_object) &&
  477. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  478. {
  479. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  480. rt_mutex_drop_thread(mutex, thread);
  481. thread->pending_object = RT_NULL;
  482. }
  483. #endif
  484. /* insert to defunct thread list */
  485. rt_thread_defunct_enqueue(thread);
  486. /* enable interrupt */
  487. rt_hw_interrupt_enable(level);
  488. return RT_EOK;
  489. }
  490. RTM_EXPORT(rt_thread_delete);
  491. #endif /* RT_USING_HEAP */
  492. /**
  493. * @brief This function will let current thread yield processor, and scheduler will
  494. * choose the highest thread to run. After yield processor, the current thread
  495. * is still in READY state.
  496. *
  497. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  498. * If the return value is any other values, it means this operation failed.
  499. */
  500. rt_err_t rt_thread_yield(void)
  501. {
  502. struct rt_thread *thread;
  503. rt_base_t level;
  504. thread = rt_thread_self();
  505. level = rt_hw_interrupt_disable();
  506. thread->remaining_tick = thread->init_tick;
  507. thread->stat |= RT_THREAD_STAT_YIELD;
  508. rt_hw_interrupt_enable(level);
  509. rt_schedule();
  510. return RT_EOK;
  511. }
  512. RTM_EXPORT(rt_thread_yield);
  513. /**
  514. * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend,
  515. * when the thread timer reaches the tick value, scheduler will awaken this thread.
  516. *
  517. * @param tick is the sleep ticks.
  518. *
  519. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  520. * If the return value is any other values, it means this operation failed.
  521. */
  522. rt_err_t rt_thread_sleep(rt_tick_t tick)
  523. {
  524. rt_base_t level;
  525. struct rt_thread *thread;
  526. int err;
  527. if (tick == 0)
  528. {
  529. return -RT_EINVAL;
  530. }
  531. /* set to current thread */
  532. thread = rt_thread_self();
  533. RT_ASSERT(thread != RT_NULL);
  534. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  535. /* current context checking */
  536. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  537. /* disable interrupt */
  538. level = rt_hw_interrupt_disable();
  539. /* reset thread error */
  540. thread->error = RT_EOK;
  541. /* suspend thread */
  542. err = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  543. /* reset the timeout of thread timer and start it */
  544. if (err == RT_EOK)
  545. {
  546. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  547. rt_timer_start(&(thread->thread_timer));
  548. /* enable interrupt */
  549. rt_hw_interrupt_enable(level);
  550. thread->error = -RT_EINTR;
  551. rt_schedule();
  552. /* clear error number of this thread to RT_EOK */
  553. if (thread->error == -RT_ETIMEOUT)
  554. thread->error = RT_EOK;
  555. }
  556. else
  557. {
  558. rt_hw_interrupt_enable(level);
  559. }
  560. return err;
  561. }
  562. /**
  563. * @brief This function will let current thread delay for some ticks.
  564. *
  565. * @param tick is the delay ticks.
  566. *
  567. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  568. * If the return value is any other values, it means this operation failed.
  569. */
  570. rt_err_t rt_thread_delay(rt_tick_t tick)
  571. {
  572. return rt_thread_sleep(tick);
  573. }
  574. RTM_EXPORT(rt_thread_delay);
  575. /**
  576. * @brief This function will let current thread delay until (*tick + inc_tick).
  577. *
  578. * @param tick is the tick of last wakeup.
  579. *
  580. * @param inc_tick is the increment tick.
  581. *
  582. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  583. * If the return value is any other values, it means this operation failed.
  584. */
  585. rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
  586. {
  587. rt_base_t level;
  588. struct rt_thread *thread;
  589. rt_tick_t cur_tick;
  590. RT_ASSERT(tick != RT_NULL);
  591. /* set to current thread */
  592. thread = rt_thread_self();
  593. RT_ASSERT(thread != RT_NULL);
  594. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  595. /* disable interrupt */
  596. level = rt_hw_interrupt_disable();
  597. /* reset thread error */
  598. thread->error = RT_EOK;
  599. cur_tick = rt_tick_get();
  600. if (cur_tick - *tick < inc_tick)
  601. {
  602. rt_tick_t left_tick;
  603. *tick += inc_tick;
  604. left_tick = *tick - cur_tick;
  605. /* suspend thread */
  606. rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  607. /* reset the timeout of thread timer and start it */
  608. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick);
  609. rt_timer_start(&(thread->thread_timer));
  610. /* enable interrupt */
  611. rt_hw_interrupt_enable(level);
  612. rt_schedule();
  613. /* clear error number of this thread to RT_EOK */
  614. if (thread->error == -RT_ETIMEOUT)
  615. {
  616. thread->error = RT_EOK;
  617. }
  618. }
  619. else
  620. {
  621. *tick = cur_tick;
  622. rt_hw_interrupt_enable(level);
  623. }
  624. return thread->error;
  625. }
  626. RTM_EXPORT(rt_thread_delay_until);
  627. /**
  628. * @brief This function will let current thread delay for some milliseconds.
  629. *
  630. * @param ms is the delay ms time.
  631. *
  632. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  633. * If the return value is any other values, it means this operation failed.
  634. */
  635. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  636. {
  637. rt_tick_t tick;
  638. tick = rt_tick_from_millisecond(ms);
  639. return rt_thread_sleep(tick);
  640. }
  641. RTM_EXPORT(rt_thread_mdelay);
  642. #ifdef RT_USING_SMP
  643. static void rt_thread_cpu_bind(rt_thread_t thread, int cpu)
  644. {
  645. rt_base_t level;
  646. if (cpu >= RT_CPUS_NR)
  647. {
  648. cpu = RT_CPUS_NR;
  649. }
  650. level = rt_hw_interrupt_disable();
  651. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  652. {
  653. /* unbind */
  654. /* remove from old ready queue */
  655. rt_schedule_remove_thread(thread);
  656. /* change thread bind cpu */
  657. thread->bind_cpu = cpu;
  658. /* add to new ready queue */
  659. rt_schedule_insert_thread(thread);
  660. if (rt_thread_self() != RT_NULL)
  661. {
  662. rt_schedule();
  663. }
  664. }
  665. else
  666. {
  667. thread->bind_cpu = cpu;
  668. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  669. {
  670. /* thread is running on a cpu */
  671. int current_cpu = rt_hw_cpu_id();
  672. if (cpu != RT_CPUS_NR)
  673. {
  674. if (thread->oncpu == current_cpu)
  675. {
  676. /* current thread on current cpu */
  677. if (cpu != current_cpu)
  678. {
  679. /* bind to other cpu */
  680. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << cpu);
  681. /* self cpu need reschedule */
  682. rt_schedule();
  683. }
  684. /* else do nothing */
  685. }
  686. else
  687. {
  688. /* no running on self cpu, but dest cpu can be itself */
  689. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << thread->oncpu);
  690. }
  691. }
  692. /* else do nothing */
  693. }
  694. }
  695. rt_hw_interrupt_enable(level);
  696. }
  697. #endif
  698. /**
  699. * @brief This function will control thread behaviors according to control command.
  700. *
  701. * @param thread is the specified thread to be controlled.
  702. *
  703. * @param cmd is the control command, which includes.
  704. *
  705. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
  706. *
  707. * RT_THREAD_CTRL_STARTUP for starting a thread.
  708. *
  709. * RT_THREAD_CTRL_CLOSE for delete a thread.
  710. *
  711. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  712. *
  713. * @param arg is the argument of control command.
  714. *
  715. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  716. * If the return value is any other values, it means this operation failed.
  717. */
  718. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  719. {
  720. rt_base_t level;
  721. /* parameter check */
  722. RT_ASSERT(thread != RT_NULL);
  723. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  724. switch (cmd)
  725. {
  726. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  727. {
  728. /* disable interrupt */
  729. level = rt_hw_interrupt_disable();
  730. /* for ready thread, change queue */
  731. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  732. {
  733. /* remove thread from schedule queue first */
  734. rt_schedule_remove_thread(thread);
  735. /* change thread priority */
  736. thread->current_priority = *(rt_uint8_t *)arg;
  737. /* recalculate priority attribute */
  738. #if RT_THREAD_PRIORITY_MAX > 32
  739. thread->number = thread->current_priority >> 3; /* 5bit */
  740. thread->number_mask = 1 << thread->number;
  741. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  742. #else
  743. thread->number_mask = 1 << thread->current_priority;
  744. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  745. /* insert thread to schedule queue again */
  746. rt_schedule_insert_thread(thread);
  747. }
  748. else
  749. {
  750. thread->current_priority = *(rt_uint8_t *)arg;
  751. /* recalculate priority attribute */
  752. #if RT_THREAD_PRIORITY_MAX > 32
  753. thread->number = thread->current_priority >> 3; /* 5bit */
  754. thread->number_mask = 1 << thread->number;
  755. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  756. #else
  757. thread->number_mask = 1 << thread->current_priority;
  758. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  759. }
  760. /* enable interrupt */
  761. rt_hw_interrupt_enable(level);
  762. break;
  763. }
  764. case RT_THREAD_CTRL_STARTUP:
  765. {
  766. return rt_thread_startup(thread);
  767. }
  768. case RT_THREAD_CTRL_CLOSE:
  769. {
  770. rt_err_t rt_err = -RT_EINVAL;
  771. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  772. {
  773. rt_err = rt_thread_detach(thread);
  774. }
  775. #ifdef RT_USING_HEAP
  776. else
  777. {
  778. rt_err = rt_thread_delete(thread);
  779. }
  780. #endif /* RT_USING_HEAP */
  781. rt_schedule();
  782. return rt_err;
  783. }
  784. #ifdef RT_USING_SMP
  785. case RT_THREAD_CTRL_BIND_CPU:
  786. {
  787. rt_uint8_t cpu;
  788. cpu = (rt_uint8_t)(size_t)arg;
  789. rt_thread_cpu_bind(thread, cpu);
  790. break;
  791. }
  792. #endif /*RT_USING_SMP*/
  793. default:
  794. break;
  795. }
  796. return RT_EOK;
  797. }
  798. RTM_EXPORT(rt_thread_control);
  799. #ifdef RT_USING_SMART
  800. int lwp_suspend_sigcheck(rt_thread_t thread, int suspend_flag);
  801. #endif
  802. static void rt_thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
  803. {
  804. rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  805. RT_ASSERT(thread != RT_NULL);
  806. switch (suspend_flag)
  807. {
  808. case RT_INTERRUPTIBLE:
  809. stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
  810. break;
  811. case RT_KILLABLE:
  812. stat = RT_THREAD_SUSPEND_KILLABLE;
  813. break;
  814. case RT_UNINTERRUPTIBLE:
  815. stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  816. break;
  817. default:
  818. RT_ASSERT(0);
  819. break;
  820. }
  821. thread->stat = stat | (thread->stat & ~RT_THREAD_STAT_MASK);
  822. }
  823. /**
  824. * @brief This function will suspend the specified thread and change it to suspend state.
  825. *
  826. * @note This function ONLY can suspend current thread itself.
  827. * rt_thread_suspend(rt_thread_self());
  828. *
  829. * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
  830. * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
  831. * other threads and occupying this resouce, starvation can occur very easily.
  832. *
  833. * @param thread the thread to be suspended.
  834. * @param suspend_flag status flag of the thread to be suspended.
  835. *
  836. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  837. * If the return value is any other values, it means this operation failed.
  838. */
  839. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  840. {
  841. rt_base_t stat;
  842. rt_base_t level;
  843. /* parameter check */
  844. RT_ASSERT(thread != RT_NULL);
  845. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  846. RT_ASSERT(thread == rt_thread_self());
  847. LOG_D("thread suspend: %s", thread->parent.name);
  848. stat = thread->stat & RT_THREAD_STAT_MASK;
  849. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  850. {
  851. LOG_D("thread suspend: thread disorder, 0x%2x", thread->stat);
  852. return -RT_ERROR;
  853. }
  854. /* disable interrupt */
  855. level = rt_hw_interrupt_disable();
  856. if (stat == RT_THREAD_RUNNING)
  857. {
  858. /* not suspend running status thread on other core */
  859. RT_ASSERT(thread == rt_thread_self());
  860. }
  861. #ifdef RT_USING_SMART
  862. if (lwp_suspend_sigcheck(thread, suspend_flag) == 0)
  863. {
  864. /* not to suspend */
  865. rt_hw_interrupt_enable(level);
  866. rt_kprintf("-RT_EINTR\r\n");
  867. return -RT_EINTR;
  868. }
  869. #endif
  870. /* change thread stat */
  871. rt_schedule_remove_thread(thread);
  872. rt_thread_set_suspend_state(thread, suspend_flag);
  873. /* stop thread timer anyway */
  874. rt_timer_stop(&(thread->thread_timer));
  875. /* enable interrupt */
  876. rt_hw_interrupt_enable(level);
  877. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  878. return RT_EOK;
  879. }
  880. RTM_EXPORT(rt_thread_suspend_with_flag);
  881. rt_err_t rt_thread_suspend(rt_thread_t thread)
  882. {
  883. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  884. }
  885. RTM_EXPORT(rt_thread_suspend);
  886. /**
  887. * @brief This function will resume a thread and put it to system ready queue.
  888. *
  889. * @param thread is the thread to be resumed.
  890. *
  891. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  892. * If the return value is any other values, it means this operation failed.
  893. */
  894. rt_err_t rt_thread_resume(rt_thread_t thread)
  895. {
  896. rt_base_t level;
  897. /* parameter check */
  898. RT_ASSERT(thread != RT_NULL);
  899. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  900. LOG_D("thread resume: %s", thread->parent.name);
  901. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  902. {
  903. LOG_D("thread resume: thread disorder, %d",
  904. thread->stat);
  905. return -RT_ERROR;
  906. }
  907. /* disable interrupt */
  908. level = rt_hw_interrupt_disable();
  909. /* remove from suspend list */
  910. rt_list_remove(&(thread->tlist));
  911. rt_timer_stop(&thread->thread_timer);
  912. #ifdef RT_USING_SMART
  913. thread->wakeup.func = RT_NULL;
  914. #endif
  915. /* enable interrupt */
  916. rt_hw_interrupt_enable(level);
  917. /* insert to schedule ready list */
  918. rt_schedule_insert_thread(thread);
  919. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  920. return RT_EOK;
  921. }
  922. RTM_EXPORT(rt_thread_resume);
  923. #ifdef RT_USING_SMART
  924. /**
  925. * This function will wakeup a thread with customized operation.
  926. *
  927. * @param thread the thread to be resumed
  928. *
  929. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  930. */
  931. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  932. {
  933. register rt_base_t temp;
  934. rt_err_t ret;
  935. RT_ASSERT(thread != RT_NULL);
  936. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  937. /* disable interrupt */
  938. temp = rt_hw_interrupt_disable();
  939. if (thread->wakeup.func)
  940. {
  941. ret = thread->wakeup.func(thread->wakeup.user_data, thread);
  942. thread->wakeup.func = RT_NULL;
  943. }
  944. else
  945. {
  946. ret = rt_thread_resume(thread);
  947. }
  948. rt_hw_interrupt_enable(temp);
  949. return ret;
  950. }
  951. RTM_EXPORT(rt_thread_wakeup);
  952. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  953. {
  954. register rt_base_t temp;
  955. RT_ASSERT(thread != RT_NULL);
  956. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  957. temp = rt_hw_interrupt_disable();
  958. thread->wakeup.func = func;
  959. thread->wakeup.user_data = user_data;
  960. rt_hw_interrupt_enable(temp);
  961. }
  962. RTM_EXPORT(rt_thread_wakeup_set);
  963. #endif
  964. /**
  965. * @brief This function will find the specified thread.
  966. *
  967. * @note Please don't invoke this function in interrupt status.
  968. *
  969. * @param name is the name of thread finding.
  970. *
  971. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  972. * If the return value is RT_NULL, it means this operation failed.
  973. */
  974. rt_thread_t rt_thread_find(char *name)
  975. {
  976. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  977. }
  978. RTM_EXPORT(rt_thread_find);
  979. /**
  980. * @brief This function will return the name of the specified thread
  981. *
  982. * @note Please don't invoke this function in interrupt status
  983. *
  984. * @param thread the thread to retrieve thread name
  985. * @param name buffer to store the thread name string
  986. * @param name_size maximum size of the buffer to store the thread name
  987. *
  988. * @return If the return value is RT_EOK, the function is successfully executed
  989. * If the return value is -RT_EINVAL, it means this operation failed
  990. */
  991. rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size)
  992. {
  993. return (thread == RT_NULL) ? -RT_EINVAL : rt_object_get_name(&thread->parent, name, name_size);
  994. }
  995. RTM_EXPORT(rt_thread_get_name);
  996. /**@}*/