1
0

thread.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  36. */
  37. #include <rthw.h>
  38. #include <rtthread.h>
  39. #include <stddef.h>
  40. #define DBG_TAG "kernel.thread"
  41. #define DBG_LVL DBG_INFO
  42. #include <rtdbg.h>
  43. #ifndef __on_rt_thread_inited_hook
  44. #define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
  45. #endif
  46. #ifndef __on_rt_thread_suspend_hook
  47. #define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
  48. #endif
  49. #ifndef __on_rt_thread_resume_hook
  50. #define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
  51. #endif
  52. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  53. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  54. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  55. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  56. /**
  57. * @brief This function sets a hook function when the system suspend a thread.
  58. *
  59. * @note The hook function must be simple and never be blocked or suspend.
  60. *
  61. * @param hook is the specified hook function.
  62. */
  63. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  64. {
  65. rt_thread_suspend_hook = hook;
  66. }
  67. /**
  68. * @brief This function sets a hook function when the system resume a thread.
  69. *
  70. * @note The hook function must be simple and never be blocked or suspend.
  71. *
  72. * @param hook is the specified hook function.
  73. */
  74. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  75. {
  76. rt_thread_resume_hook = hook;
  77. }
  78. /**
  79. * @brief This function sets a hook function when a thread is initialized.
  80. *
  81. * @param hook is the specified hook function.
  82. */
  83. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  84. {
  85. rt_thread_inited_hook = hook;
  86. }
  87. #endif /* defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) */
  88. static void _thread_exit(void)
  89. {
  90. struct rt_thread *thread;
  91. rt_base_t level;
  92. /* get current thread */
  93. LOG_D("line:%d thread:%s exit\n",__LINE__,rt_thread_self()->parent.name);
  94. thread = rt_thread_self();
  95. rt_get_thread_struct(thread);
  96. rt_thread_defunct_enqueue(thread);
  97. level = rt_spin_lock_irqsave(&(thread->spinlock));
  98. rt_timer_detach(&thread->thread_timer);
  99. /* insert to defunct thread list */
  100. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  101. LOG_D("line:%d thread:%s exit\n",__LINE__,rt_thread_self()->parent.name);
  102. rt_put_thread_struct(thread);
  103. thread->stat = RT_THREAD_CLOSE;
  104. /* switch to next task */
  105. rt_schedule();
  106. }
  107. /**
  108. * @brief This function is the timeout function for thread, normally which is invoked
  109. * when thread is timeout to wait some resource.
  110. *
  111. * @param parameter is the parameter of thread timeout function
  112. */
  113. static void _thread_timeout(void *parameter)
  114. {
  115. struct rt_thread *thread;
  116. rt_base_t level;
  117. thread = (struct rt_thread *)parameter;
  118. /* parameter check */
  119. RT_ASSERT(thread != RT_NULL);
  120. RT_ASSERT((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK);
  121. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  122. level = rt_spin_lock_irqsave(&(thread->spinlock));
  123. /* set error number */
  124. thread->error = -RT_ETIMEOUT;
  125. /* remove from suspend list */
  126. rt_list_remove(&(thread->tlist));
  127. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  128. /* insert to schedule ready list */
  129. rt_schedule_insert_thread(thread);
  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. rt_list_init(&(thread->tlist_schedule));
  145. #ifdef RT_USING_MEM_PROTECTION
  146. thread->mem_regions = RT_NULL;
  147. #endif
  148. #ifdef RT_USING_SMART
  149. thread->wakeup.func = RT_NULL;
  150. #endif
  151. thread->entry = (void *)entry;
  152. thread->parameter = parameter;
  153. /* stack init */
  154. thread->stack_addr = stack_start;
  155. thread->stack_size = stack_size;
  156. /* init thread stack */
  157. rt_memset(thread->stack_addr, '#', thread->stack_size);
  158. #ifdef RT_USING_HW_STACK_GUARD
  159. rt_hw_stack_guard_init(thread);
  160. #endif
  161. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  162. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  163. (void *)((char *)thread->stack_addr),
  164. (void *)_thread_exit);
  165. #else
  166. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  167. (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
  168. (void *)_thread_exit);
  169. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  170. /* priority init */
  171. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  172. thread->init_priority = priority;
  173. thread->current_priority = priority;
  174. thread->number_mask = 0;
  175. #ifdef RT_USING_MUTEX
  176. rt_list_init(&thread->taken_object_list);
  177. thread->pending_object = RT_NULL;
  178. #endif
  179. #ifdef RT_USING_EVENT
  180. thread->event_set = 0;
  181. thread->event_info = 0;
  182. #endif /* RT_USING_EVENT */
  183. #if RT_THREAD_PRIORITY_MAX > 32
  184. thread->number = 0;
  185. thread->high_mask = 0;
  186. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  187. /* tick init */
  188. rt_atomic_store(&thread->init_tick, tick);
  189. rt_atomic_store(&thread->remaining_tick, tick);
  190. /* error and flags */
  191. thread->error = RT_EOK;
  192. thread->stat = RT_THREAD_INIT;
  193. #ifdef RT_USING_SMP
  194. /* not bind on any cpu */
  195. thread->bind_cpu = RT_CPUS_NR;
  196. thread->oncpu = RT_CPU_DETACHED;
  197. /* lock init */
  198. rt_atomic_store(&thread->cpus_lock_nest, 0);
  199. rt_atomic_store(&thread->critical_lock_nest, 0);
  200. #endif /* RT_USING_SMP */
  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);
  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. rt_list_init(&(thread->sibling));
  226. /* lwp thread-signal init */
  227. rt_memset(&thread->signal.sigset_mask, 0, sizeof(lwp_sigset_t));
  228. rt_memset(&thread->signal.sig_queue.sigset_pending, 0, sizeof(lwp_sigset_t));
  229. rt_list_init(&thread->signal.sig_queue.siginfo_list);
  230. rt_memset(&thread->user_ctx, 0, sizeof thread->user_ctx);
  231. /* initialize user_time and system_time */
  232. thread->user_time = 0;
  233. thread->system_time = 0;
  234. #endif
  235. #ifdef RT_USING_CPU_USAGE
  236. thread->duration_tick = 0;
  237. #endif /* RT_USING_CPU_USAGE */
  238. #ifdef RT_USING_PTHREADS
  239. thread->pthread_data = RT_NULL;
  240. #endif /* RT_USING_PTHREADS */
  241. #ifdef RT_USING_MODULE
  242. thread->parent.module_id = 0;
  243. #endif /* RT_USING_MODULE */
  244. rt_atomic_store(&thread->ref_count, 0);
  245. rt_spin_lock_init(&thread->spinlock);
  246. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (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((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. /* calculate priority attribute */
  335. #if RT_THREAD_PRIORITY_MAX > 32
  336. thread->number = thread->current_priority >> 3; /* 5bit */
  337. thread->number_mask = 1L << thread->number;
  338. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  339. #else
  340. thread->number_mask = 1L << thread->current_priority;
  341. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  342. LOG_D("startup a thread:%s with priority:%d",
  343. thread->parent.name, thread->current_priority);
  344. /* change thread stat */
  345. thread->stat = RT_THREAD_SUSPEND;
  346. /* then resume it */
  347. rt_thread_resume(thread);
  348. if (rt_thread_self() != RT_NULL)
  349. {
  350. /* do a scheduling */
  351. rt_schedule();
  352. }
  353. return RT_EOK;
  354. }
  355. RTM_EXPORT(rt_thread_startup);
  356. /**
  357. * @brief This function will detach a thread. The thread object will be removed from
  358. * thread queue and detached/deleted from the system object management.
  359. *
  360. * @param thread is the thread to be deleted.
  361. *
  362. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  363. * If the return value is any other values, it means this operation failed.
  364. */
  365. rt_err_t rt_thread_detach(rt_thread_t thread)
  366. {
  367. rt_base_t level;
  368. /* parameter check */
  369. RT_ASSERT(thread != RT_NULL);
  370. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  371. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  372. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  373. return RT_EOK;
  374. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  375. {
  376. /* remove from schedule */
  377. rt_schedule_remove_thread(thread);
  378. }
  379. /* disable interrupt */
  380. level = rt_spin_lock_irqsave(&(thread->spinlock));
  381. /* release thread timer */
  382. rt_timer_detach(&(thread->thread_timer));
  383. /* change stat */
  384. thread->stat = RT_THREAD_CLOSE;
  385. #ifdef RT_USING_MUTEX
  386. if ((thread->pending_object) &&
  387. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  388. {
  389. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  390. rt_mutex_drop_thread(mutex, thread);
  391. thread->pending_object = RT_NULL;
  392. }
  393. #endif
  394. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  395. /* insert to defunct thread list */
  396. rt_thread_defunct_enqueue(thread);
  397. return RT_EOK;
  398. }
  399. RTM_EXPORT(rt_thread_detach);
  400. #ifdef RT_USING_HEAP
  401. /**
  402. * @brief This function will create a thread object and allocate thread object memory.
  403. * and stack.
  404. *
  405. * @param name is the name of thread, which shall be unique.
  406. *
  407. * @param entry is the entry function of thread.
  408. *
  409. * @param parameter is the parameter of thread enter function.
  410. *
  411. * @param stack_size is the size of thread stack.
  412. *
  413. * @param priority is the priority of thread.
  414. *
  415. * @param tick is the time slice if there are same priority thread.
  416. *
  417. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  418. * If the return value is RT_NULL, it means this operation failed.
  419. */
  420. rt_thread_t rt_thread_create(const char *name,
  421. void (*entry)(void *parameter),
  422. void *parameter,
  423. rt_uint32_t stack_size,
  424. rt_uint8_t priority,
  425. rt_uint32_t tick)
  426. {
  427. struct rt_thread *thread;
  428. void *stack_start;
  429. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  430. name);
  431. if (thread == RT_NULL)
  432. return RT_NULL;
  433. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  434. if (stack_start == RT_NULL)
  435. {
  436. /* allocate stack failure */
  437. rt_object_delete((rt_object_t)thread);
  438. return RT_NULL;
  439. }
  440. _thread_init(thread,
  441. name,
  442. entry,
  443. parameter,
  444. stack_start,
  445. stack_size,
  446. priority,
  447. tick);
  448. return thread;
  449. }
  450. RTM_EXPORT(rt_thread_create);
  451. /**
  452. * @brief This function will delete a thread. The thread object will be removed from
  453. * thread queue and deleted from system object management in the idle thread.
  454. *
  455. * @param thread is the thread to be deleted.
  456. *
  457. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  458. * If the return value is any other values, it means this operation failed.
  459. */
  460. rt_err_t rt_thread_delete(rt_thread_t thread)
  461. {
  462. rt_base_t level;
  463. /* parameter check */
  464. RT_ASSERT(thread != RT_NULL);
  465. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  466. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  467. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  468. return RT_EOK;
  469. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  470. {
  471. /* remove from schedule */
  472. rt_schedule_remove_thread(thread);
  473. }
  474. level = rt_spin_lock_irqsave(&(thread->spinlock));
  475. /* release thread timer */
  476. rt_timer_detach(&(thread->thread_timer));
  477. /* change stat */
  478. thread->stat = RT_THREAD_CLOSE;
  479. #ifdef RT_USING_MUTEX
  480. if ((thread->pending_object) &&
  481. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  482. {
  483. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  484. rt_mutex_drop_thread(mutex, thread);
  485. thread->pending_object = RT_NULL;
  486. }
  487. #endif
  488. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  489. /* insert to defunct thread list */
  490. rt_thread_defunct_enqueue(thread);
  491. return RT_EOK;
  492. }
  493. RTM_EXPORT(rt_thread_delete);
  494. #endif /* RT_USING_HEAP */
  495. /**
  496. * @brief This function will let current thread yield processor, and scheduler will
  497. * choose the highest thread to run. After yield processor, the current thread
  498. * is still in READY state.
  499. *
  500. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  501. * If the return value is any other values, it means this operation failed.
  502. */
  503. rt_err_t rt_thread_yield(void)
  504. {
  505. struct rt_thread *thread;
  506. rt_base_t level;
  507. thread = rt_thread_self();
  508. level = rt_spin_lock_irqsave(&(thread->spinlock));
  509. rt_atomic_store(&thread->remaining_tick, thread->init_tick);
  510. thread->stat |= RT_THREAD_STAT_YIELD;
  511. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  512. rt_schedule();
  513. return RT_EOK;
  514. }
  515. RTM_EXPORT(rt_thread_yield);
  516. /**
  517. * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend,
  518. * when the thread timer reaches the tick value, scheduler will awaken this thread.
  519. *
  520. * @param tick is the sleep ticks.
  521. *
  522. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  523. * If the return value is any other values, it means this operation failed.
  524. */
  525. rt_err_t rt_thread_sleep(rt_tick_t tick)
  526. {
  527. rt_base_t level, level_local;
  528. struct rt_thread *thread;
  529. int err;
  530. if (tick == 0)
  531. {
  532. return -RT_EINVAL;
  533. }
  534. /* set to current thread */
  535. thread = rt_thread_self();
  536. RT_ASSERT(thread != RT_NULL);
  537. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  538. /* current context checking */
  539. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  540. /* reset thread error */
  541. thread->error = RT_EOK;
  542. level_local = rt_hw_local_irq_disable();
  543. /* suspend thread */
  544. err = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  545. level = rt_spin_lock_irqsave(&(thread->spinlock));
  546. /* reset the timeout of thread timer and start it */
  547. if (err == RT_EOK)
  548. {
  549. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  550. rt_timer_start(&(thread->thread_timer));
  551. /* enable interrupt */
  552. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  553. rt_hw_local_irq_enable(level_local);
  554. thread->error = -RT_EINTR;
  555. rt_schedule();
  556. /* clear error number of this thread to RT_EOK */
  557. if (thread->error == -RT_ETIMEOUT)
  558. thread->error = RT_EOK;
  559. }
  560. else
  561. {
  562. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  563. rt_hw_local_irq_enable(level_local);
  564. }
  565. return err;
  566. }
  567. /**
  568. * @brief This function will let current thread delay for some ticks.
  569. *
  570. * @param tick is the delay ticks.
  571. *
  572. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  573. * If the return value is any other values, it means this operation failed.
  574. */
  575. rt_err_t rt_thread_delay(rt_tick_t tick)
  576. {
  577. return rt_thread_sleep(tick);
  578. }
  579. RTM_EXPORT(rt_thread_delay);
  580. /**
  581. * @brief This function will let current thread delay until (*tick + inc_tick).
  582. *
  583. * @param tick is the tick of last wakeup.
  584. *
  585. * @param inc_tick is the increment tick.
  586. *
  587. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  588. * If the return value is any other values, it means this operation failed.
  589. */
  590. rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
  591. {
  592. rt_base_t level;
  593. struct rt_thread *thread;
  594. rt_tick_t cur_tick;
  595. RT_ASSERT(tick != RT_NULL);
  596. /* set to current thread */
  597. thread = rt_thread_self();
  598. RT_ASSERT(thread != RT_NULL);
  599. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  600. /* disable interrupt */
  601. level = rt_spin_lock_irqsave(&(thread->spinlock));
  602. /* reset thread error */
  603. thread->error = RT_EOK;
  604. cur_tick = rt_tick_get();
  605. if (cur_tick - *tick < inc_tick)
  606. {
  607. rt_tick_t left_tick;
  608. *tick += inc_tick;
  609. left_tick = *tick - cur_tick;
  610. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  611. /* suspend thread */
  612. rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  613. level = rt_spin_lock_irqsave(&(thread->spinlock));
  614. /* reset the timeout of thread timer and start it */
  615. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick);
  616. rt_timer_start(&(thread->thread_timer));
  617. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  618. rt_schedule();
  619. /* clear error number of this thread to RT_EOK */
  620. if (thread->error == -RT_ETIMEOUT)
  621. {
  622. thread->error = RT_EOK;
  623. }
  624. }
  625. else
  626. {
  627. *tick = cur_tick;
  628. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  629. }
  630. return thread->error;
  631. }
  632. RTM_EXPORT(rt_thread_delay_until);
  633. /**
  634. * @brief This function will let current thread delay for some milliseconds.
  635. *
  636. * @param ms is the delay ms time.
  637. *
  638. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  639. * If the return value is any other values, it means this operation failed.
  640. */
  641. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  642. {
  643. rt_tick_t tick;
  644. tick = rt_tick_from_millisecond(ms);
  645. return rt_thread_sleep(tick);
  646. }
  647. RTM_EXPORT(rt_thread_mdelay);
  648. #ifdef RT_USING_SMP
  649. static void rt_thread_cpu_bind(rt_thread_t thread, int cpu)
  650. {
  651. rt_base_t level;
  652. if (cpu >= RT_CPUS_NR)
  653. {
  654. cpu = RT_CPUS_NR;
  655. }
  656. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  657. {
  658. /* unbind */
  659. /* remove from old ready queue */
  660. rt_schedule_remove_thread(thread);
  661. /* change thread bind cpu */
  662. thread->bind_cpu = cpu;
  663. /* add to new ready queue */
  664. rt_schedule_insert_thread(thread);
  665. if (rt_thread_self() != RT_NULL)
  666. {
  667. rt_schedule();
  668. }
  669. }
  670. else
  671. {
  672. level = rt_spin_lock_irqsave(&(thread->spinlock));
  673. thread->bind_cpu = cpu;
  674. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  675. {
  676. /* thread is running on a cpu */
  677. int current_cpu = rt_hw_cpu_id();
  678. if (cpu != RT_CPUS_NR)
  679. {
  680. if (thread->oncpu == current_cpu)
  681. {
  682. /* current thread on current cpu */
  683. if (cpu != current_cpu)
  684. {
  685. /* bind to other cpu */
  686. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << cpu);
  687. /* self cpu need reschedule */
  688. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  689. rt_schedule();
  690. level = rt_spin_lock_irqsave(&(thread->spinlock));
  691. }
  692. /* else do nothing */
  693. }
  694. else
  695. {
  696. /* no running on self cpu, but dest cpu can be itself */
  697. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << thread->oncpu);
  698. }
  699. }
  700. /* else do nothing */
  701. }
  702. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  703. }
  704. }
  705. #endif
  706. /**
  707. * @brief This function will control thread behaviors according to control command.
  708. *
  709. * @param thread is the specified thread to be controlled.
  710. *
  711. * @param cmd is the control command, which includes.
  712. *
  713. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
  714. *
  715. * RT_THREAD_CTRL_STARTUP for starting a thread.
  716. *
  717. * RT_THREAD_CTRL_CLOSE for delete a thread.
  718. *
  719. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  720. *
  721. * @param arg is the argument of control command.
  722. *
  723. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  724. * If the return value is any other values, it means this operation failed.
  725. */
  726. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  727. {
  728. rt_base_t level;
  729. /* parameter check */
  730. RT_ASSERT(thread != RT_NULL);
  731. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  732. switch (cmd)
  733. {
  734. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  735. {
  736. /* for ready thread, change queue */
  737. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  738. {
  739. /* remove thread from schedule queue first */
  740. rt_schedule_remove_thread(thread);
  741. level = rt_spin_lock_irqsave(&(thread->spinlock));
  742. /* change thread priority */
  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. thread->stat = RT_THREAD_INIT;
  753. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  754. /* insert thread to schedule queue again */
  755. rt_schedule_insert_thread(thread);
  756. }
  757. else
  758. {
  759. level = rt_spin_lock_irqsave(&(thread->spinlock));
  760. thread->current_priority = *(rt_uint8_t *)arg;
  761. /* recalculate priority attribute */
  762. #if RT_THREAD_PRIORITY_MAX > 32
  763. thread->number = thread->current_priority >> 3; /* 5bit */
  764. thread->number_mask = 1 << thread->number;
  765. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  766. #else
  767. thread->number_mask = 1 << thread->current_priority;
  768. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  769. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  770. }
  771. break;
  772. }
  773. case RT_THREAD_CTRL_STARTUP:
  774. {
  775. return rt_thread_startup(thread);
  776. }
  777. case RT_THREAD_CTRL_CLOSE:
  778. {
  779. rt_err_t rt_err = -RT_EINVAL;
  780. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  781. {
  782. rt_err = rt_thread_detach(thread);
  783. }
  784. #ifdef RT_USING_HEAP
  785. else
  786. {
  787. rt_err = rt_thread_delete(thread);
  788. }
  789. #endif /* RT_USING_HEAP */
  790. rt_schedule();
  791. return rt_err;
  792. }
  793. #ifdef RT_USING_SMP
  794. case RT_THREAD_CTRL_BIND_CPU:
  795. {
  796. rt_uint8_t cpu;
  797. cpu = (rt_uint8_t)(size_t)arg;
  798. rt_thread_cpu_bind(thread, cpu);
  799. break;
  800. }
  801. #endif /*RT_USING_SMP*/
  802. default:
  803. break;
  804. }
  805. return RT_EOK;
  806. }
  807. RTM_EXPORT(rt_thread_control);
  808. #ifdef RT_USING_SMART
  809. #include <lwp_signal.h>
  810. #endif
  811. static void rt_thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
  812. {
  813. rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  814. RT_ASSERT(thread != RT_NULL);
  815. switch (suspend_flag)
  816. {
  817. case RT_INTERRUPTIBLE:
  818. stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
  819. break;
  820. case RT_KILLABLE:
  821. stat = RT_THREAD_SUSPEND_KILLABLE;
  822. break;
  823. case RT_UNINTERRUPTIBLE:
  824. stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  825. break;
  826. default:
  827. RT_ASSERT(0);
  828. break;
  829. }
  830. thread->stat = stat | (thread->stat & ~RT_THREAD_STAT_MASK);
  831. }
  832. /**
  833. * @brief This function will suspend the specified thread and change it to suspend state.
  834. *
  835. * @note This function ONLY can suspend current thread itself.
  836. * rt_thread_suspend(rt_thread_self());
  837. *
  838. * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
  839. * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
  840. * other threads and occupying this resouce, starvation can occur very easily.
  841. *
  842. * @param thread the thread to be suspended.
  843. * @param suspend_flag status flag of the thread to be suspended.
  844. *
  845. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  846. * If the return value is any other values, it means this operation failed.
  847. */
  848. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  849. {
  850. rt_base_t stat;
  851. rt_base_t level;
  852. /* parameter check */
  853. RT_ASSERT(thread != RT_NULL);
  854. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  855. RT_ASSERT(thread == rt_thread_self());
  856. LOG_D("thread suspend: %s", thread->parent.name);
  857. level = rt_spin_lock_irqsave(&(thread->spinlock));
  858. stat = thread->stat & RT_THREAD_STAT_MASK;
  859. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  860. {
  861. LOG_D("thread suspend: thread disorder, 0x%2x", thread->stat);
  862. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  863. return -RT_ERROR;
  864. }
  865. if (stat == RT_THREAD_RUNNING)
  866. {
  867. /* not suspend running status thread on other core */
  868. RT_ASSERT(thread == rt_thread_self());
  869. }
  870. #ifdef RT_USING_SMART
  871. if (lwp_thread_signal_suspend_check(thread, suspend_flag) == 0)
  872. {
  873. /* not to suspend */
  874. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  875. return -RT_EINTR;
  876. }
  877. #endif
  878. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  879. rt_schedule_remove_thread(thread);
  880. level = rt_spin_lock_irqsave(&(thread->spinlock));
  881. rt_thread_set_suspend_state(thread, suspend_flag);
  882. /* stop thread timer anyway */
  883. rt_timer_stop(&(thread->thread_timer));
  884. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  885. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  886. return RT_EOK;
  887. }
  888. RTM_EXPORT(rt_thread_suspend_with_flag);
  889. rt_err_t rt_thread_suspend(rt_thread_t thread)
  890. {
  891. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  892. }
  893. RTM_EXPORT(rt_thread_suspend);
  894. /**
  895. * @brief This function will resume a thread and put it to system ready queue.
  896. *
  897. * @param thread is the thread to be resumed.
  898. *
  899. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  900. * If the return value is any other values, it means this operation failed.
  901. */
  902. rt_err_t rt_thread_resume(rt_thread_t thread)
  903. {
  904. rt_base_t level;
  905. /* parameter check */
  906. RT_ASSERT(thread != RT_NULL);
  907. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  908. LOG_D("thread resume: %s", thread->parent.name);
  909. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  910. {
  911. LOG_D("thread resume: thread disorder, %d",
  912. thread->stat);
  913. return -RT_ERROR;
  914. }
  915. level = rt_spin_lock_irqsave(&(thread->spinlock)); //TODO need lock for cpu
  916. /* remove from suspend list */
  917. rt_list_remove(&(thread->tlist));
  918. rt_timer_stop(&thread->thread_timer);
  919. #ifdef RT_USING_SMART
  920. thread->wakeup.func = RT_NULL;
  921. #endif
  922. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  923. /* insert to schedule ready list */
  924. rt_schedule_insert_thread(thread);
  925. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  926. return RT_EOK;
  927. }
  928. RTM_EXPORT(rt_thread_resume);
  929. #ifdef RT_USING_SMART
  930. /**
  931. * This function will wakeup a thread with customized operation.
  932. *
  933. * @param thread the thread to be resumed
  934. *
  935. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  936. */
  937. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  938. {
  939. register rt_base_t temp;
  940. rt_err_t ret;
  941. rt_wakeup_func_t func = RT_NULL;
  942. RT_ASSERT(thread != RT_NULL);
  943. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  944. temp = rt_spin_lock_irqsave(&(thread->spinlock));
  945. func = thread->wakeup.func;
  946. thread->wakeup.func = RT_NULL;
  947. rt_spin_unlock_irqrestore(&(thread->spinlock), temp);
  948. if (func)
  949. {
  950. ret = func(thread->wakeup.user_data, thread);
  951. }
  952. else
  953. {
  954. ret = rt_thread_resume(thread);
  955. }
  956. return ret;
  957. }
  958. RTM_EXPORT(rt_thread_wakeup);
  959. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  960. {
  961. register rt_base_t temp;
  962. RT_ASSERT(thread != RT_NULL);
  963. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  964. temp = rt_spin_lock_irqsave(&(thread->spinlock));
  965. thread->wakeup.func = func;
  966. thread->wakeup.user_data = user_data;
  967. rt_spin_unlock_irqrestore(&(thread->spinlock), temp);
  968. }
  969. RTM_EXPORT(rt_thread_wakeup_set);
  970. #endif
  971. /**
  972. * @brief This function will find the specified thread.
  973. *
  974. * @note Please don't invoke this function in interrupt status.
  975. *
  976. * @param name is the name of thread finding.
  977. *
  978. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  979. * If the return value is RT_NULL, it means this operation failed.
  980. */
  981. rt_thread_t rt_thread_find(char *name)
  982. {
  983. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  984. }
  985. RTM_EXPORT(rt_thread_find);
  986. /**
  987. * @brief This function will return the name of the specified thread
  988. *
  989. * @note Please don't invoke this function in interrupt status
  990. *
  991. * @param thread the thread to retrieve thread name
  992. * @param name buffer to store the thread name string
  993. * @param name_size maximum size of the buffer to store the thread name
  994. *
  995. * @return If the return value is RT_EOK, the function is successfully executed
  996. * If the return value is -RT_EINVAL, it means this operation failed
  997. */
  998. rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size)
  999. {
  1000. return (thread == RT_NULL) ? -RT_EINVAL : rt_object_get_name(&thread->parent, name, name_size);
  1001. }
  1002. RTM_EXPORT(rt_thread_get_name);
  1003. /**@}*/