thread.c 23 KB

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