thread.c 28 KB

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