thread.c 36 KB

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