thread.c 21 KB

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