thread.c 28 KB

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