thread.c 35 KB

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