thread.c 37 KB

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