thread.c 34 KB

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