thread.c 28 KB

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