thread.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. */
  31. #include <rthw.h>
  32. #include <rtthread.h>
  33. #include <stddef.h>
  34. #ifdef RT_USING_HOOK
  35. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  36. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  37. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  38. /**
  39. * @brief This function sets a hook function when the system suspend a thread.
  40. *
  41. * @note The hook function must be simple and never be blocked or suspend.
  42. *
  43. * @param hook is the specified hook function.
  44. */
  45. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  46. {
  47. rt_thread_suspend_hook = hook;
  48. }
  49. /**
  50. * @brief This function sets a hook function when the system resume 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_resume_sethook(void (*hook)(rt_thread_t thread))
  57. {
  58. rt_thread_resume_hook = hook;
  59. }
  60. /**
  61. * @brief This function sets a hook function when a thread is initialized.
  62. *
  63. * @param hook is the specified hook function.
  64. */
  65. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  66. {
  67. rt_thread_inited_hook = hook;
  68. }
  69. #endif /* RT_USING_HOOK */
  70. static void _thread_cleanup_execute(rt_thread_t thread)
  71. {
  72. register rt_base_t level;
  73. #ifdef RT_USING_MODULE
  74. struct rt_dlmodule *module = RT_NULL;
  75. #endif /* RT_USING_MODULE */
  76. level = rt_hw_interrupt_disable();
  77. #ifdef RT_USING_MODULE
  78. module = (struct rt_dlmodule*)thread->module_id;
  79. if (module)
  80. {
  81. dlmodule_destroy(module);
  82. }
  83. #endif /* RT_USING_MODULE */
  84. /* invoke thread cleanup */
  85. if (thread->cleanup != RT_NULL)
  86. thread->cleanup(thread);
  87. #ifdef RT_USING_SIGNALS
  88. rt_thread_free_sig(thread);
  89. #endif /* RT_USING_SIGNALS */
  90. rt_hw_interrupt_enable(level);
  91. }
  92. static void _thread_exit(void)
  93. {
  94. struct rt_thread *thread;
  95. register rt_base_t level;
  96. /* get current thread */
  97. thread = rt_thread_self();
  98. /* disable interrupt */
  99. level = rt_hw_interrupt_disable();
  100. _thread_cleanup_execute(thread);
  101. /* remove from schedule */
  102. rt_schedule_remove_thread(thread);
  103. /* change stat */
  104. thread->stat = RT_THREAD_CLOSE;
  105. /* remove it from timer list */
  106. rt_timer_detach(&thread->thread_timer);
  107. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  108. {
  109. rt_object_detach((rt_object_t)thread);
  110. }
  111. else
  112. {
  113. /* insert to defunct thread list */
  114. rt_thread_defunct_enqueue(thread);
  115. }
  116. /* switch to next task */
  117. rt_schedule();
  118. /* enable interrupt */
  119. rt_hw_interrupt_enable(level);
  120. }
  121. static rt_err_t _thread_init(struct rt_thread *thread,
  122. const char *name,
  123. void (*entry)(void *parameter),
  124. void *parameter,
  125. void *stack_start,
  126. rt_uint32_t stack_size,
  127. rt_uint8_t priority,
  128. rt_uint32_t tick)
  129. {
  130. /* init thread list */
  131. rt_list_init(&(thread->tlist));
  132. thread->entry = (void *)entry;
  133. thread->parameter = parameter;
  134. /* stack init */
  135. thread->stack_addr = stack_start;
  136. thread->stack_size = stack_size;
  137. /* init thread stack */
  138. rt_memset(thread->stack_addr, '#', thread->stack_size);
  139. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  140. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  141. (void *)((char *)thread->stack_addr),
  142. (void *)_thread_exit);
  143. #else
  144. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  145. (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
  146. (void *)_thread_exit);
  147. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  148. /* priority init */
  149. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  150. thread->init_priority = priority;
  151. thread->current_priority = priority;
  152. thread->number_mask = 0;
  153. #if RT_THREAD_PRIORITY_MAX > 32
  154. thread->number = 0;
  155. thread->high_mask = 0;
  156. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  157. /* tick init */
  158. thread->init_tick = tick;
  159. thread->remaining_tick = tick;
  160. /* error and flags */
  161. thread->error = RT_EOK;
  162. thread->stat = RT_THREAD_INIT;
  163. #ifdef RT_USING_SMP
  164. /* not bind on any cpu */
  165. thread->bind_cpu = RT_CPUS_NR;
  166. thread->oncpu = RT_CPU_DETACHED;
  167. /* lock init */
  168. thread->scheduler_lock_nest = 0;
  169. thread->cpus_lock_nest = 0;
  170. thread->critical_lock_nest = 0;
  171. #endif /* RT_USING_SMP */
  172. /* initialize cleanup function and user data */
  173. thread->cleanup = 0;
  174. thread->user_data = 0;
  175. /* initialize thread timer */
  176. rt_timer_init(&(thread->thread_timer),
  177. thread->name,
  178. rt_thread_timeout,
  179. thread,
  180. 0,
  181. RT_TIMER_FLAG_ONE_SHOT);
  182. /* initialize signal */
  183. #ifdef RT_USING_SIGNALS
  184. thread->sig_mask = 0x00;
  185. thread->sig_pending = 0x00;
  186. #ifndef RT_USING_SMP
  187. thread->sig_ret = RT_NULL;
  188. #endif /* RT_USING_SMP */
  189. thread->sig_vectors = RT_NULL;
  190. thread->si_list = RT_NULL;
  191. #endif /* RT_USING_SIGNALS */
  192. #ifdef RT_USING_LWP
  193. thread->lwp = RT_NULL;
  194. #endif /* RT_USING_LWP */
  195. #ifdef RT_USING_CPU_USAGE
  196. thread->duration_tick = 0;
  197. #endif
  198. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  199. return RT_EOK;
  200. }
  201. /**
  202. * @addtogroup Thread
  203. */
  204. /**@{*/
  205. /**
  206. * @brief This function will initialize a thread. It's used to initialize a
  207. * static thread object.
  208. *
  209. * @param thread is the static thread object.
  210. *
  211. * @param name is the name of thread, which shall be unique.
  212. *
  213. * @param entry is the entry function of thread.
  214. *
  215. * @param parameter is the parameter of thread enter function.
  216. *
  217. * @param stack_start is the start address of thread stack.
  218. *
  219. * @param stack_size is the size of thread stack.
  220. *
  221. * @param priority is the priority of thread.
  222. *
  223. * @param tick is the time slice if there are same priority thread.
  224. *
  225. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  226. * If the return value is any other values, it means this operation failed.
  227. */
  228. rt_err_t rt_thread_init(struct rt_thread *thread,
  229. const char *name,
  230. void (*entry)(void *parameter),
  231. void *parameter,
  232. void *stack_start,
  233. rt_uint32_t stack_size,
  234. rt_uint8_t priority,
  235. rt_uint32_t tick)
  236. {
  237. /* thread check */
  238. RT_ASSERT(thread != RT_NULL);
  239. RT_ASSERT(stack_start != RT_NULL);
  240. /* initialize thread object */
  241. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  242. return _thread_init(thread,
  243. name,
  244. entry,
  245. parameter,
  246. stack_start,
  247. stack_size,
  248. priority,
  249. tick);
  250. }
  251. RTM_EXPORT(rt_thread_init);
  252. /**
  253. * @brief This function will return self thread object.
  254. *
  255. * @return The self thread object.
  256. */
  257. rt_thread_t rt_thread_self(void)
  258. {
  259. #ifdef RT_USING_SMP
  260. rt_base_t lock;
  261. rt_thread_t self;
  262. lock = rt_hw_local_irq_disable();
  263. self = rt_cpu_self()->current_thread;
  264. rt_hw_local_irq_enable(lock);
  265. return self;
  266. #else
  267. extern rt_thread_t rt_current_thread;
  268. return rt_current_thread;
  269. #endif /* RT_USING_SMP */
  270. }
  271. RTM_EXPORT(rt_thread_self);
  272. /**
  273. * @brief This function will start a thread and put it to system ready queue.
  274. *
  275. * @param thread is the thread to be started.
  276. *
  277. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  278. * If the return value is any other values, it means this operation failed.
  279. */
  280. rt_err_t rt_thread_startup(rt_thread_t thread)
  281. {
  282. /* thread check */
  283. RT_ASSERT(thread != RT_NULL);
  284. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  285. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  286. /* set current priority to initialize priority */
  287. thread->current_priority = thread->init_priority;
  288. /* calculate priority attribute */
  289. #if RT_THREAD_PRIORITY_MAX > 32
  290. thread->number = thread->current_priority >> 3; /* 5bit */
  291. thread->number_mask = 1L << thread->number;
  292. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  293. #else
  294. thread->number_mask = 1L << thread->current_priority;
  295. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  296. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  297. thread->name, thread->init_priority));
  298. /* change thread stat */
  299. thread->stat = RT_THREAD_SUSPEND;
  300. /* then resume it */
  301. rt_thread_resume(thread);
  302. if (rt_thread_self() != RT_NULL)
  303. {
  304. /* do a scheduling */
  305. rt_schedule();
  306. }
  307. return RT_EOK;
  308. }
  309. RTM_EXPORT(rt_thread_startup);
  310. /**
  311. * @brief This function will detach a thread. The thread object will be removed from
  312. * thread queue and detached/deleted from the system object management.
  313. *
  314. * @param thread is the thread to be deleted.
  315. *
  316. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  317. * If the return value is any other values, it means this operation failed.
  318. */
  319. rt_err_t rt_thread_detach(rt_thread_t thread)
  320. {
  321. rt_base_t lock;
  322. /* thread check */
  323. RT_ASSERT(thread != RT_NULL);
  324. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  325. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  326. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  327. return RT_EOK;
  328. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  329. {
  330. /* remove from schedule */
  331. rt_schedule_remove_thread(thread);
  332. }
  333. _thread_cleanup_execute(thread);
  334. /* release thread timer */
  335. rt_timer_detach(&(thread->thread_timer));
  336. /* change stat */
  337. thread->stat = RT_THREAD_CLOSE;
  338. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  339. {
  340. rt_object_detach((rt_object_t)thread);
  341. }
  342. else
  343. {
  344. /* disable interrupt */
  345. lock = rt_hw_interrupt_disable();
  346. /* insert to defunct thread list */
  347. rt_thread_defunct_enqueue(thread);
  348. /* enable interrupt */
  349. rt_hw_interrupt_enable(lock);
  350. }
  351. return RT_EOK;
  352. }
  353. RTM_EXPORT(rt_thread_detach);
  354. #ifdef RT_USING_HEAP
  355. /**
  356. * @brief This function will create a thread object and allocate thread object memory.
  357. * and stack.
  358. *
  359. * @param name is the name of thread, which shall be unique.
  360. *
  361. * @param entry is the entry function of thread.
  362. *
  363. * @param parameter is the parameter of thread enter function.
  364. *
  365. * @param stack_size is the size of thread stack.
  366. *
  367. * @param priority is the priority of thread.
  368. *
  369. * @param tick is the time slice if there are same priority thread.
  370. *
  371. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  372. * If the return value is RT_NULL, it means this operation failed.
  373. */
  374. rt_thread_t rt_thread_create(const char *name,
  375. void (*entry)(void *parameter),
  376. void *parameter,
  377. rt_uint32_t stack_size,
  378. rt_uint8_t priority,
  379. rt_uint32_t tick)
  380. {
  381. struct rt_thread *thread;
  382. void *stack_start;
  383. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  384. name);
  385. if (thread == RT_NULL)
  386. return RT_NULL;
  387. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  388. if (stack_start == RT_NULL)
  389. {
  390. /* allocate stack failure */
  391. rt_object_delete((rt_object_t)thread);
  392. return RT_NULL;
  393. }
  394. _thread_init(thread,
  395. name,
  396. entry,
  397. parameter,
  398. stack_start,
  399. stack_size,
  400. priority,
  401. tick);
  402. return thread;
  403. }
  404. RTM_EXPORT(rt_thread_create);
  405. /**
  406. * @brief This function will delete a thread. The thread object will be removed from
  407. * thread queue and deleted from system object management in the idle thread.
  408. *
  409. * @param thread is the thread to be deleted.
  410. *
  411. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  412. * If the return value is any other values, it means this operation failed.
  413. */
  414. rt_err_t rt_thread_delete(rt_thread_t thread)
  415. {
  416. rt_base_t lock;
  417. /* thread check */
  418. RT_ASSERT(thread != RT_NULL);
  419. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  420. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  421. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  422. return RT_EOK;
  423. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  424. {
  425. /* remove from schedule */
  426. rt_schedule_remove_thread(thread);
  427. }
  428. _thread_cleanup_execute(thread);
  429. /* release thread timer */
  430. rt_timer_detach(&(thread->thread_timer));
  431. /* disable interrupt */
  432. lock = rt_hw_interrupt_disable();
  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 If suspend self thread, after this function call, the
  684. * rt_schedule() must be invoked.
  685. *
  686. * @param thread is the thread to be suspended.
  687. *
  688. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  689. * If the return value is any other values, it means this operation failed.
  690. */
  691. rt_err_t rt_thread_suspend(rt_thread_t thread)
  692. {
  693. register rt_base_t stat;
  694. register rt_base_t temp;
  695. /* thread check */
  696. RT_ASSERT(thread != RT_NULL);
  697. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  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",
  703. thread->stat));
  704. return -RT_ERROR;
  705. }
  706. /* disable interrupt */
  707. temp = rt_hw_interrupt_disable();
  708. if (stat == RT_THREAD_RUNNING)
  709. {
  710. /* not suspend running status thread on other core */
  711. RT_ASSERT(thread == rt_thread_self());
  712. }
  713. /* change thread stat */
  714. rt_schedule_remove_thread(thread);
  715. thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
  716. /* stop thread timer anyway */
  717. rt_timer_stop(&(thread->thread_timer));
  718. /* enable interrupt */
  719. rt_hw_interrupt_enable(temp);
  720. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  721. return RT_EOK;
  722. }
  723. RTM_EXPORT(rt_thread_suspend);
  724. /**
  725. * @brief This function will resume a thread and put it to system ready queue.
  726. *
  727. * @param thread is the thread to be resumed.
  728. *
  729. * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
  730. * If the return value is any other values, it means this operation failed.
  731. */
  732. rt_err_t rt_thread_resume(rt_thread_t thread)
  733. {
  734. register rt_base_t temp;
  735. /* thread check */
  736. RT_ASSERT(thread != RT_NULL);
  737. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  738. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  739. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
  740. {
  741. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  742. thread->stat));
  743. return -RT_ERROR;
  744. }
  745. /* disable interrupt */
  746. temp = rt_hw_interrupt_disable();
  747. /* remove from suspend list */
  748. rt_list_remove(&(thread->tlist));
  749. rt_timer_stop(&thread->thread_timer);
  750. /* insert to schedule ready list */
  751. rt_schedule_insert_thread(thread);
  752. /* enable interrupt */
  753. rt_hw_interrupt_enable(temp);
  754. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  755. return RT_EOK;
  756. }
  757. RTM_EXPORT(rt_thread_resume);
  758. /**
  759. * @brief This function is the timeout function for thread, normally which is invoked
  760. * when thread is timeout to wait some resource.
  761. *
  762. * @param parameter is the parameter of thread timeout function
  763. */
  764. void rt_thread_timeout(void *parameter)
  765. {
  766. struct rt_thread *thread;
  767. register rt_base_t temp;
  768. thread = (struct rt_thread *)parameter;
  769. /* thread check */
  770. RT_ASSERT(thread != RT_NULL);
  771. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
  772. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  773. /* disable interrupt */
  774. temp = rt_hw_interrupt_disable();
  775. /* set error number */
  776. thread->error = -RT_ETIMEOUT;
  777. /* remove from suspend list */
  778. rt_list_remove(&(thread->tlist));
  779. /* insert to schedule ready list */
  780. rt_schedule_insert_thread(thread);
  781. /* enable interrupt */
  782. rt_hw_interrupt_enable(temp);
  783. /* do schedule */
  784. rt_schedule();
  785. }
  786. RTM_EXPORT(rt_thread_timeout);
  787. /**
  788. * @brief This function will find the specified thread.
  789. *
  790. * @note Please don't invoke this function in interrupt status.
  791. *
  792. * @param name is the name of thread finding.
  793. *
  794. * @return If the return value is a rt_thread structure pointer, the function is successfully executed.
  795. * If the return value is RT_NULL, it means this operation failed.
  796. */
  797. rt_thread_t rt_thread_find(char *name)
  798. {
  799. return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
  800. }
  801. RTM_EXPORT(rt_thread_find);
  802. /**@}*/