thread.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  134. (void *)((char *)thread->stack_addr + thread->stack_size - 4),
  135. (void *)rt_thread_exit);
  136. /* priority init */
  137. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  138. thread->init_priority = priority;
  139. thread->current_priority = priority;
  140. thread->number_mask = 0;
  141. #if RT_THREAD_PRIORITY_MAX > 32
  142. thread->number = 0;
  143. thread->high_mask = 0;
  144. #endif
  145. /* tick init */
  146. thread->init_tick = tick;
  147. thread->remaining_tick = tick;
  148. /* error and flags */
  149. thread->error = RT_EOK;
  150. thread->stat = RT_THREAD_INIT;
  151. /* initialize cleanup function and user data */
  152. thread->cleanup = 0;
  153. thread->user_data = 0;
  154. /* init thread timer */
  155. rt_timer_init(&(thread->thread_timer),
  156. thread->name,
  157. rt_thread_timeout,
  158. thread,
  159. 0,
  160. RT_TIMER_FLAG_ONE_SHOT);
  161. /* initialize signal */
  162. #ifdef RT_USING_SIGNALS
  163. thread->sig_mask = 0x00;
  164. thread->sig_pending = 0x00;
  165. thread->sig_ret = RT_NULL;
  166. thread->sig_vectors = RT_NULL;
  167. thread->si_list = RT_NULL;
  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. * This function will initialize a thread, normally it's used to initialize a
  178. * static thread object.
  179. *
  180. * @param thread the static thread object
  181. * @param name the name of thread, which shall be unique
  182. * @param entry the entry function of thread
  183. * @param parameter the parameter of thread enter function
  184. * @param stack_start the start address of thread stack
  185. * @param stack_size the size of thread stack
  186. * @param priority the priority of thread
  187. * @param tick the time slice if there are same priority thread
  188. *
  189. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  190. */
  191. rt_err_t rt_thread_init(struct rt_thread *thread,
  192. const char *name,
  193. void (*entry)(void *parameter),
  194. void *parameter,
  195. void *stack_start,
  196. rt_uint32_t stack_size,
  197. rt_uint8_t priority,
  198. rt_uint32_t tick)
  199. {
  200. /* thread check */
  201. RT_ASSERT(thread != RT_NULL);
  202. RT_ASSERT(stack_start != RT_NULL);
  203. /* init thread object */
  204. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  205. return _rt_thread_init(thread,
  206. name,
  207. entry,
  208. parameter,
  209. stack_start,
  210. stack_size,
  211. priority,
  212. tick);
  213. }
  214. RTM_EXPORT(rt_thread_init);
  215. /**
  216. * This function will return self thread object
  217. *
  218. * @return the self thread object
  219. */
  220. rt_thread_t rt_thread_self(void)
  221. {
  222. return rt_current_thread;
  223. }
  224. RTM_EXPORT(rt_thread_self);
  225. /**
  226. * This function will start a thread and put it to system ready queue
  227. *
  228. * @param thread the thread to be started
  229. *
  230. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  231. */
  232. rt_err_t rt_thread_startup(rt_thread_t thread)
  233. {
  234. /* thread check */
  235. RT_ASSERT(thread != RT_NULL);
  236. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  237. /* set current priority to init priority */
  238. thread->current_priority = thread->init_priority;
  239. /* calculate priority attribute */
  240. #if RT_THREAD_PRIORITY_MAX > 32
  241. thread->number = thread->current_priority >> 3; /* 5bit */
  242. thread->number_mask = 1L << thread->number;
  243. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  244. #else
  245. thread->number_mask = 1L << thread->current_priority;
  246. #endif
  247. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  248. thread->name, thread->init_priority));
  249. /* change thread stat */
  250. thread->stat = RT_THREAD_SUSPEND;
  251. /* then resume it */
  252. rt_thread_resume(thread);
  253. if (rt_thread_self() != RT_NULL)
  254. {
  255. /* do a scheduling */
  256. rt_schedule();
  257. }
  258. return RT_EOK;
  259. }
  260. RTM_EXPORT(rt_thread_startup);
  261. /**
  262. * This function will detach a thread. The thread object will be removed from
  263. * thread queue and detached/deleted from system object management.
  264. *
  265. * @param thread the thread to be deleted
  266. *
  267. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  268. */
  269. rt_err_t rt_thread_detach(rt_thread_t thread)
  270. {
  271. rt_base_t lock;
  272. /* thread check */
  273. RT_ASSERT(thread != RT_NULL);
  274. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  275. {
  276. /* remove from schedule */
  277. rt_schedule_remove_thread(thread);
  278. }
  279. /* release thread timer */
  280. rt_timer_detach(&(thread->thread_timer));
  281. /* change stat */
  282. thread->stat = RT_THREAD_CLOSE;
  283. /* detach object */
  284. rt_object_detach((rt_object_t)thread);
  285. if (thread->cleanup != RT_NULL)
  286. {
  287. /* disable interrupt */
  288. lock = rt_hw_interrupt_disable();
  289. /* insert to defunct thread list */
  290. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  291. /* enable interrupt */
  292. rt_hw_interrupt_enable(lock);
  293. }
  294. return RT_EOK;
  295. }
  296. RTM_EXPORT(rt_thread_detach);
  297. #ifdef RT_USING_HEAP
  298. /**
  299. * This function will create a thread object and allocate thread object memory
  300. * and stack.
  301. *
  302. * @param name the name of thread, which shall be unique
  303. * @param entry the entry function of thread
  304. * @param parameter the parameter of thread enter function
  305. * @param stack_size the size of thread stack
  306. * @param priority the priority of thread
  307. * @param tick the time slice if there are same priority thread
  308. *
  309. * @return the created thread object
  310. */
  311. rt_thread_t rt_thread_create(const char *name,
  312. void (*entry)(void *parameter),
  313. void *parameter,
  314. rt_uint32_t stack_size,
  315. rt_uint8_t priority,
  316. rt_uint32_t tick)
  317. {
  318. struct rt_thread *thread;
  319. void *stack_start;
  320. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  321. name);
  322. if (thread == RT_NULL)
  323. return RT_NULL;
  324. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  325. if (stack_start == RT_NULL)
  326. {
  327. /* allocate stack failure */
  328. rt_object_delete((rt_object_t)thread);
  329. return RT_NULL;
  330. }
  331. _rt_thread_init(thread,
  332. name,
  333. entry,
  334. parameter,
  335. stack_start,
  336. stack_size,
  337. priority,
  338. tick);
  339. return thread;
  340. }
  341. RTM_EXPORT(rt_thread_create);
  342. /**
  343. * This function will delete a thread. The thread object will be removed from
  344. * thread queue and deleted from system object management in the idle thread.
  345. *
  346. * @param thread the thread to be deleted
  347. *
  348. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  349. */
  350. rt_err_t rt_thread_delete(rt_thread_t thread)
  351. {
  352. rt_base_t lock;
  353. /* thread check */
  354. RT_ASSERT(thread != RT_NULL);
  355. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  356. {
  357. /* remove from schedule */
  358. rt_schedule_remove_thread(thread);
  359. }
  360. /* release thread timer */
  361. rt_timer_detach(&(thread->thread_timer));
  362. /* change stat */
  363. thread->stat = RT_THREAD_CLOSE;
  364. /* disable interrupt */
  365. lock = rt_hw_interrupt_disable();
  366. /* insert to defunct thread list */
  367. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  368. /* enable interrupt */
  369. rt_hw_interrupt_enable(lock);
  370. return RT_EOK;
  371. }
  372. RTM_EXPORT(rt_thread_delete);
  373. #endif
  374. /**
  375. * This function will let current thread yield processor, and scheduler will
  376. * choose a highest thread to run. After yield processor, the current thread
  377. * is still in READY state.
  378. *
  379. * @return RT_EOK
  380. */
  381. rt_err_t rt_thread_yield(void)
  382. {
  383. register rt_base_t level;
  384. struct rt_thread *thread;
  385. /* disable interrupt */
  386. level = rt_hw_interrupt_disable();
  387. /* set to current thread */
  388. thread = rt_current_thread;
  389. /* if the thread stat is READY and on ready queue list */
  390. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY &&
  391. thread->tlist.next != thread->tlist.prev)
  392. {
  393. /* remove thread from thread list */
  394. rt_list_remove(&(thread->tlist));
  395. /* put thread to end of ready queue */
  396. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  397. &(thread->tlist));
  398. /* enable interrupt */
  399. rt_hw_interrupt_enable(level);
  400. rt_schedule();
  401. return RT_EOK;
  402. }
  403. /* enable interrupt */
  404. rt_hw_interrupt_enable(level);
  405. return RT_EOK;
  406. }
  407. RTM_EXPORT(rt_thread_yield);
  408. /**
  409. * This function will let current thread sleep for some ticks.
  410. *
  411. * @param tick the sleep ticks
  412. *
  413. * @return RT_EOK
  414. */
  415. rt_err_t rt_thread_sleep(rt_tick_t tick)
  416. {
  417. register rt_base_t temp;
  418. struct rt_thread *thread;
  419. /* disable interrupt */
  420. temp = rt_hw_interrupt_disable();
  421. /* set to current thread */
  422. thread = rt_current_thread;
  423. RT_ASSERT(thread != RT_NULL);
  424. /* suspend thread */
  425. rt_thread_suspend(thread);
  426. /* reset the timeout of thread timer and start it */
  427. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  428. rt_timer_start(&(thread->thread_timer));
  429. /* enable interrupt */
  430. rt_hw_interrupt_enable(temp);
  431. rt_schedule();
  432. /* clear error number of this thread to RT_EOK */
  433. if (thread->error == -RT_ETIMEOUT)
  434. thread->error = RT_EOK;
  435. return RT_EOK;
  436. }
  437. /**
  438. * This function will let current thread delay for some ticks.
  439. *
  440. * @param tick the delay ticks
  441. *
  442. * @return RT_EOK
  443. */
  444. rt_err_t rt_thread_delay(rt_tick_t tick)
  445. {
  446. return rt_thread_sleep(tick);
  447. }
  448. RTM_EXPORT(rt_thread_delay);
  449. /**
  450. * This function will control thread behaviors according to control command.
  451. *
  452. * @param thread the specified thread to be controlled
  453. * @param cmd the control command, which includes
  454. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  455. * RT_THREAD_CTRL_STARTUP for starting a thread;
  456. * RT_THREAD_CTRL_CLOSE for delete a thread.
  457. * @param arg the argument of control command
  458. *
  459. * @return RT_EOK
  460. */
  461. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  462. {
  463. register rt_base_t temp;
  464. /* thread check */
  465. RT_ASSERT(thread != RT_NULL);
  466. switch (cmd)
  467. {
  468. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  469. /* disable interrupt */
  470. temp = rt_hw_interrupt_disable();
  471. /* for ready thread, change queue */
  472. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  473. {
  474. /* remove thread from schedule queue first */
  475. rt_schedule_remove_thread(thread);
  476. /* change thread priority */
  477. thread->current_priority = *(rt_uint8_t *)arg;
  478. /* recalculate priority attribute */
  479. #if RT_THREAD_PRIORITY_MAX > 32
  480. thread->number = thread->current_priority >> 3; /* 5bit */
  481. thread->number_mask = 1 << thread->number;
  482. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  483. #else
  484. thread->number_mask = 1 << thread->current_priority;
  485. #endif
  486. /* insert thread to schedule queue again */
  487. rt_schedule_insert_thread(thread);
  488. }
  489. else
  490. {
  491. thread->current_priority = *(rt_uint8_t *)arg;
  492. /* recalculate priority attribute */
  493. #if RT_THREAD_PRIORITY_MAX > 32
  494. thread->number = thread->current_priority >> 3; /* 5bit */
  495. thread->number_mask = 1 << thread->number;
  496. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  497. #else
  498. thread->number_mask = 1 << thread->current_priority;
  499. #endif
  500. }
  501. /* enable interrupt */
  502. rt_hw_interrupt_enable(temp);
  503. break;
  504. case RT_THREAD_CTRL_STARTUP:
  505. return rt_thread_startup(thread);
  506. #ifdef RT_USING_HEAP
  507. case RT_THREAD_CTRL_CLOSE:
  508. return rt_thread_delete(thread);
  509. #endif
  510. default:
  511. break;
  512. }
  513. return RT_EOK;
  514. }
  515. RTM_EXPORT(rt_thread_control);
  516. /**
  517. * This function will suspend the specified thread.
  518. *
  519. * @param thread the thread to be suspended
  520. *
  521. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  522. *
  523. * @note if suspend self thread, after this function call, the
  524. * rt_schedule() must be invoked.
  525. */
  526. rt_err_t rt_thread_suspend(rt_thread_t thread)
  527. {
  528. register rt_base_t temp;
  529. /* thread check */
  530. RT_ASSERT(thread != RT_NULL);
  531. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  532. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
  533. {
  534. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, %d\n",
  535. thread->stat));
  536. return -RT_ERROR;
  537. }
  538. /* disable interrupt */
  539. temp = rt_hw_interrupt_disable();
  540. /* change thread stat */
  541. thread->stat = RT_THREAD_SUSPEND;
  542. rt_schedule_remove_thread(thread);
  543. /* stop thread timer anyway */
  544. rt_timer_stop(&(thread->thread_timer));
  545. /* enable interrupt */
  546. rt_hw_interrupt_enable(temp);
  547. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  548. return RT_EOK;
  549. }
  550. RTM_EXPORT(rt_thread_suspend);
  551. /**
  552. * This function will resume a thread and put it to system ready queue.
  553. *
  554. * @param thread the thread to be resumed
  555. *
  556. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  557. */
  558. rt_err_t rt_thread_resume(rt_thread_t thread)
  559. {
  560. register rt_base_t temp;
  561. /* thread check */
  562. RT_ASSERT(thread != RT_NULL);
  563. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  564. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
  565. {
  566. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  567. thread->stat));
  568. return -RT_ERROR;
  569. }
  570. /* disable interrupt */
  571. temp = rt_hw_interrupt_disable();
  572. /* remove from suspend list */
  573. rt_list_remove(&(thread->tlist));
  574. rt_timer_stop(&thread->thread_timer);
  575. /* enable interrupt */
  576. rt_hw_interrupt_enable(temp);
  577. /* insert to schedule ready list */
  578. rt_schedule_insert_thread(thread);
  579. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  580. return RT_EOK;
  581. }
  582. RTM_EXPORT(rt_thread_resume);
  583. /**
  584. * This function is the timeout function for thread, normally which is invoked
  585. * when thread is timeout to wait some resource.
  586. *
  587. * @param parameter the parameter of thread timeout function
  588. */
  589. void rt_thread_timeout(void *parameter)
  590. {
  591. struct rt_thread *thread;
  592. thread = (struct rt_thread *)parameter;
  593. /* thread check */
  594. RT_ASSERT(thread != RT_NULL);
  595. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
  596. /* set error number */
  597. thread->error = -RT_ETIMEOUT;
  598. /* remove from suspend list */
  599. rt_list_remove(&(thread->tlist));
  600. /* insert to schedule ready list */
  601. rt_schedule_insert_thread(thread);
  602. /* do schedule */
  603. rt_schedule();
  604. }
  605. RTM_EXPORT(rt_thread_timeout);
  606. /**
  607. * This function will find the specified thread.
  608. *
  609. * @param name the name of thread finding
  610. *
  611. * @return the found thread
  612. *
  613. * @note please don't invoke this function in interrupt status.
  614. */
  615. rt_thread_t rt_thread_find(char *name)
  616. {
  617. struct rt_object_information *information;
  618. struct rt_object *object;
  619. struct rt_list_node *node;
  620. extern struct rt_object_information rt_object_container[];
  621. /* enter critical */
  622. if (rt_thread_self() != RT_NULL)
  623. rt_enter_critical();
  624. /* try to find device object */
  625. information = &rt_object_container[RT_Object_Class_Thread];
  626. for (node = information->object_list.next;
  627. node != &(information->object_list);
  628. node = node->next)
  629. {
  630. object = rt_list_entry(node, struct rt_object, list);
  631. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  632. {
  633. /* leave critical */
  634. if (rt_thread_self() != RT_NULL)
  635. rt_exit_critical();
  636. return (rt_thread_t)object;
  637. }
  638. }
  639. /* leave critical */
  640. if (rt_thread_self() != RT_NULL)
  641. rt_exit_critical();
  642. /* not found */
  643. return RT_NULL;
  644. }
  645. RTM_EXPORT(rt_thread_find);
  646. /**@}*/