thread.c 25 KB

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