thread.c 27 KB

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