thread.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. #ifndef RT_USING_SMP
  301. return rt_cpu_self()->current_thread;
  302. #elif defined (ARCH_USING_HW_THREAD_SELF)
  303. return rt_hw_thread_self();
  304. #else /* !ARCH_USING_HW_THREAD_SELF */
  305. rt_thread_t self;
  306. rt_base_t lock;
  307. lock = rt_hw_local_irq_disable();
  308. self = rt_cpu_self()->current_thread;
  309. rt_hw_local_irq_enable(lock);
  310. return self;
  311. #endif /* ARCH_USING_HW_THREAD_SELF */
  312. }
  313. RTM_EXPORT(rt_thread_self);
  314. /**
  315. * @brief This function will start a thread and put it to system ready queue.
  316. *
  317. * @param thread is the thread to be started.
  318. *
  319. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  320. * If the return value is any other values, it means this operation failed.
  321. */
  322. rt_err_t rt_thread_startup(rt_thread_t thread)
  323. {
  324. /* parameter check */
  325. RT_ASSERT(thread != RT_NULL);
  326. RT_ASSERT((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  327. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  328. LOG_D("startup a thread:%s with priority:%d",
  329. thread->parent.name, thread->current_priority);
  330. /* calculate priority attribute and reset thread stat to suspend */
  331. rt_sched_thread_startup(thread);
  332. /* resume and do a schedule if scheduler is available */
  333. rt_thread_resume(thread);
  334. return RT_EOK;
  335. }
  336. RTM_EXPORT(rt_thread_startup);
  337. /**
  338. * @brief This function will close a thread. The thread object will be removed from
  339. * thread queue and detached/deleted from the system object management.
  340. * It's different from rt_thread_delete or rt_thread_detach that this will not enqueue
  341. * the closing thread to cleanup queue.
  342. *
  343. * @param thread is the thread to be closed.
  344. *
  345. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  346. * If the return value is any other values, it means this operation failed.
  347. */
  348. rt_err_t rt_thread_close(rt_thread_t thread)
  349. {
  350. rt_sched_lock_level_t slvl;
  351. rt_uint8_t thread_status;
  352. /* forbid scheduling on current core if closing current thread */
  353. RT_ASSERT(thread != rt_thread_self() || rt_critical_level());
  354. /* before checking status of scheduler */
  355. rt_sched_lock(&slvl);
  356. /* check if thread is already closed */
  357. thread_status = rt_sched_thread_get_stat(thread);
  358. if (thread_status != RT_THREAD_CLOSE)
  359. {
  360. if (thread_status != RT_THREAD_INIT)
  361. {
  362. /* remove from schedule */
  363. rt_sched_remove_thread(thread);
  364. }
  365. /* release thread timer */
  366. rt_timer_detach(&(thread->thread_timer));
  367. /* change stat */
  368. rt_sched_thread_close(thread);
  369. }
  370. /* scheduler works are done */
  371. rt_sched_unlock(slvl);
  372. return RT_EOK;
  373. }
  374. RTM_EXPORT(rt_thread_close);
  375. static rt_err_t _thread_detach(rt_thread_t thread);
  376. /**
  377. * @brief This function will detach a thread. The thread object will be removed from
  378. * thread queue and detached/deleted from the system object management.
  379. *
  380. * @param thread is the thread to be deleted.
  381. *
  382. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  383. * If the return value is any other values, it means this operation failed.
  384. */
  385. rt_err_t rt_thread_detach(rt_thread_t thread)
  386. {
  387. /* parameter check */
  388. RT_ASSERT(thread != RT_NULL);
  389. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  390. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  391. return _thread_detach(thread);
  392. }
  393. RTM_EXPORT(rt_thread_detach);
  394. static rt_err_t _thread_detach(rt_thread_t thread)
  395. {
  396. rt_err_t error;
  397. rt_base_t critical_level;
  398. /**
  399. * forbid scheduling on current core before returning since current thread
  400. * may be detached from scheduler.
  401. */
  402. critical_level = rt_enter_critical();
  403. error = rt_thread_close(thread);
  404. _thread_detach_from_mutex(thread);
  405. /* insert to defunct thread list */
  406. rt_thread_defunct_enqueue(thread);
  407. rt_exit_critical_safe(critical_level);
  408. return error;
  409. }
  410. #ifdef RT_USING_HEAP
  411. /**
  412. * @brief This function will create a thread object and allocate thread object memory.
  413. * and stack.
  414. *
  415. * @param name is the name of thread, which shall be unique.
  416. *
  417. * @param entry is the entry function of thread.
  418. *
  419. * @param parameter is the parameter of thread enter function.
  420. *
  421. * @param stack_size is the size of thread stack.
  422. *
  423. * @param priority is the priority of thread.
  424. *
  425. * @param tick is the time slice if there are same priority thread.
  426. *
  427. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  428. * If the return value is RT_NULL, it means this operation failed.
  429. */
  430. rt_thread_t rt_thread_create(const char *name,
  431. void (*entry)(void *parameter),
  432. void *parameter,
  433. rt_uint32_t stack_size,
  434. rt_uint8_t priority,
  435. rt_uint32_t tick)
  436. {
  437. /* parameter check */
  438. RT_ASSERT(tick != 0);
  439. struct rt_thread *thread;
  440. void *stack_start;
  441. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  442. name);
  443. if (thread == RT_NULL)
  444. return RT_NULL;
  445. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  446. if (stack_start == RT_NULL)
  447. {
  448. /* allocate stack failure */
  449. rt_object_delete((rt_object_t)thread);
  450. return RT_NULL;
  451. }
  452. _thread_init(thread,
  453. name,
  454. entry,
  455. parameter,
  456. stack_start,
  457. stack_size,
  458. priority,
  459. tick);
  460. return thread;
  461. }
  462. RTM_EXPORT(rt_thread_create);
  463. /**
  464. * @brief This function will delete a thread. The thread object will be removed from
  465. * thread queue and deleted from system object management in the idle thread.
  466. *
  467. * @param thread is the thread to be deleted.
  468. *
  469. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  470. * If the return value is any other values, it means this operation failed.
  471. */
  472. rt_err_t rt_thread_delete(rt_thread_t thread)
  473. {
  474. /* parameter check */
  475. RT_ASSERT(thread != RT_NULL);
  476. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  477. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  478. return _thread_detach(thread);
  479. }
  480. RTM_EXPORT(rt_thread_delete);
  481. #endif /* RT_USING_HEAP */
  482. /**
  483. * @brief This function will let current thread yield processor, and scheduler will
  484. * choose the highest thread to run. After yield processor, the current thread
  485. * is still in READY state.
  486. *
  487. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  488. * If the return value is any other values, it means this operation failed.
  489. */
  490. rt_err_t rt_thread_yield(void)
  491. {
  492. rt_sched_lock_level_t slvl;
  493. rt_sched_lock(&slvl);
  494. rt_sched_thread_yield(rt_thread_self());
  495. rt_sched_unlock_n_resched(slvl);
  496. return RT_EOK;
  497. }
  498. RTM_EXPORT(rt_thread_yield);
  499. /**
  500. * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend,
  501. * when the thread timer reaches the tick value, scheduler will awaken this thread.
  502. *
  503. * @param tick is the sleep ticks.
  504. *
  505. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  506. * If the return value is any other values, it means this operation failed.
  507. */
  508. static rt_err_t _thread_sleep(rt_tick_t tick)
  509. {
  510. struct rt_thread *thread;
  511. rt_base_t critical_level;
  512. int err;
  513. if (tick == 0)
  514. {
  515. return -RT_EINVAL;
  516. }
  517. /* set to current thread */
  518. thread = rt_thread_self();
  519. RT_ASSERT(thread != RT_NULL);
  520. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  521. /* current context checking */
  522. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  523. /* reset thread error */
  524. thread->error = RT_EOK;
  525. /* lock scheduler since current thread may be suspended */
  526. critical_level = rt_enter_critical();
  527. /* suspend thread */
  528. err = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  529. /* reset the timeout of thread timer and start it */
  530. if (err == RT_EOK)
  531. {
  532. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  533. rt_timer_start(&(thread->thread_timer));
  534. thread->error = -RT_EINTR;
  535. /* notify a pending rescheduling */
  536. rt_schedule();
  537. /* exit critical and do a rescheduling */
  538. rt_exit_critical_safe(critical_level);
  539. /* clear error number of this thread to RT_EOK */
  540. if (thread->error == -RT_ETIMEOUT)
  541. thread->error = RT_EOK;
  542. }
  543. else
  544. {
  545. rt_exit_critical_safe(critical_level);
  546. }
  547. return err;
  548. }
  549. /**
  550. * @brief This function will let current thread delay for some ticks.
  551. *
  552. * @param tick is the delay ticks.
  553. *
  554. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  555. * If the return value is any other values, it means this operation failed.
  556. */
  557. rt_err_t rt_thread_delay(rt_tick_t tick)
  558. {
  559. return _thread_sleep(tick);
  560. }
  561. RTM_EXPORT(rt_thread_delay);
  562. /**
  563. * @brief This function will let current thread delay until (*tick + inc_tick).
  564. *
  565. * @param tick is the tick of last wakeup.
  566. *
  567. * @param inc_tick is the increment tick.
  568. *
  569. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  570. * If the return value is any other values, it means this operation failed.
  571. */
  572. rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
  573. {
  574. struct rt_thread *thread;
  575. rt_tick_t cur_tick;
  576. rt_base_t critical_level;
  577. RT_ASSERT(tick != RT_NULL);
  578. /* set to current thread */
  579. thread = rt_thread_self();
  580. RT_ASSERT(thread != RT_NULL);
  581. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  582. /* reset thread error */
  583. thread->error = RT_EOK;
  584. /* disable interrupt */
  585. critical_level = rt_enter_critical();
  586. cur_tick = rt_tick_get();
  587. if (cur_tick - *tick < inc_tick)
  588. {
  589. rt_tick_t left_tick;
  590. *tick += inc_tick;
  591. left_tick = *tick - cur_tick;
  592. /* suspend thread */
  593. rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  594. /* reset the timeout of thread timer and start it */
  595. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick);
  596. rt_timer_start(&(thread->thread_timer));
  597. rt_exit_critical_safe(critical_level);
  598. rt_schedule();
  599. /* clear error number of this thread to RT_EOK */
  600. if (thread->error == -RT_ETIMEOUT)
  601. {
  602. thread->error = RT_EOK;
  603. }
  604. }
  605. else
  606. {
  607. *tick = cur_tick;
  608. rt_exit_critical_safe(critical_level);
  609. }
  610. return thread->error;
  611. }
  612. RTM_EXPORT(rt_thread_delay_until);
  613. /**
  614. * @brief This function will let current thread delay for some milliseconds.
  615. *
  616. * @param ms is the delay ms time.
  617. *
  618. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  619. * If the return value is any other values, it means this operation failed.
  620. */
  621. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  622. {
  623. rt_tick_t tick;
  624. tick = rt_tick_from_millisecond(ms);
  625. return _thread_sleep(tick);
  626. }
  627. RTM_EXPORT(rt_thread_mdelay);
  628. #ifdef RT_USING_SMP
  629. #endif
  630. /**
  631. * @brief This function will control thread behaviors according to control command.
  632. *
  633. * @param thread is the specified thread to be controlled.
  634. *
  635. * @param cmd is the control command, which includes.
  636. *
  637. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
  638. *
  639. * RT_THREAD_CTRL_STARTUP for starting a thread.
  640. *
  641. * RT_THREAD_CTRL_CLOSE for delete a thread.
  642. *
  643. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  644. *
  645. * @param arg is the argument of control command.
  646. *
  647. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  648. * If the return value is any other values, it means this operation failed.
  649. */
  650. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  651. {
  652. /* parameter check */
  653. RT_ASSERT(thread != RT_NULL);
  654. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  655. switch (cmd)
  656. {
  657. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  658. {
  659. rt_err_t error;
  660. rt_sched_lock_level_t slvl;
  661. rt_sched_lock(&slvl);
  662. error = rt_sched_thread_change_priority(thread, *(rt_uint8_t *)arg);
  663. rt_sched_unlock(slvl);
  664. return error;
  665. }
  666. case RT_THREAD_CTRL_STARTUP:
  667. {
  668. return rt_thread_startup(thread);
  669. }
  670. case RT_THREAD_CTRL_CLOSE:
  671. {
  672. rt_err_t rt_err = -RT_EINVAL;
  673. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  674. {
  675. rt_err = rt_thread_detach(thread);
  676. }
  677. #ifdef RT_USING_HEAP
  678. else
  679. {
  680. rt_err = rt_thread_delete(thread);
  681. }
  682. #endif /* RT_USING_HEAP */
  683. rt_schedule();
  684. return rt_err;
  685. }
  686. case RT_THREAD_CTRL_BIND_CPU:
  687. {
  688. rt_uint8_t cpu;
  689. cpu = (rt_uint8_t)(size_t)arg;
  690. return rt_sched_thread_bind_cpu(thread, cpu);
  691. }
  692. default:
  693. break;
  694. }
  695. return RT_EOK;
  696. }
  697. RTM_EXPORT(rt_thread_control);
  698. #ifdef RT_USING_SMART
  699. #include <lwp_signal.h>
  700. #endif
  701. static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
  702. {
  703. rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  704. RT_ASSERT(thread != RT_NULL);
  705. switch (suspend_flag)
  706. {
  707. case RT_INTERRUPTIBLE:
  708. stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
  709. break;
  710. case RT_KILLABLE:
  711. stat = RT_THREAD_SUSPEND_KILLABLE;
  712. break;
  713. case RT_UNINTERRUPTIBLE:
  714. stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  715. break;
  716. default:
  717. RT_ASSERT(0);
  718. break;
  719. }
  720. RT_SCHED_CTX(thread).stat = stat | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  721. }
  722. /**
  723. * @brief This function will suspend the specified thread and change it to suspend state.
  724. *
  725. * @note This function ONLY can suspend current thread itself.
  726. * rt_thread_suspend(rt_thread_self());
  727. *
  728. * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
  729. * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
  730. * other threads and occupying this resouce, starvation can occur very easily.
  731. *
  732. * @param thread the thread to be suspended.
  733. * @param susp_list the list thread enqueued to. RT_NULL if no list.
  734. * @param ipc_flags is a flag for the thread object to be suspended. It determines how the thread is suspended.
  735. * The flag can be ONE of the following values:
  736. * RT_IPC_FLAG_PRIO The pending threads will queue in order of priority.
  737. * RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method
  738. * (also known as first-come-first-served (FCFS) scheduling strategy).
  739. * NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to use
  740. * RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
  741. * the first-in-first-out principle, and you clearly understand that all threads involved in
  742. * this semaphore will become non-real-time threads.
  743. * @param suspend_flag status flag of the thread to be suspended.
  744. *
  745. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  746. * If the return value is any other values, it means this operation failed.
  747. */
  748. rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int ipc_flags, int suspend_flag)
  749. {
  750. rt_base_t stat;
  751. rt_sched_lock_level_t slvl;
  752. /* parameter check */
  753. RT_ASSERT(thread != RT_NULL);
  754. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  755. RT_ASSERT(thread == rt_thread_self());
  756. LOG_D("thread suspend: %s", thread->parent.name);
  757. rt_sched_lock(&slvl);
  758. stat = rt_sched_thread_get_stat(thread);
  759. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  760. {
  761. LOG_D("thread suspend: thread disorder, 0x%2x", thread->stat);
  762. rt_sched_unlock(slvl);
  763. return -RT_ERROR;
  764. }
  765. if (stat == RT_THREAD_RUNNING)
  766. {
  767. /* not suspend running status thread on other core */
  768. RT_ASSERT(thread == rt_thread_self());
  769. }
  770. #ifdef RT_USING_SMART
  771. if (thread->lwp)
  772. {
  773. rt_sched_unlock(slvl);
  774. /* check pending signals for thread before suspend */
  775. if (lwp_thread_signal_suspend_check(thread, suspend_flag) == 0)
  776. {
  777. /* not to suspend */
  778. return -RT_EINTR;
  779. }
  780. rt_sched_lock(&slvl);
  781. if (stat == RT_THREAD_READY)
  782. {
  783. stat = rt_sched_thread_get_stat(thread);
  784. if (stat != RT_THREAD_READY)
  785. {
  786. /* status updated while we check for signal */
  787. rt_sched_unlock(slvl);
  788. return -RT_ERROR;
  789. }
  790. }
  791. }
  792. #endif
  793. /* change thread stat */
  794. rt_sched_remove_thread(thread);
  795. _thread_set_suspend_state(thread, suspend_flag);
  796. if (susp_list)
  797. {
  798. /**
  799. * enqueue thread on the push list before leaving critical region of
  800. * scheduler, so we won't miss notification of async events.
  801. */
  802. rt_susp_list_enqueue(susp_list, thread, ipc_flags);
  803. }
  804. /* stop thread timer anyway */
  805. rt_sched_thread_timer_stop(thread);
  806. rt_sched_unlock(slvl);
  807. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  808. return RT_EOK;
  809. }
  810. RTM_EXPORT(rt_thread_suspend_to_list);
  811. /**
  812. * @brief This function will suspend the specified thread and change it to suspend state.
  813. *
  814. * @note This function ONLY can suspend current thread itself.
  815. * rt_thread_suspend(rt_thread_self());
  816. *
  817. * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
  818. * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
  819. * other threads and occupying this resouce, starvation can occur very easily.
  820. *
  821. * @param thread the thread to be suspended.
  822. * @param suspend_flag status flag of the thread to be suspended.
  823. *
  824. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  825. * If the return value is any other values, it means this operation failed.
  826. */
  827. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  828. {
  829. return rt_thread_suspend_to_list(thread, RT_NULL, 0, suspend_flag);
  830. }
  831. RTM_EXPORT(rt_thread_suspend_with_flag);
  832. rt_err_t rt_thread_suspend(rt_thread_t thread)
  833. {
  834. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  835. }
  836. RTM_EXPORT(rt_thread_suspend);
  837. /**
  838. * @brief This function will resume a thread and put it to system ready queue.
  839. *
  840. * @param thread is the thread to be resumed.
  841. *
  842. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  843. * If the return value is any other values, it means this operation failed.
  844. */
  845. rt_err_t rt_thread_resume(rt_thread_t thread)
  846. {
  847. rt_sched_lock_level_t slvl;
  848. rt_err_t error;
  849. /* parameter check */
  850. RT_ASSERT(thread != RT_NULL);
  851. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  852. LOG_D("thread resume: %s", thread->parent.name);
  853. rt_sched_lock(&slvl);
  854. error = rt_sched_thread_ready(thread);
  855. if (!error)
  856. {
  857. error = rt_sched_unlock_n_resched(slvl);
  858. /**
  859. * RT_ESCHEDLOCKED indicates that the current thread is in a critical section,
  860. * rather than 'thread' can't be resumed. Therefore, we can ignore this error.
  861. */
  862. if (error == -RT_ESCHEDLOCKED)
  863. {
  864. error = RT_EOK;
  865. }
  866. }
  867. else
  868. {
  869. rt_sched_unlock(slvl);
  870. }
  871. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  872. return error;
  873. }
  874. RTM_EXPORT(rt_thread_resume);
  875. #ifdef RT_USING_SMART
  876. /**
  877. * This function will wakeup a thread with customized operation.
  878. *
  879. * @param thread the thread to be resumed
  880. *
  881. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  882. */
  883. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  884. {
  885. rt_sched_lock_level_t slvl;
  886. rt_err_t ret;
  887. rt_wakeup_func_t func = RT_NULL;
  888. RT_ASSERT(thread != RT_NULL);
  889. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  890. rt_sched_lock(&slvl);
  891. func = thread->wakeup_handle.func;
  892. thread->wakeup_handle.func = RT_NULL;
  893. rt_sched_unlock(slvl);
  894. if (func)
  895. {
  896. ret = func(thread->wakeup_handle.user_data, thread);
  897. }
  898. else
  899. {
  900. ret = rt_thread_resume(thread);
  901. }
  902. return ret;
  903. }
  904. RTM_EXPORT(rt_thread_wakeup);
  905. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  906. {
  907. rt_sched_lock_level_t slvl;
  908. RT_ASSERT(thread != RT_NULL);
  909. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  910. rt_sched_lock(&slvl);
  911. thread->wakeup_handle.func = func;
  912. thread->wakeup_handle.user_data = user_data;
  913. rt_sched_unlock(slvl);
  914. }
  915. RTM_EXPORT(rt_thread_wakeup_set);
  916. #endif
  917. /**
  918. * @brief This function will find the specified thread.
  919. *
  920. * @note Please don't invoke this function in interrupt status.
  921. *
  922. * @param name is the name of thread finding.
  923. *
  924. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  925. * If the return value is RT_NULL, it means this operation failed.
  926. */
  927. rt_thread_t rt_thread_find(char *name)
  928. {
  929. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  930. }
  931. RTM_EXPORT(rt_thread_find);
  932. /**
  933. * @brief This function will return the name of the specified thread
  934. *
  935. * @note Please don't invoke this function in interrupt status
  936. *
  937. * @param thread the thread to retrieve thread name
  938. * @param name buffer to store the thread name string
  939. * @param name_size maximum size of the buffer to store the thread name
  940. *
  941. * @return If the return value is RT_EOK, the function is successfully executed
  942. * If the return value is -RT_EINVAL, it means this operation failed
  943. */
  944. rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size)
  945. {
  946. return (thread == RT_NULL) ? -RT_EINVAL : rt_object_get_name(&thread->parent, name, name_size);
  947. }
  948. RTM_EXPORT(rt_thread_get_name);
  949. /**@}*/