thread.c 20 KB

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