thread.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-28 Bernard first version
  9. * 2006-04-29 Bernard implement thread timer
  10. * 2006-04-30 Bernard added THREAD_DEBUG
  11. * 2006-05-27 Bernard fixed the rt_thread_yield bug
  12. * 2006-06-03 Bernard fixed the thread timer init bug
  13. * 2006-08-10 Bernard fixed the timer bug in thread_sleep
  14. * 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach
  15. * 2006-09-03 Bernard implement rt_thread_detach
  16. * 2008-02-16 Bernard fixed the rt_thread_timeout bug
  17. * 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to
  18. * RT_EOK.
  19. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  20. * 2011-09-01 Bernard fixed rt_thread_exit issue when the current
  21. * thread preempted, which reported by Jiaxing Lee.
  22. * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.
  23. * 2012-12-29 Bernard fixed compiling warning.
  24. * 2016-08-09 ArdaFu add thread suspend and resume hook.
  25. * 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach
  26. * bug when thread has not startup.
  27. * 2018-11-22 Jesven yield is same to rt_schedule
  28. * add support for tasks bound to cpu
  29. * 2021-02-24 Meco Man rearrange rt_thread_control() - schedule the thread when close it
  30. * 2021-11-15 THEWON Remove duplicate work between idle and _thread_exit
  31. * 2021-12-27 Meco Man remove .init_priority
  32. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to thread.c
  33. * 2022-01-24 THEWON let rt_thread_sleep return thread->error when using signal
  34. * 2022-10-15 Bernard add nested mutex feature
  35. */
  36. #include <rthw.h>
  37. #include <rtthread.h>
  38. #include <stddef.h>
  39. #ifndef __on_rt_thread_inited_hook
  40. #define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
  41. #endif
  42. #ifndef __on_rt_thread_suspend_hook
  43. #define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
  44. #endif
  45. #ifndef __on_rt_thread_resume_hook
  46. #define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
  47. #endif
  48. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  49. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  50. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  51. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  52. /**
  53. * @brief This function sets a hook function when the system suspend a thread.
  54. *
  55. * @note The hook function must be simple and never be blocked or suspend.
  56. *
  57. * @param hook is the specified hook function.
  58. */
  59. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  60. {
  61. rt_thread_suspend_hook = hook;
  62. }
  63. /**
  64. * @brief This function sets a hook function when the system resume a thread.
  65. *
  66. * @note The hook function must be simple and never be blocked or suspend.
  67. *
  68. * @param hook is the specified hook function.
  69. */
  70. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  71. {
  72. rt_thread_resume_hook = hook;
  73. }
  74. /**
  75. * @brief This function sets a hook function when a thread is initialized.
  76. *
  77. * @param hook is the specified hook function.
  78. */
  79. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  80. {
  81. rt_thread_inited_hook = hook;
  82. }
  83. #endif /* defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) */
  84. static void _thread_exit(void)
  85. {
  86. struct rt_thread *thread;
  87. rt_base_t level;
  88. /* get current thread */
  89. thread = rt_thread_self();
  90. /* disable interrupt */
  91. level = rt_hw_interrupt_disable();
  92. /* remove from schedule */
  93. rt_schedule_remove_thread(thread);
  94. /* remove it from timer list */
  95. rt_timer_detach(&thread->thread_timer);
  96. /* change stat */
  97. thread->stat = RT_THREAD_CLOSE;
  98. /* insert to defunct thread list */
  99. rt_thread_defunct_enqueue(thread);
  100. /* enable interrupt */
  101. rt_hw_interrupt_enable(level);
  102. /* switch to next task */
  103. rt_schedule();
  104. }
  105. /**
  106. * @brief This function is the timeout function for thread, normally which is invoked
  107. * when thread is timeout to wait some resource.
  108. *
  109. * @param parameter is the parameter of thread timeout function
  110. */
  111. static void _thread_timeout(void *parameter)
  112. {
  113. struct rt_thread *thread;
  114. rt_base_t level;
  115. thread = (struct rt_thread *)parameter;
  116. /* parameter check */
  117. RT_ASSERT(thread != RT_NULL);
  118. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
  119. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  120. /* disable interrupt */
  121. level = rt_hw_interrupt_disable();
  122. /* set error number */
  123. thread->error = -RT_ETIMEOUT;
  124. /* remove from suspend list */
  125. rt_list_remove(&(thread->tlist));
  126. /* insert to schedule ready list */
  127. rt_schedule_insert_thread(thread);
  128. /* enable interrupt */
  129. rt_hw_interrupt_enable(level);
  130. /* do schedule */
  131. rt_schedule();
  132. }
  133. static rt_err_t _thread_init(struct rt_thread *thread,
  134. const char *name,
  135. void (*entry)(void *parameter),
  136. void *parameter,
  137. void *stack_start,
  138. rt_uint32_t stack_size,
  139. rt_uint8_t priority,
  140. rt_uint32_t tick)
  141. {
  142. /* init thread list */
  143. rt_list_init(&(thread->tlist));
  144. thread->entry = (void *)entry;
  145. thread->parameter = parameter;
  146. /* stack init */
  147. thread->stack_addr = stack_start;
  148. thread->stack_size = stack_size;
  149. /* init thread stack */
  150. rt_memset(thread->stack_addr, '#', thread->stack_size);
  151. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  152. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  153. (void *)((char *)thread->stack_addr),
  154. (void *)_thread_exit);
  155. #else
  156. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  157. (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
  158. (void *)_thread_exit);
  159. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  160. /* priority init */
  161. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  162. thread->init_priority = priority;
  163. thread->current_priority = priority;
  164. thread->number_mask = 0;
  165. #ifdef RT_USING_MUTEX
  166. rt_list_init(&thread->taken_object_list);
  167. thread->pending_object = RT_NULL;
  168. #endif
  169. #ifdef RT_USING_EVENT
  170. thread->event_set = 0;
  171. thread->event_info = 0;
  172. #endif /* RT_USING_EVENT */
  173. #if RT_THREAD_PRIORITY_MAX > 32
  174. thread->number = 0;
  175. thread->high_mask = 0;
  176. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  177. /* tick init */
  178. thread->init_tick = tick;
  179. thread->remaining_tick = tick;
  180. /* error and flags */
  181. thread->error = RT_EOK;
  182. thread->stat = RT_THREAD_INIT;
  183. #ifdef RT_USING_SMP
  184. /* not bind on any cpu */
  185. thread->bind_cpu = RT_CPUS_NR;
  186. thread->oncpu = RT_CPU_DETACHED;
  187. /* lock init */
  188. thread->scheduler_lock_nest = 0;
  189. thread->cpus_lock_nest = 0;
  190. thread->critical_lock_nest = 0;
  191. #endif /* RT_USING_SMP */
  192. /* initialize cleanup function and user data */
  193. thread->cleanup = 0;
  194. thread->user_data = 0;
  195. /* initialize thread timer */
  196. rt_timer_init(&(thread->thread_timer),
  197. thread->name,
  198. _thread_timeout,
  199. thread,
  200. 0,
  201. RT_TIMER_FLAG_ONE_SHOT);
  202. /* initialize signal */
  203. #ifdef RT_USING_SIGNALS
  204. thread->sig_mask = 0x00;
  205. thread->sig_pending = 0x00;
  206. #ifndef RT_USING_SMP
  207. thread->sig_ret = RT_NULL;
  208. #endif /* RT_USING_SMP */
  209. thread->sig_vectors = RT_NULL;
  210. thread->si_list = RT_NULL;
  211. #endif /* RT_USING_SIGNALS */
  212. #ifdef RT_USING_LWP
  213. thread->lwp = RT_NULL;
  214. #endif /* RT_USING_LWP */
  215. #ifdef RT_USING_CPU_USAGE
  216. thread->duration_tick = 0;
  217. #endif /* RT_USING_CPU_USAGE */
  218. #ifdef RT_USING_PTHREADS
  219. thread->pthread_data = RT_NULL;
  220. #endif /* RT_USING_PTHREADS */
  221. #ifdef RT_USING_MODULE
  222. thread->module_id = 0;
  223. #endif /* RT_USING_MODULE */
  224. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  225. return RT_EOK;
  226. }
  227. /**
  228. * @addtogroup Thread
  229. */
  230. /**@{*/
  231. /**
  232. * @brief This function will initialize a thread. It's used to initialize a
  233. * static thread object.
  234. *
  235. * @param thread is the static thread object.
  236. *
  237. * @param name is the name of thread, which shall be unique.
  238. *
  239. * @param entry is the entry function of thread.
  240. *
  241. * @param parameter is the parameter of thread enter function.
  242. *
  243. * @param stack_start is the start address of thread stack.
  244. *
  245. * @param stack_size is the size of thread stack.
  246. *
  247. * @param priority is the priority of thread.
  248. *
  249. * @param tick is the time slice if there are same priority thread.
  250. *
  251. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  252. * If the return value is any other values, it means this operation failed.
  253. */
  254. rt_err_t rt_thread_init(struct rt_thread *thread,
  255. const char *name,
  256. void (*entry)(void *parameter),
  257. void *parameter,
  258. void *stack_start,
  259. rt_uint32_t stack_size,
  260. rt_uint8_t priority,
  261. rt_uint32_t tick)
  262. {
  263. /* parameter check */
  264. RT_ASSERT(thread != RT_NULL);
  265. RT_ASSERT(stack_start != RT_NULL);
  266. /* initialize thread object */
  267. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  268. return _thread_init(thread,
  269. name,
  270. entry,
  271. parameter,
  272. stack_start,
  273. stack_size,
  274. priority,
  275. tick);
  276. }
  277. RTM_EXPORT(rt_thread_init);
  278. /**
  279. * @brief This function will return self thread object.
  280. *
  281. * @return The self thread object.
  282. */
  283. rt_thread_t rt_thread_self(void)
  284. {
  285. #ifdef RT_USING_SMP
  286. rt_base_t lock;
  287. rt_thread_t self;
  288. lock = rt_hw_local_irq_disable();
  289. self = rt_cpu_self()->current_thread;
  290. rt_hw_local_irq_enable(lock);
  291. return self;
  292. #else
  293. extern rt_thread_t rt_current_thread;
  294. return rt_current_thread;
  295. #endif /* RT_USING_SMP */
  296. }
  297. RTM_EXPORT(rt_thread_self);
  298. /**
  299. * @brief This function will start a thread and put it to system ready queue.
  300. *
  301. * @param thread is the thread to be started.
  302. *
  303. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  304. * If the return value is any other values, it means this operation failed.
  305. */
  306. rt_err_t rt_thread_startup(rt_thread_t thread)
  307. {
  308. /* parameter check */
  309. RT_ASSERT(thread != RT_NULL);
  310. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  311. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  312. /* calculate priority attribute */
  313. #if RT_THREAD_PRIORITY_MAX > 32
  314. thread->number = thread->current_priority >> 3; /* 5bit */
  315. thread->number_mask = 1L << thread->number;
  316. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  317. #else
  318. thread->number_mask = 1L << thread->current_priority;
  319. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  320. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  321. thread->name, thread->current_priority));
  322. /* change thread stat */
  323. thread->stat = RT_THREAD_SUSPEND;
  324. /* then resume it */
  325. rt_thread_resume(thread);
  326. if (rt_thread_self() != RT_NULL)
  327. {
  328. /* do a scheduling */
  329. rt_schedule();
  330. }
  331. return RT_EOK;
  332. }
  333. RTM_EXPORT(rt_thread_startup);
  334. /**
  335. * @brief This function will detach a thread. The thread object will be removed from
  336. * thread queue and detached/deleted from the system object management.
  337. *
  338. * @param thread is the thread to be deleted.
  339. *
  340. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  341. * If the return value is any other values, it means this operation failed.
  342. */
  343. rt_err_t rt_thread_detach(rt_thread_t thread)
  344. {
  345. rt_base_t level;
  346. /* parameter check */
  347. RT_ASSERT(thread != RT_NULL);
  348. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  349. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  350. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  351. return RT_EOK;
  352. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  353. {
  354. /* remove from schedule */
  355. rt_schedule_remove_thread(thread);
  356. }
  357. /* disable interrupt */
  358. level = rt_hw_interrupt_disable();
  359. /* release thread timer */
  360. rt_timer_detach(&(thread->thread_timer));
  361. /* change stat */
  362. thread->stat = RT_THREAD_CLOSE;
  363. #ifdef RT_USING_MUTEX
  364. if ((thread->pending_object) &&
  365. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  366. {
  367. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  368. rt_mutex_drop_thread(mutex, thread);
  369. thread->pending_object = RT_NULL;
  370. }
  371. #endif
  372. /* insert to defunct thread list */
  373. rt_thread_defunct_enqueue(thread);
  374. /* enable interrupt */
  375. rt_hw_interrupt_enable(level);
  376. return RT_EOK;
  377. }
  378. RTM_EXPORT(rt_thread_detach);
  379. #ifdef RT_USING_HEAP
  380. /**
  381. * @brief This function will create a thread object and allocate thread object memory.
  382. * and stack.
  383. *
  384. * @param name is the name of thread, which shall be unique.
  385. *
  386. * @param entry is the entry function of thread.
  387. *
  388. * @param parameter is the parameter of thread enter function.
  389. *
  390. * @param stack_size is the size of thread stack.
  391. *
  392. * @param priority is the priority of thread.
  393. *
  394. * @param tick is the time slice if there are same priority thread.
  395. *
  396. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  397. * If the return value is RT_NULL, it means this operation failed.
  398. */
  399. rt_thread_t rt_thread_create(const char *name,
  400. void (*entry)(void *parameter),
  401. void *parameter,
  402. rt_uint32_t stack_size,
  403. rt_uint8_t priority,
  404. rt_uint32_t tick)
  405. {
  406. struct rt_thread *thread;
  407. void *stack_start;
  408. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  409. name);
  410. if (thread == RT_NULL)
  411. return RT_NULL;
  412. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  413. if (stack_start == RT_NULL)
  414. {
  415. /* allocate stack failure */
  416. rt_object_delete((rt_object_t)thread);
  417. return RT_NULL;
  418. }
  419. _thread_init(thread,
  420. name,
  421. entry,
  422. parameter,
  423. stack_start,
  424. stack_size,
  425. priority,
  426. tick);
  427. return thread;
  428. }
  429. RTM_EXPORT(rt_thread_create);
  430. /**
  431. * @brief This function will delete a thread. The thread object will be removed from
  432. * thread queue and deleted from system object management in the idle thread.
  433. *
  434. * @param thread is the thread to be deleted.
  435. *
  436. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  437. * If the return value is any other values, it means this operation failed.
  438. */
  439. rt_err_t rt_thread_delete(rt_thread_t thread)
  440. {
  441. rt_base_t level;
  442. /* parameter check */
  443. RT_ASSERT(thread != RT_NULL);
  444. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  445. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  446. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  447. return RT_EOK;
  448. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  449. {
  450. /* remove from schedule */
  451. rt_schedule_remove_thread(thread);
  452. }
  453. /* disable interrupt */
  454. level = rt_hw_interrupt_disable();
  455. /* release thread timer */
  456. rt_timer_detach(&(thread->thread_timer));
  457. /* change stat */
  458. thread->stat = RT_THREAD_CLOSE;
  459. #ifdef RT_USING_MUTEX
  460. if ((thread->pending_object) &&
  461. (rt_object_get_type(thread->pending_object) == RT_Object_Class_Mutex))
  462. {
  463. struct rt_mutex *mutex = (struct rt_mutex*)thread->pending_object;
  464. rt_mutex_drop_thread(mutex, thread);
  465. thread->pending_object = RT_NULL;
  466. }
  467. #endif
  468. /* insert to defunct thread list */
  469. rt_thread_defunct_enqueue(thread);
  470. /* enable interrupt */
  471. rt_hw_interrupt_enable(level);
  472. return RT_EOK;
  473. }
  474. RTM_EXPORT(rt_thread_delete);
  475. #endif /* RT_USING_HEAP */
  476. /**
  477. * @brief This function will let current thread yield processor, and scheduler will
  478. * choose the highest thread to run. After yield processor, the current thread
  479. * is still in READY state.
  480. *
  481. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  482. * If the return value is any other values, it means this operation failed.
  483. */
  484. rt_err_t rt_thread_yield(void)
  485. {
  486. struct rt_thread *thread;
  487. rt_base_t level;
  488. thread = rt_thread_self();
  489. level = rt_hw_interrupt_disable();
  490. thread->remaining_tick = thread->init_tick;
  491. thread->stat |= RT_THREAD_STAT_YIELD;
  492. rt_schedule();
  493. rt_hw_interrupt_enable(level);
  494. return RT_EOK;
  495. }
  496. RTM_EXPORT(rt_thread_yield);
  497. /**
  498. * @brief This function will let current thread sleep for some ticks. Change current thread state to suspend,
  499. * when the thread timer reaches the tick value, scheduler will awaken this thread.
  500. *
  501. * @param tick is the sleep ticks.
  502. *
  503. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  504. * If the return value is any other values, it means this operation failed.
  505. */
  506. rt_err_t rt_thread_sleep(rt_tick_t tick)
  507. {
  508. rt_base_t level;
  509. struct rt_thread *thread;
  510. if (tick == 0)
  511. {
  512. return -RT_EINVAL;
  513. }
  514. /* set to current thread */
  515. thread = rt_thread_self();
  516. RT_ASSERT(thread != RT_NULL);
  517. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  518. /* current context checking */
  519. RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
  520. /* disable interrupt */
  521. level = rt_hw_interrupt_disable();
  522. /* reset thread error */
  523. thread->error = RT_EOK;
  524. /* suspend thread */
  525. rt_thread_suspend(thread);
  526. /* reset the timeout of thread timer and start it */
  527. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  528. rt_timer_start(&(thread->thread_timer));
  529. /* enable interrupt */
  530. rt_hw_interrupt_enable(level);
  531. rt_schedule();
  532. /* clear error number of this thread to RT_EOK */
  533. if (thread->error == -RT_ETIMEOUT)
  534. thread->error = RT_EOK;
  535. return thread->error;
  536. }
  537. /**
  538. * @brief This function will let current thread delay for some ticks.
  539. *
  540. * @param tick is the delay ticks.
  541. *
  542. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  543. * If the return value is any other values, it means this operation failed.
  544. */
  545. rt_err_t rt_thread_delay(rt_tick_t tick)
  546. {
  547. return rt_thread_sleep(tick);
  548. }
  549. RTM_EXPORT(rt_thread_delay);
  550. /**
  551. * @brief This function will let current thread delay until (*tick + inc_tick).
  552. *
  553. * @param tick is the tick of last wakeup.
  554. *
  555. * @param inc_tick is the increment tick.
  556. *
  557. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  558. * If the return value is any other values, it means this operation failed.
  559. */
  560. rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
  561. {
  562. rt_base_t level;
  563. struct rt_thread *thread;
  564. rt_tick_t cur_tick;
  565. RT_ASSERT(tick != RT_NULL);
  566. /* set to current thread */
  567. thread = rt_thread_self();
  568. RT_ASSERT(thread != RT_NULL);
  569. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  570. /* disable interrupt */
  571. level = rt_hw_interrupt_disable();
  572. /* reset thread error */
  573. thread->error = RT_EOK;
  574. cur_tick = rt_tick_get();
  575. if (cur_tick - *tick < inc_tick)
  576. {
  577. rt_tick_t left_tick;
  578. *tick += inc_tick;
  579. left_tick = *tick - cur_tick;
  580. /* suspend thread */
  581. rt_thread_suspend(thread);
  582. /* reset the timeout of thread timer and start it */
  583. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick);
  584. rt_timer_start(&(thread->thread_timer));
  585. /* enable interrupt */
  586. rt_hw_interrupt_enable(level);
  587. rt_schedule();
  588. /* clear error number of this thread to RT_EOK */
  589. if (thread->error == -RT_ETIMEOUT)
  590. {
  591. thread->error = RT_EOK;
  592. }
  593. }
  594. else
  595. {
  596. *tick = cur_tick;
  597. rt_hw_interrupt_enable(level);
  598. }
  599. return thread->error;
  600. }
  601. RTM_EXPORT(rt_thread_delay_until);
  602. /**
  603. * @brief This function will let current thread delay for some milliseconds.
  604. *
  605. * @param ms is the delay ms time.
  606. *
  607. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  608. * If the return value is any other values, it means this operation failed.
  609. */
  610. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  611. {
  612. rt_tick_t tick;
  613. tick = rt_tick_from_millisecond(ms);
  614. return rt_thread_sleep(tick);
  615. }
  616. RTM_EXPORT(rt_thread_mdelay);
  617. /**
  618. * @brief This function will control thread behaviors according to control command.
  619. *
  620. * @param thread is the specified thread to be controlled.
  621. *
  622. * @param cmd is the control command, which includes.
  623. *
  624. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
  625. *
  626. * RT_THREAD_CTRL_STARTUP for starting a thread.
  627. *
  628. * RT_THREAD_CTRL_CLOSE for delete a thread.
  629. *
  630. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  631. *
  632. * @param arg is the argument of control command.
  633. *
  634. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  635. * If the return value is any other values, it means this operation failed.
  636. */
  637. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  638. {
  639. rt_base_t level;
  640. /* parameter check */
  641. RT_ASSERT(thread != RT_NULL);
  642. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  643. switch (cmd)
  644. {
  645. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  646. {
  647. /* disable interrupt */
  648. level = rt_hw_interrupt_disable();
  649. /* for ready thread, change queue */
  650. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  651. {
  652. /* remove thread from schedule queue first */
  653. rt_schedule_remove_thread(thread);
  654. /* change thread priority */
  655. thread->current_priority = *(rt_uint8_t *)arg;
  656. /* recalculate priority attribute */
  657. #if RT_THREAD_PRIORITY_MAX > 32
  658. thread->number = thread->current_priority >> 3; /* 5bit */
  659. thread->number_mask = 1 << thread->number;
  660. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  661. #else
  662. thread->number_mask = 1 << thread->current_priority;
  663. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  664. /* insert thread to schedule queue again */
  665. rt_schedule_insert_thread(thread);
  666. }
  667. else
  668. {
  669. thread->current_priority = *(rt_uint8_t *)arg;
  670. /* recalculate priority attribute */
  671. #if RT_THREAD_PRIORITY_MAX > 32
  672. thread->number = thread->current_priority >> 3; /* 5bit */
  673. thread->number_mask = 1 << thread->number;
  674. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  675. #else
  676. thread->number_mask = 1 << thread->current_priority;
  677. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  678. }
  679. /* enable interrupt */
  680. rt_hw_interrupt_enable(level);
  681. break;
  682. }
  683. case RT_THREAD_CTRL_STARTUP:
  684. {
  685. return rt_thread_startup(thread);
  686. }
  687. case RT_THREAD_CTRL_CLOSE:
  688. {
  689. rt_err_t rt_err;
  690. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  691. {
  692. rt_err = rt_thread_detach(thread);
  693. }
  694. #ifdef RT_USING_HEAP
  695. else
  696. {
  697. rt_err = rt_thread_delete(thread);
  698. }
  699. #endif /* RT_USING_HEAP */
  700. rt_schedule();
  701. return rt_err;
  702. }
  703. #ifdef RT_USING_SMP
  704. case RT_THREAD_CTRL_BIND_CPU:
  705. {
  706. rt_uint8_t cpu;
  707. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  708. {
  709. /* we only support bind cpu before started phase. */
  710. return RT_ERROR;
  711. }
  712. cpu = (rt_uint8_t)(rt_size_t)arg;
  713. thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;
  714. break;
  715. }
  716. #endif /* RT_USING_SMP */
  717. default:
  718. break;
  719. }
  720. return RT_EOK;
  721. }
  722. RTM_EXPORT(rt_thread_control);
  723. /**
  724. * @brief This function will suspend the specified thread and change it to suspend state.
  725. *
  726. * @note This function ONLY can suspend current thread itself.
  727. * rt_thread_suspend(rt_thread_self());
  728. *
  729. * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
  730. * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
  731. * other threads and occupying this resouce, starvation can occur very easily.
  732. *
  733. * @param thread is the thread to be suspended.
  734. *
  735. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  736. * If the return value is any other values, it means this operation failed.
  737. */
  738. rt_err_t rt_thread_suspend(rt_thread_t thread)
  739. {
  740. rt_base_t stat;
  741. rt_base_t level;
  742. /* parameter check */
  743. RT_ASSERT(thread != RT_NULL);
  744. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  745. RT_ASSERT(thread == rt_thread_self());
  746. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  747. stat = thread->stat & RT_THREAD_STAT_MASK;
  748. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  749. {
  750. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n", thread->stat));
  751. return -RT_ERROR;
  752. }
  753. /* disable interrupt */
  754. level = rt_hw_interrupt_disable();
  755. /* change thread stat */
  756. rt_schedule_remove_thread(thread);
  757. thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
  758. /* stop thread timer anyway */
  759. rt_timer_stop(&(thread->thread_timer));
  760. /* enable interrupt */
  761. rt_hw_interrupt_enable(level);
  762. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  763. return RT_EOK;
  764. }
  765. RTM_EXPORT(rt_thread_suspend);
  766. /**
  767. * @brief This function will resume a thread and put it to system ready queue.
  768. *
  769. * @param thread is the thread to be resumed.
  770. *
  771. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  772. * If the return value is any other values, it means this operation failed.
  773. */
  774. rt_err_t rt_thread_resume(rt_thread_t thread)
  775. {
  776. rt_base_t level;
  777. /* parameter check */
  778. RT_ASSERT(thread != RT_NULL);
  779. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  780. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  781. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
  782. {
  783. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  784. thread->stat));
  785. return -RT_ERROR;
  786. }
  787. /* disable interrupt */
  788. level = rt_hw_interrupt_disable();
  789. /* remove from suspend list */
  790. rt_list_remove(&(thread->tlist));
  791. rt_timer_stop(&thread->thread_timer);
  792. /* insert to schedule ready list */
  793. rt_schedule_insert_thread(thread);
  794. /* enable interrupt */
  795. rt_hw_interrupt_enable(level);
  796. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  797. return RT_EOK;
  798. }
  799. RTM_EXPORT(rt_thread_resume);
  800. /**
  801. * @brief This function will find the specified thread.
  802. *
  803. * @note Please don't invoke this function in interrupt status.
  804. *
  805. * @param name is the name of thread finding.
  806. *
  807. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  808. * If the return value is RT_NULL, it means this operation failed.
  809. */
  810. rt_thread_t rt_thread_find(char *name)
  811. {
  812. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  813. }
  814. RTM_EXPORT(rt_thread_find);
  815. /**@}*/