thread.c 34 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 rt_thread_sleep return thread->error when using signal
  34. * 2022-10-15 Bernard add nested mutex feature
  35. */
  36. #include <rthw.h>
  37. #include <rtthread.h>
  38. #include <stddef.h>
  39. #ifndef __on_rt_thread_inited_hook
  40. #define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
  41. #endif
  42. #ifndef __on_rt_thread_suspend_hook
  43. #define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
  44. #endif
  45. #ifndef __on_rt_thread_resume_hook
  46. #define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
  47. #endif
  48. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  49. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  50. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  51. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  52. /**
  53. * @brief This function sets a hook function when the system suspend a thread.
  54. *
  55. * @note The hook function must be simple and never be blocked or suspend.
  56. *
  57. * @param hook is the specified hook function.
  58. */
  59. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  60. {
  61. rt_thread_suspend_hook = hook;
  62. }
  63. /**
  64. * @brief This function sets a hook function when the system resume a thread.
  65. *
  66. * @note The hook function must be simple and never be blocked or suspend.
  67. *
  68. * @param hook is the specified hook function.
  69. */
  70. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  71. {
  72. rt_thread_resume_hook = hook;
  73. }
  74. /**
  75. * @brief This function sets a hook function when a thread is initialized.
  76. *
  77. * @param hook is the specified hook function.
  78. */
  79. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  80. {
  81. rt_thread_inited_hook = hook;
  82. }
  83. #endif /* defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) */
  84. static void _thread_exit(void)
  85. {
  86. struct rt_thread *thread;
  87. rt_base_t level;
  88. /* get current thread */
  89. thread = rt_thread_self();
  90. /* disable interrupt */
  91. level = rt_hw_interrupt_disable();
  92. /* remove from schedule */
  93. rt_schedule_remove_thread(thread);
  94. /* remove it from timer list */
  95. rt_timer_detach(&thread->thread_timer);
  96. /* change stat */
  97. thread->stat = RT_THREAD_CLOSE;
  98. /* insert to defunct thread list */
  99. rt_thread_defunct_enqueue(thread);
  100. /* enable interrupt */
  101. rt_hw_interrupt_enable(level);
  102. /* switch to next task */
  103. rt_schedule();
  104. }
  105. /**
  106. * @brief This function is the timeout function for thread, normally which is invoked
  107. * when thread is timeout to wait some resource.
  108. *
  109. * @param parameter is the parameter of thread timeout function
  110. */
  111. static void _thread_timeout(void *parameter)
  112. {
  113. struct rt_thread *thread;
  114. rt_base_t level;
  115. thread = (struct rt_thread *)parameter;
  116. /* parameter check */
  117. RT_ASSERT(thread != RT_NULL);
  118. RT_ASSERT((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK);
  119. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  120. /* disable interrupt */
  121. level = rt_hw_interrupt_disable();
  122. /* set error number */
  123. thread->error = -RT_ETIMEOUT;
  124. /* remove from suspend list */
  125. rt_list_remove(&(thread->tlist));
  126. /* insert to schedule ready list */
  127. rt_schedule_insert_thread(thread);
  128. /* enable interrupt */
  129. rt_hw_interrupt_enable(level);
  130. /* do schedule */
  131. rt_schedule();
  132. }
  133. static rt_err_t _thread_init(struct rt_thread *thread,
  134. const char *name,
  135. void (*entry)(void *parameter),
  136. void *parameter,
  137. void *stack_start,
  138. rt_uint32_t stack_size,
  139. rt_uint8_t priority,
  140. rt_uint32_t tick)
  141. {
  142. /* init thread list */
  143. rt_list_init(&(thread->tlist));
  144. #ifdef RT_USING_SMART
  145. thread->wakeup.func = RT_NULL;
  146. #endif
  147. thread->entry = (void *)entry;
  148. thread->parameter = parameter;
  149. /* stack init */
  150. thread->stack_addr = stack_start;
  151. thread->stack_size = stack_size;
  152. /* init thread stack */
  153. rt_memset(thread->stack_addr, '#', thread->stack_size);
  154. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  155. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  156. (void *)((char *)thread->stack_addr),
  157. (void *)_thread_exit);
  158. #else
  159. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  160. (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
  161. (void *)_thread_exit);
  162. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  163. /* priority init */
  164. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  165. thread->init_priority = priority;
  166. thread->current_priority = priority;
  167. thread->number_mask = 0;
  168. #ifdef RT_USING_MUTEX
  169. rt_list_init(&thread->taken_object_list);
  170. thread->pending_object = RT_NULL;
  171. #endif
  172. #ifdef RT_USING_EVENT
  173. thread->event_set = 0;
  174. thread->event_info = 0;
  175. #endif /* RT_USING_EVENT */
  176. #if RT_THREAD_PRIORITY_MAX > 32
  177. thread->number = 0;
  178. thread->high_mask = 0;
  179. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  180. /* tick init */
  181. thread->init_tick = tick;
  182. thread->remaining_tick = tick;
  183. /* error and flags */
  184. thread->error = RT_EOK;
  185. thread->stat = RT_THREAD_INIT;
  186. #ifdef RT_USING_SMP
  187. /* not bind on any cpu */
  188. thread->bind_cpu = RT_CPUS_NR;
  189. thread->oncpu = RT_CPU_DETACHED;
  190. /* lock init */
  191. thread->scheduler_lock_nest = 0;
  192. thread->cpus_lock_nest = 0;
  193. thread->critical_lock_nest = 0;
  194. #endif /* RT_USING_SMP */
  195. /* initialize cleanup function and user data */
  196. thread->cleanup = 0;
  197. thread->user_data = 0;
  198. /* initialize thread timer */
  199. rt_timer_init(&(thread->thread_timer),
  200. thread->parent.name,
  201. _thread_timeout,
  202. thread,
  203. 0,
  204. RT_TIMER_FLAG_ONE_SHOT);
  205. /* initialize signal */
  206. #ifdef RT_USING_SIGNALS
  207. thread->sig_mask = 0x00;
  208. thread->sig_pending = 0x00;
  209. #ifndef RT_USING_SMP
  210. thread->sig_ret = RT_NULL;
  211. #endif /* RT_USING_SMP */
  212. thread->sig_vectors = RT_NULL;
  213. thread->si_list = RT_NULL;
  214. #endif /* RT_USING_SIGNALS */
  215. #ifdef RT_USING_SMART
  216. thread->lwp = RT_NULL;
  217. rt_list_init(&(thread->sibling));
  218. rt_memset(&thread->signal, 0, sizeof(lwp_sigset_t));
  219. rt_memset(&thread->signal_mask, 0, sizeof(lwp_sigset_t));
  220. thread->signal_mask_bak = 0;
  221. thread->signal_in_process = 0;
  222. rt_memset(&thread->user_ctx, 0, sizeof thread->user_ctx);
  223. #endif
  224. #ifdef RT_USING_CPU_USAGE
  225. thread->duration_tick = 0;
  226. #endif /* RT_USING_CPU_USAGE */
  227. #ifdef RT_USING_PTHREADS
  228. thread->pthread_data = RT_NULL;
  229. #endif /* RT_USING_PTHREADS */
  230. #ifdef RT_USING_MODULE
  231. thread->parent.module_id = 0;
  232. #endif /* RT_USING_MODULE */
  233. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  234. return RT_EOK;
  235. }
  236. /**
  237. * @addtogroup Thread
  238. */
  239. /**@{*/
  240. /**
  241. * @brief This function will initialize a thread. It's used to initialize a
  242. * static thread object.
  243. *
  244. * @param thread is the static thread object.
  245. *
  246. * @param name is the name of thread, which shall be unique.
  247. *
  248. * @param entry is the entry function of thread.
  249. *
  250. * @param parameter is the parameter of thread enter function.
  251. *
  252. * @param stack_start is the start address of thread stack.
  253. *
  254. * @param stack_size is the size of thread stack.
  255. *
  256. * @param priority is the priority of thread.
  257. *
  258. * @param tick is the time slice if there are same priority thread.
  259. *
  260. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  261. * If the return value is any other values, it means this operation failed.
  262. */
  263. rt_err_t rt_thread_init(struct rt_thread *thread,
  264. const char *name,
  265. void (*entry)(void *parameter),
  266. void *parameter,
  267. void *stack_start,
  268. rt_uint32_t stack_size,
  269. rt_uint8_t priority,
  270. rt_uint32_t tick)
  271. {
  272. /* parameter check */
  273. RT_ASSERT(thread != RT_NULL);
  274. RT_ASSERT(stack_start != RT_NULL);
  275. /* initialize thread object */
  276. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  277. return _thread_init(thread,
  278. name,
  279. entry,
  280. parameter,
  281. stack_start,
  282. stack_size,
  283. priority,
  284. tick);
  285. }
  286. RTM_EXPORT(rt_thread_init);
  287. /**
  288. * @brief This function will return self thread object.
  289. *
  290. * @return The self thread object.
  291. */
  292. rt_thread_t rt_thread_self(void)
  293. {
  294. #ifdef RT_USING_SMP
  295. rt_base_t lock;
  296. rt_thread_t self;
  297. lock = rt_hw_local_irq_disable();
  298. self = rt_cpu_self()->current_thread;
  299. rt_hw_local_irq_enable(lock);
  300. return self;
  301. #else
  302. extern rt_thread_t rt_current_thread;
  303. return rt_current_thread;
  304. #endif /* RT_USING_SMP */
  305. }
  306. RTM_EXPORT(rt_thread_self);
  307. /**
  308. * @brief This function will start a thread and put it to system ready queue.
  309. *
  310. * @param thread is the thread to be started.
  311. *
  312. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  313. * If the return value is any other values, it means this operation failed.
  314. */
  315. rt_err_t rt_thread_startup(rt_thread_t thread)
  316. {
  317. /* parameter check */
  318. RT_ASSERT(thread != RT_NULL);
  319. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  320. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  321. /* calculate priority attribute */
  322. #if RT_THREAD_PRIORITY_MAX > 32
  323. thread->number = thread->current_priority >> 3; /* 5bit */
  324. thread->number_mask = 1L << thread->number;
  325. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  326. #else
  327. thread->number_mask = 1L << thread->current_priority;
  328. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  329. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  330. thread->parent.name, thread->current_priority));
  331. /* change thread stat */
  332. thread->stat = RT_THREAD_SUSPEND;
  333. /* then resume it */
  334. rt_thread_resume(thread);
  335. if (rt_thread_self() != RT_NULL)
  336. {
  337. /* do a scheduling */
  338. rt_schedule();
  339. }
  340. return RT_EOK;
  341. }
  342. RTM_EXPORT(rt_thread_startup);
  343. /**
  344. * @brief This function will detach a thread. The thread object will be removed from
  345. * thread queue and detached/deleted from the system object management.
  346. *
  347. * @param thread is the thread to be deleted.
  348. *
  349. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  350. * If the return value is any other values, it means this operation failed.
  351. */
  352. rt_err_t rt_thread_detach(rt_thread_t thread)
  353. {
  354. rt_base_t level;
  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. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  360. return RT_EOK;
  361. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  362. {
  363. /* remove from schedule */
  364. rt_schedule_remove_thread(thread);
  365. }
  366. /* disable interrupt */
  367. level = rt_hw_interrupt_disable();
  368. /* release thread timer */
  369. rt_timer_detach(&(thread->thread_timer));
  370. /* change stat */
  371. thread->stat = RT_THREAD_CLOSE;
  372. #ifdef RT_USING_MUTEX
  373. if ((thread->pending_object) &&
  374. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  375. {
  376. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  377. rt_mutex_drop_thread(mutex, thread);
  378. thread->pending_object = RT_NULL;
  379. }
  380. #endif
  381. /* insert to defunct thread list */
  382. rt_thread_defunct_enqueue(thread);
  383. /* enable interrupt */
  384. rt_hw_interrupt_enable(level);
  385. return RT_EOK;
  386. }
  387. RTM_EXPORT(rt_thread_detach);
  388. #ifdef RT_USING_HEAP
  389. /**
  390. * @brief This function will create a thread object and allocate thread object memory.
  391. * and stack.
  392. *
  393. * @param name is the name of thread, which shall be unique.
  394. *
  395. * @param entry is the entry function of thread.
  396. *
  397. * @param parameter is the parameter of thread enter function.
  398. *
  399. * @param stack_size is the size of thread stack.
  400. *
  401. * @param priority is the priority of thread.
  402. *
  403. * @param tick is the time slice if there are same priority thread.
  404. *
  405. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  406. * If the return value is RT_NULL, it means this operation failed.
  407. */
  408. rt_thread_t rt_thread_create(const char *name,
  409. void (*entry)(void *parameter),
  410. void *parameter,
  411. rt_uint32_t stack_size,
  412. rt_uint8_t priority,
  413. rt_uint32_t tick)
  414. {
  415. struct rt_thread *thread;
  416. void *stack_start;
  417. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  418. name);
  419. if (thread == RT_NULL)
  420. return RT_NULL;
  421. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  422. if (stack_start == RT_NULL)
  423. {
  424. /* allocate stack failure */
  425. rt_object_delete((rt_object_t)thread);
  426. return RT_NULL;
  427. }
  428. _thread_init(thread,
  429. name,
  430. entry,
  431. parameter,
  432. stack_start,
  433. stack_size,
  434. priority,
  435. tick);
  436. return thread;
  437. }
  438. RTM_EXPORT(rt_thread_create);
  439. /**
  440. * @brief This function will delete a thread. The thread object will be removed from
  441. * thread queue and deleted from system object management in the idle thread.
  442. *
  443. * @param thread is the thread to be deleted.
  444. *
  445. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  446. * If the return value is any other values, it means this operation failed.
  447. */
  448. rt_err_t rt_thread_delete(rt_thread_t thread)
  449. {
  450. rt_base_t level;
  451. /* parameter check */
  452. RT_ASSERT(thread != RT_NULL);
  453. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  454. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  455. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  456. return RT_EOK;
  457. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  458. {
  459. /* remove from schedule */
  460. rt_schedule_remove_thread(thread);
  461. }
  462. /* disable interrupt */
  463. level = rt_hw_interrupt_disable();
  464. /* release thread timer */
  465. rt_timer_detach(&(thread->thread_timer));
  466. /* change stat */
  467. thread->stat = RT_THREAD_CLOSE;
  468. #ifdef RT_USING_MUTEX
  469. if ((thread->pending_object) &&
  470. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  471. {
  472. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  473. rt_mutex_drop_thread(mutex, thread);
  474. thread->pending_object = RT_NULL;
  475. }
  476. #endif
  477. /* insert to defunct thread list */
  478. rt_thread_defunct_enqueue(thread);
  479. /* enable interrupt */
  480. rt_hw_interrupt_enable(level);
  481. return RT_EOK;
  482. }
  483. RTM_EXPORT(rt_thread_delete);
  484. #endif /* RT_USING_HEAP */
  485. /**
  486. * @brief This function will let current thread yield processor, and scheduler will
  487. * choose the highest thread to run. After yield processor, the current thread
  488. * is still in READY state.
  489. *
  490. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  491. * If the return value is any other values, it means this operation failed.
  492. */
  493. rt_err_t rt_thread_yield(void)
  494. {
  495. struct rt_thread *thread;
  496. rt_base_t level;
  497. thread = rt_thread_self();
  498. level = rt_hw_interrupt_disable();
  499. thread->remaining_tick = thread->init_tick;
  500. thread->stat |= RT_THREAD_STAT_YIELD;
  501. rt_schedule();
  502. rt_hw_interrupt_enable(level);
  503. return RT_EOK;
  504. }
  505. RTM_EXPORT(rt_thread_yield);
  506. /**
  507. * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend,
  508. * when the thread timer reaches the tick value, scheduler will awaken this thread.
  509. *
  510. * @param tick is the sleep ticks.
  511. *
  512. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  513. * If the return value is any other values, it means this operation failed.
  514. */
  515. rt_err_t rt_thread_sleep(rt_tick_t tick)
  516. {
  517. rt_base_t level;
  518. struct rt_thread *thread;
  519. int err;
  520. if (tick == 0)
  521. {
  522. return -RT_EINVAL;
  523. }
  524. /* set to current thread */
  525. thread = rt_thread_self();
  526. RT_ASSERT(thread != RT_NULL);
  527. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  528. /* current context checking */
  529. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  530. /* disable interrupt */
  531. level = rt_hw_interrupt_disable();
  532. /* reset thread error */
  533. thread->error = RT_EOK;
  534. /* suspend thread */
  535. err = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  536. /* reset the timeout of thread timer and start it */
  537. if (err == RT_EOK)
  538. {
  539. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  540. rt_timer_start(&(thread->thread_timer));
  541. /* enable interrupt */
  542. rt_hw_interrupt_enable(level);
  543. thread->error = -RT_EINTR;
  544. rt_schedule();
  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_hw_interrupt_enable(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 rt_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. rt_base_t level;
  581. struct rt_thread *thread;
  582. rt_tick_t cur_tick;
  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. /* disable interrupt */
  589. level = rt_hw_interrupt_disable();
  590. /* reset thread error */
  591. thread->error = RT_EOK;
  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. /* enable interrupt */
  604. rt_hw_interrupt_enable(level);
  605. rt_schedule();
  606. /* clear error number of this thread to RT_EOK */
  607. if (thread->error == -RT_ETIMEOUT)
  608. {
  609. thread->error = RT_EOK;
  610. }
  611. }
  612. else
  613. {
  614. *tick = cur_tick;
  615. rt_hw_interrupt_enable(level);
  616. }
  617. return thread->error;
  618. }
  619. RTM_EXPORT(rt_thread_delay_until);
  620. /**
  621. * @brief This function will let current thread delay for some milliseconds.
  622. *
  623. * @param ms is the delay ms time.
  624. *
  625. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  626. * If the return value is any other values, it means this operation failed.
  627. */
  628. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  629. {
  630. rt_tick_t tick;
  631. tick = rt_tick_from_millisecond(ms);
  632. return rt_thread_sleep(tick);
  633. }
  634. RTM_EXPORT(rt_thread_mdelay);
  635. #ifdef RT_USING_SMP
  636. static void rt_thread_cpu_bind(rt_thread_t thread, int cpu)
  637. {
  638. rt_base_t level;
  639. if (cpu >= RT_CPUS_NR)
  640. {
  641. cpu = RT_CPUS_NR;
  642. }
  643. level = rt_hw_interrupt_disable();
  644. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  645. {
  646. /* unbind */
  647. /* remove from old ready queue */
  648. rt_schedule_remove_thread(thread);
  649. /* change thread bind cpu */
  650. thread->bind_cpu = cpu;
  651. /* add to new ready queue */
  652. rt_schedule_insert_thread(thread);
  653. if (rt_thread_self() != RT_NULL)
  654. {
  655. rt_schedule();
  656. }
  657. }
  658. else
  659. {
  660. thread->bind_cpu = cpu;
  661. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  662. {
  663. /* thread is running on a cpu */
  664. int current_cpu = rt_hw_cpu_id();
  665. if (cpu != RT_CPUS_NR)
  666. {
  667. if (thread->oncpu == current_cpu)
  668. {
  669. /* current thread on current cpu */
  670. if (cpu != current_cpu)
  671. {
  672. /* bind to other cpu */
  673. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << cpu);
  674. /* self cpu need reschedule */
  675. rt_schedule();
  676. }
  677. /* else do nothing */
  678. }
  679. else
  680. {
  681. /* no running on self cpu, but dest cpu can be itself */
  682. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << thread->oncpu);
  683. }
  684. }
  685. /* else do nothing */
  686. }
  687. }
  688. rt_hw_interrupt_enable(level);
  689. }
  690. #endif
  691. /**
  692. * @brief This function will control thread behaviors according to control command.
  693. *
  694. * @param thread is the specified thread to be controlled.
  695. *
  696. * @param cmd is the control command, which includes.
  697. *
  698. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
  699. *
  700. * RT_THREAD_CTRL_STARTUP for starting a thread.
  701. *
  702. * RT_THREAD_CTRL_CLOSE for delete a thread.
  703. *
  704. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  705. *
  706. * @param arg is the argument of control command.
  707. *
  708. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  709. * If the return value is any other values, it means this operation failed.
  710. */
  711. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  712. {
  713. rt_base_t level;
  714. /* parameter check */
  715. RT_ASSERT(thread != RT_NULL);
  716. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  717. switch (cmd)
  718. {
  719. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  720. {
  721. /* disable interrupt */
  722. level = rt_hw_interrupt_disable();
  723. /* for ready thread, change queue */
  724. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  725. {
  726. /* remove thread from schedule queue first */
  727. rt_schedule_remove_thread(thread);
  728. /* change thread priority */
  729. thread->current_priority = *(rt_uint8_t *)arg;
  730. /* recalculate priority attribute */
  731. #if RT_THREAD_PRIORITY_MAX > 32
  732. thread->number = thread->current_priority >> 3; /* 5bit */
  733. thread->number_mask = 1 << thread->number;
  734. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  735. #else
  736. thread->number_mask = 1 << thread->current_priority;
  737. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  738. /* insert thread to schedule queue again */
  739. rt_schedule_insert_thread(thread);
  740. }
  741. else
  742. {
  743. thread->current_priority = *(rt_uint8_t *)arg;
  744. /* recalculate priority attribute */
  745. #if RT_THREAD_PRIORITY_MAX > 32
  746. thread->number = thread->current_priority >> 3; /* 5bit */
  747. thread->number_mask = 1 << thread->number;
  748. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  749. #else
  750. thread->number_mask = 1 << thread->current_priority;
  751. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  752. }
  753. /* enable interrupt */
  754. rt_hw_interrupt_enable(level);
  755. break;
  756. }
  757. case RT_THREAD_CTRL_STARTUP:
  758. {
  759. return rt_thread_startup(thread);
  760. }
  761. case RT_THREAD_CTRL_CLOSE:
  762. {
  763. rt_err_t rt_err = -RT_EINVAL;
  764. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  765. {
  766. rt_err = rt_thread_detach(thread);
  767. }
  768. #ifdef RT_USING_HEAP
  769. else
  770. {
  771. rt_err = rt_thread_delete(thread);
  772. }
  773. #endif /* RT_USING_HEAP */
  774. rt_schedule();
  775. return rt_err;
  776. }
  777. #ifdef RT_USING_SMP
  778. case RT_THREAD_CTRL_BIND_CPU:
  779. {
  780. rt_uint8_t cpu;
  781. cpu = (rt_uint8_t)(size_t)arg;
  782. rt_thread_cpu_bind(thread, cpu);
  783. break;
  784. }
  785. #endif /*RT_USING_SMP*/
  786. default:
  787. break;
  788. }
  789. return RT_EOK;
  790. }
  791. RTM_EXPORT(rt_thread_control);
  792. #ifdef RT_USING_SMART
  793. int lwp_suspend_sigcheck(rt_thread_t thread, int suspend_flag);
  794. #endif
  795. static void rt_thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
  796. {
  797. rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  798. RT_ASSERT(thread != RT_NULL);
  799. switch (suspend_flag)
  800. {
  801. case RT_INTERRUPTIBLE:
  802. stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
  803. break;
  804. case RT_KILLABLE:
  805. stat = RT_THREAD_SUSPEND_KILLABLE;
  806. break;
  807. case RT_UNINTERRUPTIBLE:
  808. stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  809. break;
  810. default:
  811. RT_ASSERT(0);
  812. break;
  813. }
  814. thread->stat = stat | (thread->stat & ~RT_THREAD_STAT_MASK);
  815. }
  816. /**
  817. * @brief This function will suspend the specified thread and change it to suspend state.
  818. *
  819. * @note This function ONLY can suspend current thread itself.
  820. * rt_thread_suspend(rt_thread_self());
  821. *
  822. * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
  823. * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
  824. * other threads and occupying this resouce, starvation can occur very easily.
  825. *
  826. * @param thread the thread to be suspended.
  827. * @param suspend_flag status flag of the thread to be suspended.
  828. *
  829. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  830. * If the return value is any other values, it means this operation failed.
  831. */
  832. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  833. {
  834. rt_base_t stat;
  835. rt_base_t level;
  836. /* parameter check */
  837. RT_ASSERT(thread != RT_NULL);
  838. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  839. RT_ASSERT(thread == rt_thread_self());
  840. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->parent.name));
  841. stat = thread->stat & RT_THREAD_STAT_MASK;
  842. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  843. {
  844. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n", thread->stat));
  845. return -RT_ERROR;
  846. }
  847. /* disable interrupt */
  848. level = rt_hw_interrupt_disable();
  849. if (stat == RT_THREAD_RUNNING)
  850. {
  851. /* not suspend running status thread on other core */
  852. RT_ASSERT(thread == rt_thread_self());
  853. }
  854. #ifdef RT_USING_SMART
  855. if (lwp_suspend_sigcheck(thread, suspend_flag) == 0)
  856. {
  857. /* not to suspend */
  858. rt_hw_interrupt_enable(level);
  859. rt_kprintf("-RT_EINTR\r\n");
  860. return -RT_EINTR;
  861. }
  862. #endif
  863. /* change thread stat */
  864. rt_schedule_remove_thread(thread);
  865. rt_thread_set_suspend_state(thread, suspend_flag);
  866. /* stop thread timer anyway */
  867. rt_timer_stop(&(thread->thread_timer));
  868. /* enable interrupt */
  869. rt_hw_interrupt_enable(level);
  870. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  871. return RT_EOK;
  872. }
  873. RTM_EXPORT(rt_thread_suspend_with_flag);
  874. rt_err_t rt_thread_suspend(rt_thread_t thread)
  875. {
  876. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  877. }
  878. RTM_EXPORT(rt_thread_suspend);
  879. /**
  880. * @brief This function will resume a thread and put it to system ready queue.
  881. *
  882. * @param thread is the thread to be resumed.
  883. *
  884. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  885. * If the return value is any other values, it means this operation failed.
  886. */
  887. rt_err_t rt_thread_resume(rt_thread_t thread)
  888. {
  889. rt_base_t level;
  890. /* parameter check */
  891. RT_ASSERT(thread != RT_NULL);
  892. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  893. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->parent.name));
  894. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  895. {
  896. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  897. thread->stat));
  898. return -RT_ERROR;
  899. }
  900. /* disable interrupt */
  901. level = rt_hw_interrupt_disable();
  902. /* remove from suspend list */
  903. rt_list_remove(&(thread->tlist));
  904. rt_timer_stop(&thread->thread_timer);
  905. #ifdef RT_USING_SMART
  906. thread->wakeup.func = RT_NULL;
  907. #endif
  908. /* enable interrupt */
  909. rt_hw_interrupt_enable(level);
  910. /* insert to schedule ready list */
  911. rt_schedule_insert_thread(thread);
  912. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  913. return RT_EOK;
  914. }
  915. RTM_EXPORT(rt_thread_resume);
  916. #ifdef RT_USING_SMART
  917. /**
  918. * This function will wakeup a thread with customized operation.
  919. *
  920. * @param thread the thread to be resumed
  921. *
  922. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  923. */
  924. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  925. {
  926. register rt_base_t temp;
  927. rt_err_t ret;
  928. RT_ASSERT(thread != RT_NULL);
  929. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  930. /* disable interrupt */
  931. temp = rt_hw_interrupt_disable();
  932. if (thread->wakeup.func)
  933. {
  934. ret = thread->wakeup.func(thread->wakeup.user_data, thread);
  935. thread->wakeup.func = RT_NULL;
  936. }
  937. else
  938. {
  939. ret = rt_thread_resume(thread);
  940. }
  941. rt_hw_interrupt_enable(temp);
  942. return ret;
  943. }
  944. RTM_EXPORT(rt_thread_wakeup);
  945. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  946. {
  947. register rt_base_t temp;
  948. RT_ASSERT(thread != RT_NULL);
  949. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  950. temp = rt_hw_interrupt_disable();
  951. thread->wakeup.func = func;
  952. thread->wakeup.user_data = user_data;
  953. rt_hw_interrupt_enable(temp);
  954. }
  955. RTM_EXPORT(rt_thread_wakeup_set);
  956. #endif
  957. /**
  958. * @brief This function will find the specified thread.
  959. *
  960. * @note Please don't invoke this function in interrupt status.
  961. *
  962. * @param name is the name of thread finding.
  963. *
  964. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  965. * If the return value is RT_NULL, it means this operation failed.
  966. */
  967. rt_thread_t rt_thread_find(char *name)
  968. {
  969. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  970. }
  971. RTM_EXPORT(rt_thread_find);
  972. /**@}*/