thread.c 27 KB

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