thread.c 33 KB

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