thread.c 34 KB

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