thread.c 28 KB

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