thread.c 33 KB

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