thread.c 28 KB

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