thread.c 36 KB

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