thread.c 25 KB

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