thread.c 28 KB

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