thread.c 34 KB

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