thread.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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_LWP
  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->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_LWP
  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->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->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;
  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_LWP
  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 is the thread to be suspended.
  827. *
  828. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  829. * If the return value is any other values, it means this operation failed.
  830. */
  831. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  832. {
  833. rt_base_t stat;
  834. rt_base_t level;
  835. /* parameter check */
  836. RT_ASSERT(thread != RT_NULL);
  837. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  838. RT_ASSERT(thread == rt_thread_self());
  839. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  840. stat = thread->stat & RT_THREAD_STAT_MASK;
  841. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  842. {
  843. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n", thread->stat));
  844. return -RT_ERROR;
  845. }
  846. /* disable interrupt */
  847. level = rt_hw_interrupt_disable();
  848. if (stat == RT_THREAD_RUNNING)
  849. {
  850. /* not suspend running status thread on other core */
  851. RT_ASSERT(thread == rt_thread_self());
  852. }
  853. #ifdef RT_USING_LWP
  854. if (lwp_suspend_sigcheck(thread, suspend_flag) == 0)
  855. {
  856. /* not to suspend */
  857. rt_hw_interrupt_enable(level);
  858. rt_kprintf("-RT_EINTR\r\n");
  859. return -RT_EINTR;
  860. }
  861. #endif
  862. /* change thread stat */
  863. rt_schedule_remove_thread(thread);
  864. rt_thread_set_suspend_state(thread, suspend_flag);
  865. /* stop thread timer anyway */
  866. rt_timer_stop(&(thread->thread_timer));
  867. /* enable interrupt */
  868. rt_hw_interrupt_enable(level);
  869. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  870. return RT_EOK;
  871. }
  872. RTM_EXPORT(rt_thread_suspend_with_flag);
  873. rt_err_t rt_thread_suspend(rt_thread_t thread)
  874. {
  875. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  876. }
  877. RTM_EXPORT(rt_thread_suspend);
  878. /**
  879. * @brief This function will resume a thread and put it to system ready queue.
  880. *
  881. * @param thread is the thread to be resumed.
  882. *
  883. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  884. * If the return value is any other values, it means this operation failed.
  885. */
  886. rt_err_t rt_thread_resume(rt_thread_t thread)
  887. {
  888. rt_base_t level;
  889. /* parameter check */
  890. RT_ASSERT(thread != RT_NULL);
  891. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  892. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  893. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  894. {
  895. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  896. thread->stat));
  897. return -RT_ERROR;
  898. }
  899. /* disable interrupt */
  900. level = rt_hw_interrupt_disable();
  901. /* remove from suspend list */
  902. rt_list_remove(&(thread->tlist));
  903. rt_timer_stop(&thread->thread_timer);
  904. #ifdef RT_USING_LWP
  905. thread->wakeup.func = RT_NULL;
  906. #endif
  907. /* enable interrupt */
  908. rt_hw_interrupt_enable(level);
  909. /* insert to schedule ready list */
  910. rt_schedule_insert_thread(thread);
  911. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  912. return RT_EOK;
  913. }
  914. RTM_EXPORT(rt_thread_resume);
  915. #ifdef RT_USING_LWP
  916. /**
  917. * This function will wakeup a thread with customized operation.
  918. *
  919. * @param thread the thread to be resumed
  920. *
  921. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  922. */
  923. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  924. {
  925. register rt_base_t temp;
  926. rt_err_t ret;
  927. RT_ASSERT(thread != RT_NULL);
  928. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  929. /* disable interrupt */
  930. temp = rt_hw_interrupt_disable();
  931. if (thread->wakeup.func)
  932. {
  933. ret = thread->wakeup.func(thread->wakeup.user_data, thread);
  934. thread->wakeup.func = RT_NULL;
  935. }
  936. else
  937. {
  938. ret = rt_thread_resume(thread);
  939. }
  940. rt_hw_interrupt_enable(temp);
  941. return ret;
  942. }
  943. RTM_EXPORT(rt_thread_wakeup);
  944. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  945. {
  946. register rt_base_t temp;
  947. RT_ASSERT(thread != RT_NULL);
  948. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  949. temp = rt_hw_interrupt_disable();
  950. thread->wakeup.func = func;
  951. thread->wakeup.user_data = user_data;
  952. rt_hw_interrupt_enable(temp);
  953. }
  954. RTM_EXPORT(rt_thread_wakeup_set);
  955. #endif
  956. /**
  957. * @brief This function will find the specified thread.
  958. *
  959. * @note Please don't invoke this function in interrupt status.
  960. *
  961. * @param name is the name of thread finding.
  962. *
  963. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  964. * If the return value is RT_NULL, it means this operation failed.
  965. */
  966. rt_thread_t rt_thread_find(char *name)
  967. {
  968. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  969. }
  970. RTM_EXPORT(rt_thread_find);
  971. /**@}*/