thread.c 21 KB

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