thread.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. rt_memset(&thread->signal, 0, sizeof(lwp_sigset_t));
  222. rt_memset(&thread->signal_mask, 0, sizeof(lwp_sigset_t));
  223. thread->signal_mask_bak = 0;
  224. thread->signal_in_process = 0;
  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. int lwp_suspend_sigcheck(rt_thread_t thread, int suspend_flag);
  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_suspend_sigcheck(thread, suspend_flag) == 0)
  859. {
  860. /* not to suspend */
  861. rt_hw_interrupt_enable(level);
  862. rt_kprintf("-RT_EINTR\r\n");
  863. return -RT_EINTR;
  864. }
  865. #endif
  866. /* change thread stat */
  867. rt_schedule_remove_thread(thread);
  868. rt_thread_set_suspend_state(thread, suspend_flag);
  869. /* stop thread timer anyway */
  870. rt_timer_stop(&(thread->thread_timer));
  871. /* enable interrupt */
  872. rt_hw_interrupt_enable(level);
  873. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  874. return RT_EOK;
  875. }
  876. RTM_EXPORT(rt_thread_suspend_with_flag);
  877. rt_err_t rt_thread_suspend(rt_thread_t thread)
  878. {
  879. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  880. }
  881. RTM_EXPORT(rt_thread_suspend);
  882. /**
  883. * @brief This function will resume a thread and put it to system ready queue.
  884. *
  885. * @param thread is the thread to be resumed.
  886. *
  887. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  888. * If the return value is any other values, it means this operation failed.
  889. */
  890. rt_err_t rt_thread_resume(rt_thread_t thread)
  891. {
  892. rt_base_t level;
  893. /* parameter check */
  894. RT_ASSERT(thread != RT_NULL);
  895. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  896. LOG_D("thread resume: %s", thread->parent.name);
  897. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  898. {
  899. LOG_D("thread resume: thread disorder, %d",
  900. thread->stat);
  901. return -RT_ERROR;
  902. }
  903. /* disable interrupt */
  904. level = rt_hw_interrupt_disable();
  905. /* remove from suspend list */
  906. rt_list_remove(&(thread->tlist));
  907. rt_timer_stop(&thread->thread_timer);
  908. #ifdef RT_USING_SMART
  909. thread->wakeup.func = RT_NULL;
  910. #endif
  911. /* enable interrupt */
  912. rt_hw_interrupt_enable(level);
  913. /* insert to schedule ready list */
  914. rt_schedule_insert_thread(thread);
  915. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  916. return RT_EOK;
  917. }
  918. RTM_EXPORT(rt_thread_resume);
  919. #ifdef RT_USING_SMART
  920. /**
  921. * This function will wakeup a thread with customized operation.
  922. *
  923. * @param thread the thread to be resumed
  924. *
  925. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  926. */
  927. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  928. {
  929. register rt_base_t temp;
  930. rt_err_t ret;
  931. RT_ASSERT(thread != RT_NULL);
  932. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  933. /* disable interrupt */
  934. temp = rt_hw_interrupt_disable();
  935. if (thread->wakeup.func)
  936. {
  937. ret = thread->wakeup.func(thread->wakeup.user_data, thread);
  938. thread->wakeup.func = RT_NULL;
  939. }
  940. else
  941. {
  942. ret = rt_thread_resume(thread);
  943. }
  944. rt_hw_interrupt_enable(temp);
  945. return ret;
  946. }
  947. RTM_EXPORT(rt_thread_wakeup);
  948. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  949. {
  950. register rt_base_t temp;
  951. RT_ASSERT(thread != RT_NULL);
  952. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  953. temp = rt_hw_interrupt_disable();
  954. thread->wakeup.func = func;
  955. thread->wakeup.user_data = user_data;
  956. rt_hw_interrupt_enable(temp);
  957. }
  958. RTM_EXPORT(rt_thread_wakeup_set);
  959. #endif
  960. /**
  961. * @brief This function will find the specified thread.
  962. *
  963. * @note Please don't invoke this function in interrupt status.
  964. *
  965. * @param name is the name of thread finding.
  966. *
  967. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  968. * If the return value is RT_NULL, it means this operation failed.
  969. */
  970. rt_thread_t rt_thread_find(char *name)
  971. {
  972. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  973. }
  974. RTM_EXPORT(rt_thread_find);
  975. /**
  976. * @brief This function will return the name of the specified thread
  977. *
  978. * @note Please don't invoke this function in interrupt status
  979. *
  980. * @param thread the thread to retrieve thread name
  981. * @param name buffer to store the thread name string
  982. * @param name_size maximum size of the buffer to store the thread name
  983. *
  984. * @return If the return value is RT_EOK, the function is successfully executed
  985. * If the return value is -RT_EINVAL, it means this operation failed
  986. */
  987. rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size)
  988. {
  989. return (thread == RT_NULL) ? -RT_EINVAL : rt_object_get_name(&thread->parent, name, name_size);
  990. }
  991. RTM_EXPORT(rt_thread_get_name);
  992. /**@}*/