thread.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * File : thread.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-28 Bernard first version
  23. * 2006-04-29 Bernard implement thread timer
  24. * 2006-04-30 Bernard added THREAD_DEBUG
  25. * 2006-05-27 Bernard fixed the rt_thread_yield bug
  26. * 2006-06-03 Bernard fixed the thread timer init bug
  27. * 2006-08-10 Bernard fixed the timer bug in thread_sleep
  28. * 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach
  29. * 2006-09-03 Bernard implement rt_thread_detach
  30. * 2008-02-16 Bernard fixed the rt_thread_timeout bug
  31. * 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to
  32. * RT_EOK.
  33. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  34. * 2011-09-01 Bernard fixed rt_thread_exit issue when the current
  35. * thread preempted, which reported by Jiaxing Lee.
  36. * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.
  37. * 2012-12-29 Bernard fixed compiling warning.
  38. * 2016-08-09 ArdaFu add thread suspend and resume hook.
  39. * 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach
  40. bug when thread has not startup.
  41. */
  42. #include <rtthread.h>
  43. #include <rthw.h>
  44. extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  45. extern struct rt_thread *rt_current_thread;
  46. extern rt_list_t rt_thread_defunct;
  47. #ifdef RT_USING_HOOK
  48. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  49. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  50. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  51. /**
  52. * @ingroup Hook
  53. * This function sets a hook function when the system suspend a thread.
  54. *
  55. * @param hook the specified hook function
  56. *
  57. * @note the hook function must be simple and never be blocked or suspend.
  58. */
  59. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  60. {
  61. rt_thread_suspend_hook = hook;
  62. }
  63. /**
  64. * @ingroup Hook
  65. * This function sets a hook function when the system resume a thread.
  66. *
  67. * @param hook the specified hook function
  68. *
  69. * @note the hook function must be simple and never be blocked or suspend.
  70. */
  71. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  72. {
  73. rt_thread_resume_hook = hook;
  74. }
  75. /**
  76. * @ingroup Hook
  77. * This function sets a hook function when a thread is initialized.
  78. *
  79. * @param hook the specified hook function
  80. */
  81. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  82. {
  83. rt_thread_inited_hook = hook;
  84. }
  85. #endif
  86. void rt_thread_exit(void)
  87. {
  88. struct rt_thread *thread;
  89. register rt_base_t level;
  90. /* get current thread */
  91. thread = rt_current_thread;
  92. /* disable interrupt */
  93. level = rt_hw_interrupt_disable();
  94. /* remove from schedule */
  95. rt_schedule_remove_thread(thread);
  96. /* change stat */
  97. thread->stat = RT_THREAD_CLOSE;
  98. /* remove it from timer list */
  99. rt_timer_detach(&thread->thread_timer);
  100. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
  101. thread->cleanup == RT_NULL)
  102. {
  103. rt_object_detach((rt_object_t)thread);
  104. }
  105. else
  106. {
  107. /* insert to defunct thread list */
  108. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  109. }
  110. /* enable interrupt */
  111. rt_hw_interrupt_enable(level);
  112. /* switch to next task */
  113. rt_schedule();
  114. }
  115. static rt_err_t _rt_thread_init(struct rt_thread *thread,
  116. const char *name,
  117. void (*entry)(void *parameter),
  118. void *parameter,
  119. void *stack_start,
  120. rt_uint32_t stack_size,
  121. rt_uint8_t priority,
  122. rt_uint32_t tick)
  123. {
  124. /* init thread list */
  125. rt_list_init(&(thread->tlist));
  126. thread->entry = (void *)entry;
  127. thread->parameter = parameter;
  128. /* stack init */
  129. thread->stack_addr = stack_start;
  130. thread->stack_size = stack_size;
  131. /* init thread stack */
  132. rt_memset(thread->stack_addr, '#', thread->stack_size);
  133. #ifdef RT_STACK_UPWARD_GROW
  134. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  135. (void *)((char *)thread->stack_addr),
  136. (void *)rt_thread_exit);
  137. #else
  138. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  139. (void *)((char *)thread->stack_addr + thread->stack_size - 4),
  140. (void *)rt_thread_exit);
  141. #endif
  142. /* priority init */
  143. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  144. thread->init_priority = priority;
  145. thread->current_priority = priority;
  146. thread->number_mask = 0;
  147. #if RT_THREAD_PRIORITY_MAX > 32
  148. thread->number = 0;
  149. thread->high_mask = 0;
  150. #endif
  151. /* tick init */
  152. thread->init_tick = tick;
  153. thread->remaining_tick = tick;
  154. /* error and flags */
  155. thread->error = RT_EOK;
  156. thread->stat = RT_THREAD_INIT;
  157. /* initialize cleanup function and user data */
  158. thread->cleanup = 0;
  159. thread->user_data = 0;
  160. /* init thread timer */
  161. rt_timer_init(&(thread->thread_timer),
  162. thread->name,
  163. rt_thread_timeout,
  164. thread,
  165. 0,
  166. RT_TIMER_FLAG_ONE_SHOT);
  167. #ifdef RT_USING_MODULE
  168. thread->module_id = RT_NULL;
  169. #endif
  170. /* initialize signal */
  171. #ifdef RT_USING_SIGNALS
  172. thread->sig_mask = 0x00;
  173. thread->sig_pending = 0x00;
  174. thread->sig_ret = RT_NULL;
  175. thread->sig_vectors = RT_NULL;
  176. thread->si_list = RT_NULL;
  177. #endif
  178. #ifdef RT_USING_LWP
  179. thread->lwp = RT_NULL;
  180. #endif
  181. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  182. return RT_EOK;
  183. }
  184. /**
  185. * @addtogroup Thread
  186. */
  187. /**@{*/
  188. /**
  189. * This function will initialize a thread, normally it's used to initialize a
  190. * static thread object.
  191. *
  192. * @param thread the static thread object
  193. * @param name the name of thread, which shall be unique
  194. * @param entry the entry function of thread
  195. * @param parameter the parameter of thread enter function
  196. * @param stack_start the start address of thread stack
  197. * @param stack_size the size of thread stack
  198. * @param priority the priority of thread
  199. * @param tick the time slice if there are same priority thread
  200. *
  201. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  202. */
  203. rt_err_t rt_thread_init(struct rt_thread *thread,
  204. const char *name,
  205. void (*entry)(void *parameter),
  206. void *parameter,
  207. void *stack_start,
  208. rt_uint32_t stack_size,
  209. rt_uint8_t priority,
  210. rt_uint32_t tick)
  211. {
  212. /* thread check */
  213. RT_ASSERT(thread != RT_NULL);
  214. RT_ASSERT(stack_start != RT_NULL);
  215. /* init thread object */
  216. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  217. return _rt_thread_init(thread,
  218. name,
  219. entry,
  220. parameter,
  221. stack_start,
  222. stack_size,
  223. priority,
  224. tick);
  225. }
  226. RTM_EXPORT(rt_thread_init);
  227. /**
  228. * This function will return self thread object
  229. *
  230. * @return the self thread object
  231. */
  232. rt_thread_t rt_thread_self(void)
  233. {
  234. return rt_current_thread;
  235. }
  236. RTM_EXPORT(rt_thread_self);
  237. /**
  238. * This function will start a thread and put it to system ready queue
  239. *
  240. * @param thread the thread to be started
  241. *
  242. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  243. */
  244. rt_err_t rt_thread_startup(rt_thread_t thread)
  245. {
  246. /* thread check */
  247. RT_ASSERT(thread != RT_NULL);
  248. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  249. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  250. /* set current priority to init priority */
  251. thread->current_priority = thread->init_priority;
  252. /* calculate priority attribute */
  253. #if RT_THREAD_PRIORITY_MAX > 32
  254. thread->number = thread->current_priority >> 3; /* 5bit */
  255. thread->number_mask = 1L << thread->number;
  256. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  257. #else
  258. thread->number_mask = 1L << thread->current_priority;
  259. #endif
  260. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  261. thread->name, thread->init_priority));
  262. /* change thread stat */
  263. thread->stat = RT_THREAD_SUSPEND;
  264. /* then resume it */
  265. rt_thread_resume(thread);
  266. if (rt_thread_self() != RT_NULL)
  267. {
  268. /* do a scheduling */
  269. rt_schedule();
  270. }
  271. return RT_EOK;
  272. }
  273. RTM_EXPORT(rt_thread_startup);
  274. /**
  275. * This function will detach a thread. The thread object will be removed from
  276. * thread queue and detached/deleted from system object management.
  277. *
  278. * @param thread the thread to be deleted
  279. *
  280. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  281. */
  282. rt_err_t rt_thread_detach(rt_thread_t thread)
  283. {
  284. rt_base_t lock;
  285. /* thread check */
  286. RT_ASSERT(thread != RT_NULL);
  287. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  288. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  289. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  290. {
  291. /* remove from schedule */
  292. rt_schedule_remove_thread(thread);
  293. }
  294. /* release thread timer */
  295. rt_timer_detach(&(thread->thread_timer));
  296. /* change stat */
  297. thread->stat = RT_THREAD_CLOSE;
  298. /* detach object */
  299. rt_object_detach((rt_object_t)thread);
  300. if (thread->cleanup != RT_NULL)
  301. {
  302. /* disable interrupt */
  303. lock = rt_hw_interrupt_disable();
  304. /* insert to defunct thread list */
  305. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  306. /* enable interrupt */
  307. rt_hw_interrupt_enable(lock);
  308. }
  309. return RT_EOK;
  310. }
  311. RTM_EXPORT(rt_thread_detach);
  312. #ifdef RT_USING_HEAP
  313. /**
  314. * This function will create a thread object and allocate thread object memory
  315. * and stack.
  316. *
  317. * @param name the name of thread, which shall be unique
  318. * @param entry the entry function of thread
  319. * @param parameter the parameter of thread enter function
  320. * @param stack_size the size of thread stack
  321. * @param priority the priority of thread
  322. * @param tick the time slice if there are same priority thread
  323. *
  324. * @return the created thread object
  325. */
  326. rt_thread_t rt_thread_create(const char *name,
  327. void (*entry)(void *parameter),
  328. void *parameter,
  329. rt_uint32_t stack_size,
  330. rt_uint8_t priority,
  331. rt_uint32_t tick)
  332. {
  333. struct rt_thread *thread;
  334. void *stack_start;
  335. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  336. name);
  337. if (thread == RT_NULL)
  338. return RT_NULL;
  339. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  340. if (stack_start == RT_NULL)
  341. {
  342. /* allocate stack failure */
  343. rt_object_delete((rt_object_t)thread);
  344. return RT_NULL;
  345. }
  346. _rt_thread_init(thread,
  347. name,
  348. entry,
  349. parameter,
  350. stack_start,
  351. stack_size,
  352. priority,
  353. tick);
  354. return thread;
  355. }
  356. RTM_EXPORT(rt_thread_create);
  357. /**
  358. * This function will delete a thread. The thread object will be removed from
  359. * thread queue and deleted from system object management in the idle thread.
  360. *
  361. * @param thread the thread to be deleted
  362. *
  363. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  364. */
  365. rt_err_t rt_thread_delete(rt_thread_t thread)
  366. {
  367. rt_base_t lock;
  368. /* thread check */
  369. RT_ASSERT(thread != RT_NULL);
  370. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  371. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  372. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  373. {
  374. /* remove from schedule */
  375. rt_schedule_remove_thread(thread);
  376. }
  377. /* release thread timer */
  378. rt_timer_detach(&(thread->thread_timer));
  379. /* change stat */
  380. thread->stat = RT_THREAD_CLOSE;
  381. /* disable interrupt */
  382. lock = rt_hw_interrupt_disable();
  383. /* insert to defunct thread list */
  384. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  385. /* enable interrupt */
  386. rt_hw_interrupt_enable(lock);
  387. return RT_EOK;
  388. }
  389. RTM_EXPORT(rt_thread_delete);
  390. #endif
  391. /**
  392. * This function will let current thread yield processor, and scheduler will
  393. * choose a highest thread to run. After yield processor, the current thread
  394. * is still in READY state.
  395. *
  396. * @return RT_EOK
  397. */
  398. rt_err_t rt_thread_yield(void)
  399. {
  400. register rt_base_t level;
  401. struct rt_thread *thread;
  402. /* disable interrupt */
  403. level = rt_hw_interrupt_disable();
  404. /* set to current thread */
  405. thread = rt_current_thread;
  406. /* if the thread stat is READY and on ready queue list */
  407. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY &&
  408. thread->tlist.next != thread->tlist.prev)
  409. {
  410. /* remove thread from thread list */
  411. rt_list_remove(&(thread->tlist));
  412. /* put thread to end of ready queue */
  413. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  414. &(thread->tlist));
  415. /* enable interrupt */
  416. rt_hw_interrupt_enable(level);
  417. rt_schedule();
  418. return RT_EOK;
  419. }
  420. /* enable interrupt */
  421. rt_hw_interrupt_enable(level);
  422. return RT_EOK;
  423. }
  424. RTM_EXPORT(rt_thread_yield);
  425. /**
  426. * This function will let current thread sleep for some ticks.
  427. *
  428. * @param tick the sleep ticks
  429. *
  430. * @return RT_EOK
  431. */
  432. rt_err_t rt_thread_sleep(rt_tick_t tick)
  433. {
  434. register rt_base_t temp;
  435. struct rt_thread *thread;
  436. /* disable interrupt */
  437. temp = rt_hw_interrupt_disable();
  438. /* set to current thread */
  439. thread = rt_current_thread;
  440. RT_ASSERT(thread != RT_NULL);
  441. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  442. /* suspend thread */
  443. rt_thread_suspend(thread);
  444. /* reset the timeout of thread timer and start it */
  445. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  446. rt_timer_start(&(thread->thread_timer));
  447. /* enable interrupt */
  448. rt_hw_interrupt_enable(temp);
  449. rt_schedule();
  450. /* clear error number of this thread to RT_EOK */
  451. if (thread->error == -RT_ETIMEOUT)
  452. thread->error = RT_EOK;
  453. return RT_EOK;
  454. }
  455. /**
  456. * This function will let current thread delay for some ticks.
  457. *
  458. * @param tick the delay ticks
  459. *
  460. * @return RT_EOK
  461. */
  462. rt_err_t rt_thread_delay(rt_tick_t tick)
  463. {
  464. return rt_thread_sleep(tick);
  465. }
  466. RTM_EXPORT(rt_thread_delay);
  467. /**
  468. * This function will let current thread delay for some milliseconds.
  469. *
  470. * @param tick the delay time
  471. *
  472. * @return RT_EOK
  473. */
  474. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  475. {
  476. rt_tick_t tick;
  477. tick = rt_tick_from_millisecond(ms);
  478. return rt_thread_sleep(tick);
  479. }
  480. RTM_EXPORT(rt_thread_mdelay);
  481. /**
  482. * This function will control thread behaviors according to control command.
  483. *
  484. * @param thread the specified thread to be controlled
  485. * @param cmd the control command, which includes
  486. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  487. * RT_THREAD_CTRL_STARTUP for starting a thread;
  488. * RT_THREAD_CTRL_CLOSE for delete a thread.
  489. * @param arg the argument of control command
  490. *
  491. * @return RT_EOK
  492. */
  493. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  494. {
  495. register rt_base_t temp;
  496. /* thread check */
  497. RT_ASSERT(thread != RT_NULL);
  498. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  499. switch (cmd)
  500. {
  501. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  502. /* disable interrupt */
  503. temp = rt_hw_interrupt_disable();
  504. /* for ready thread, change queue */
  505. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  506. {
  507. /* remove thread from schedule queue first */
  508. rt_schedule_remove_thread(thread);
  509. /* change thread priority */
  510. thread->current_priority = *(rt_uint8_t *)arg;
  511. /* recalculate priority attribute */
  512. #if RT_THREAD_PRIORITY_MAX > 32
  513. thread->number = thread->current_priority >> 3; /* 5bit */
  514. thread->number_mask = 1 << thread->number;
  515. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  516. #else
  517. thread->number_mask = 1 << thread->current_priority;
  518. #endif
  519. /* insert thread to schedule queue again */
  520. rt_schedule_insert_thread(thread);
  521. }
  522. else
  523. {
  524. thread->current_priority = *(rt_uint8_t *)arg;
  525. /* recalculate priority attribute */
  526. #if RT_THREAD_PRIORITY_MAX > 32
  527. thread->number = thread->current_priority >> 3; /* 5bit */
  528. thread->number_mask = 1 << thread->number;
  529. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  530. #else
  531. thread->number_mask = 1 << thread->current_priority;
  532. #endif
  533. }
  534. /* enable interrupt */
  535. rt_hw_interrupt_enable(temp);
  536. break;
  537. case RT_THREAD_CTRL_STARTUP:
  538. return rt_thread_startup(thread);
  539. #ifdef RT_USING_HEAP
  540. case RT_THREAD_CTRL_CLOSE:
  541. return rt_thread_delete(thread);
  542. #endif
  543. default:
  544. break;
  545. }
  546. return RT_EOK;
  547. }
  548. RTM_EXPORT(rt_thread_control);
  549. /**
  550. * This function will suspend the specified thread.
  551. *
  552. * @param thread the thread to be suspended
  553. *
  554. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  555. *
  556. * @note if suspend self thread, after this function call, the
  557. * rt_schedule() must be invoked.
  558. */
  559. rt_err_t rt_thread_suspend(rt_thread_t thread)
  560. {
  561. register rt_base_t temp;
  562. /* thread check */
  563. RT_ASSERT(thread != RT_NULL);
  564. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  565. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  566. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
  567. {
  568. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
  569. thread->stat));
  570. return -RT_ERROR;
  571. }
  572. /* disable interrupt */
  573. temp = rt_hw_interrupt_disable();
  574. /* change thread stat */
  575. thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
  576. rt_schedule_remove_thread(thread);
  577. /* stop thread timer anyway */
  578. rt_timer_stop(&(thread->thread_timer));
  579. /* enable interrupt */
  580. rt_hw_interrupt_enable(temp);
  581. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  582. return RT_EOK;
  583. }
  584. RTM_EXPORT(rt_thread_suspend);
  585. /**
  586. * This function will resume a thread and put it to system ready queue.
  587. *
  588. * @param thread the thread to be resumed
  589. *
  590. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  591. */
  592. rt_err_t rt_thread_resume(rt_thread_t thread)
  593. {
  594. register rt_base_t temp;
  595. /* thread check */
  596. RT_ASSERT(thread != RT_NULL);
  597. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  598. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  599. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
  600. {
  601. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  602. thread->stat));
  603. return -RT_ERROR;
  604. }
  605. /* disable interrupt */
  606. temp = rt_hw_interrupt_disable();
  607. /* remove from suspend list */
  608. rt_list_remove(&(thread->tlist));
  609. rt_timer_stop(&thread->thread_timer);
  610. /* enable interrupt */
  611. rt_hw_interrupt_enable(temp);
  612. /* insert to schedule ready list */
  613. rt_schedule_insert_thread(thread);
  614. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  615. return RT_EOK;
  616. }
  617. RTM_EXPORT(rt_thread_resume);
  618. /**
  619. * This function is the timeout function for thread, normally which is invoked
  620. * when thread is timeout to wait some resource.
  621. *
  622. * @param parameter the parameter of thread timeout function
  623. */
  624. void rt_thread_timeout(void *parameter)
  625. {
  626. struct rt_thread *thread;
  627. thread = (struct rt_thread *)parameter;
  628. /* thread check */
  629. RT_ASSERT(thread != RT_NULL);
  630. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
  631. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  632. /* set error number */
  633. thread->error = -RT_ETIMEOUT;
  634. /* remove from suspend list */
  635. rt_list_remove(&(thread->tlist));
  636. /* insert to schedule ready list */
  637. rt_schedule_insert_thread(thread);
  638. /* do schedule */
  639. rt_schedule();
  640. }
  641. RTM_EXPORT(rt_thread_timeout);
  642. /**
  643. * This function will find the specified thread.
  644. *
  645. * @param name the name of thread finding
  646. *
  647. * @return the found thread
  648. *
  649. * @note please don't invoke this function in interrupt status.
  650. */
  651. rt_thread_t rt_thread_find(char *name)
  652. {
  653. struct rt_object_information *information;
  654. struct rt_object *object;
  655. struct rt_list_node *node;
  656. /* enter critical */
  657. if (rt_thread_self() != RT_NULL)
  658. rt_enter_critical();
  659. /* try to find device object */
  660. information = rt_object_get_information(RT_Object_Class_Thread);
  661. RT_ASSERT(information != RT_NULL);
  662. for (node = information->object_list.next;
  663. node != &(information->object_list);
  664. node = node->next)
  665. {
  666. object = rt_list_entry(node, struct rt_object, list);
  667. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  668. {
  669. /* leave critical */
  670. if (rt_thread_self() != RT_NULL)
  671. rt_exit_critical();
  672. return (rt_thread_t)object;
  673. }
  674. }
  675. /* leave critical */
  676. if (rt_thread_self() != RT_NULL)
  677. rt_exit_critical();
  678. /* not found */
  679. return RT_NULL;
  680. }
  681. RTM_EXPORT(rt_thread_find);
  682. /**@}*/