thread.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-28 Bernard first version
  9. * 2006-04-29 Bernard implement thread timer
  10. * 2006-04-30 Bernard added THREAD_DEBUG
  11. * 2006-05-27 Bernard fixed the rt_thread_yield bug
  12. * 2006-06-03 Bernard fixed the thread timer init bug
  13. * 2006-08-10 Bernard fixed the timer bug in thread_sleep
  14. * 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach
  15. * 2006-09-03 Bernard implement rt_thread_detach
  16. * 2008-02-16 Bernard fixed the rt_thread_timeout bug
  17. * 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to
  18. * RT_EOK.
  19. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  20. * 2011-09-01 Bernard fixed rt_thread_exit issue when the current
  21. * thread preempted, which reported by Jiaxing Lee.
  22. * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.
  23. * 2012-12-29 Bernard fixed compiling warning.
  24. * 2016-08-09 ArdaFu add thread suspend and resume hook.
  25. * 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach
  26. bug when thread has not startup.
  27. */
  28. #include <rtthread.h>
  29. #include <rthw.h>
  30. extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  31. extern struct rt_thread *rt_current_thread;
  32. extern rt_list_t rt_thread_defunct;
  33. #ifdef RT_USING_HOOK
  34. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  35. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  36. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  37. /**
  38. * @ingroup Hook
  39. * This function sets a hook function when the system suspend a thread.
  40. *
  41. * @param hook the specified hook function
  42. *
  43. * @note the hook function must be simple and never be blocked or suspend.
  44. */
  45. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  46. {
  47. rt_thread_suspend_hook = hook;
  48. }
  49. /**
  50. * @ingroup Hook
  51. * This function sets a hook function when the system resume a thread.
  52. *
  53. * @param hook the specified hook function
  54. *
  55. * @note the hook function must be simple and never be blocked or suspend.
  56. */
  57. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  58. {
  59. rt_thread_resume_hook = hook;
  60. }
  61. /**
  62. * @ingroup Hook
  63. * This function sets a hook function when a thread is initialized.
  64. *
  65. * @param hook the specified hook function
  66. */
  67. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  68. {
  69. rt_thread_inited_hook = hook;
  70. }
  71. #endif
  72. void rt_thread_exit(void)
  73. {
  74. struct rt_thread *thread;
  75. register rt_base_t level;
  76. /* get current thread */
  77. thread = rt_current_thread;
  78. /* disable interrupt */
  79. level = rt_hw_interrupt_disable();
  80. /* remove from schedule */
  81. rt_schedule_remove_thread(thread);
  82. /* change stat */
  83. thread->stat = RT_THREAD_CLOSE;
  84. /* remove it from timer list */
  85. rt_timer_detach(&thread->thread_timer);
  86. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
  87. thread->cleanup == RT_NULL)
  88. {
  89. rt_object_detach((rt_object_t)thread);
  90. }
  91. else
  92. {
  93. /* insert to defunct thread list */
  94. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  95. }
  96. /* enable interrupt */
  97. rt_hw_interrupt_enable(level);
  98. /* switch to next task */
  99. rt_schedule();
  100. }
  101. static rt_err_t _rt_thread_init(struct rt_thread *thread,
  102. const char *name,
  103. void (*entry)(void *parameter),
  104. void *parameter,
  105. void *stack_start,
  106. rt_uint32_t stack_size,
  107. rt_uint8_t priority,
  108. rt_uint32_t tick)
  109. {
  110. /* init thread list */
  111. rt_list_init(&(thread->tlist));
  112. thread->entry = (void *)entry;
  113. thread->parameter = parameter;
  114. /* stack init */
  115. thread->stack_addr = stack_start;
  116. thread->stack_size = stack_size;
  117. /* init thread stack */
  118. rt_memset(thread->stack_addr, '#', thread->stack_size);
  119. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  120. (void *)((char *)thread->stack_addr + thread->stack_size - 4),
  121. (void *)rt_thread_exit);
  122. /* priority init */
  123. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  124. thread->init_priority = priority;
  125. thread->current_priority = priority;
  126. thread->number_mask = 0;
  127. #if RT_THREAD_PRIORITY_MAX > 32
  128. thread->number = 0;
  129. thread->high_mask = 0;
  130. #endif
  131. /* tick init */
  132. thread->init_tick = tick;
  133. thread->remaining_tick = tick;
  134. /* error and flags */
  135. thread->error = RT_EOK;
  136. thread->stat = RT_THREAD_INIT;
  137. /* initialize cleanup function and user data */
  138. thread->cleanup = 0;
  139. thread->user_data = 0;
  140. /* init thread timer */
  141. rt_timer_init(&(thread->thread_timer),
  142. thread->name,
  143. rt_thread_timeout,
  144. thread,
  145. 0,
  146. RT_TIMER_FLAG_ONE_SHOT);
  147. /* initialize signal */
  148. #ifdef RT_USING_SIGNALS
  149. thread->sig_mask = 0x00;
  150. thread->sig_pending = 0x00;
  151. thread->sig_ret = RT_NULL;
  152. thread->sig_vectors = RT_NULL;
  153. thread->si_list = RT_NULL;
  154. #endif
  155. #ifdef RT_USING_LWP
  156. thread->lwp = RT_NULL;
  157. #endif
  158. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  159. return RT_EOK;
  160. }
  161. /**
  162. * @addtogroup Thread
  163. */
  164. /**@{*/
  165. /**
  166. * This function will initialize a thread, normally it's used to initialize a
  167. * static thread object.
  168. *
  169. * @param thread the static thread object
  170. * @param name the name of thread, which shall be unique
  171. * @param entry the entry function of thread
  172. * @param parameter the parameter of thread enter function
  173. * @param stack_start the start address of thread stack
  174. * @param stack_size the size of thread stack
  175. * @param priority the priority of thread
  176. * @param tick the time slice if there are same priority thread
  177. *
  178. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  179. */
  180. rt_err_t rt_thread_init(struct rt_thread *thread,
  181. const char *name,
  182. void (*entry)(void *parameter),
  183. void *parameter,
  184. void *stack_start,
  185. rt_uint32_t stack_size,
  186. rt_uint8_t priority,
  187. rt_uint32_t tick)
  188. {
  189. /* thread check */
  190. RT_ASSERT(thread != RT_NULL);
  191. RT_ASSERT(stack_start != RT_NULL);
  192. /* init thread object */
  193. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  194. return _rt_thread_init(thread,
  195. name,
  196. entry,
  197. parameter,
  198. stack_start,
  199. stack_size,
  200. priority,
  201. tick);
  202. }
  203. RTM_EXPORT(rt_thread_init);
  204. /**
  205. * This function will return self thread object
  206. *
  207. * @return the self thread object
  208. */
  209. rt_thread_t rt_thread_self(void)
  210. {
  211. return rt_current_thread;
  212. }
  213. RTM_EXPORT(rt_thread_self);
  214. /**
  215. * This function will start a thread and put it to system ready queue
  216. *
  217. * @param thread the thread to be started
  218. *
  219. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  220. */
  221. rt_err_t rt_thread_startup(rt_thread_t thread)
  222. {
  223. /* thread check */
  224. RT_ASSERT(thread != RT_NULL);
  225. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  226. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  227. /* set current priority to init priority */
  228. thread->current_priority = thread->init_priority;
  229. /* calculate priority attribute */
  230. #if RT_THREAD_PRIORITY_MAX > 32
  231. thread->number = thread->current_priority >> 3; /* 5bit */
  232. thread->number_mask = 1L << thread->number;
  233. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  234. #else
  235. thread->number_mask = 1L << thread->current_priority;
  236. #endif
  237. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  238. thread->name, thread->init_priority));
  239. /* change thread stat */
  240. thread->stat = RT_THREAD_SUSPEND;
  241. /* then resume it */
  242. rt_thread_resume(thread);
  243. if (rt_thread_self() != RT_NULL)
  244. {
  245. /* do a scheduling */
  246. rt_schedule();
  247. }
  248. return RT_EOK;
  249. }
  250. RTM_EXPORT(rt_thread_startup);
  251. /**
  252. * This function will detach a thread. The thread object will be removed from
  253. * thread queue and detached/deleted from system object management.
  254. *
  255. * @param thread the thread to be deleted
  256. *
  257. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  258. */
  259. rt_err_t rt_thread_detach(rt_thread_t thread)
  260. {
  261. rt_base_t lock;
  262. /* thread check */
  263. RT_ASSERT(thread != RT_NULL);
  264. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  265. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  266. if ((thread->stat & RT_THREAD_STAT_MASK) != 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. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  348. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  349. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  350. {
  351. /* remove from schedule */
  352. rt_schedule_remove_thread(thread);
  353. }
  354. /* release thread timer */
  355. rt_timer_detach(&(thread->thread_timer));
  356. /* change stat */
  357. thread->stat = RT_THREAD_CLOSE;
  358. /* disable interrupt */
  359. lock = rt_hw_interrupt_disable();
  360. /* insert to defunct thread list */
  361. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  362. /* enable interrupt */
  363. rt_hw_interrupt_enable(lock);
  364. return RT_EOK;
  365. }
  366. RTM_EXPORT(rt_thread_delete);
  367. #endif
  368. /**
  369. * This function will let current thread yield processor, and scheduler will
  370. * choose a highest thread to run. After yield processor, the current thread
  371. * is still in READY state.
  372. *
  373. * @return RT_EOK
  374. */
  375. rt_err_t rt_thread_yield(void)
  376. {
  377. register rt_base_t level;
  378. struct rt_thread *thread;
  379. /* disable interrupt */
  380. level = rt_hw_interrupt_disable();
  381. /* set to current thread */
  382. thread = rt_current_thread;
  383. /* if the thread stat is READY and on ready queue list */
  384. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY &&
  385. thread->tlist.next != thread->tlist.prev)
  386. {
  387. /* remove thread from thread list */
  388. rt_list_remove(&(thread->tlist));
  389. /* put thread to end of ready queue */
  390. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  391. &(thread->tlist));
  392. /* enable interrupt */
  393. rt_hw_interrupt_enable(level);
  394. rt_schedule();
  395. return RT_EOK;
  396. }
  397. /* enable interrupt */
  398. rt_hw_interrupt_enable(level);
  399. return RT_EOK;
  400. }
  401. RTM_EXPORT(rt_thread_yield);
  402. /**
  403. * This function will let current thread sleep for some ticks.
  404. *
  405. * @param tick the sleep ticks
  406. *
  407. * @return RT_EOK
  408. */
  409. rt_err_t rt_thread_sleep(rt_tick_t tick)
  410. {
  411. register rt_base_t temp;
  412. struct rt_thread *thread;
  413. /* disable interrupt */
  414. temp = rt_hw_interrupt_disable();
  415. /* set to current thread */
  416. thread = rt_current_thread;
  417. RT_ASSERT(thread != RT_NULL);
  418. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  419. /* suspend thread */
  420. rt_thread_suspend(thread);
  421. /* reset the timeout of thread timer and start it */
  422. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  423. rt_timer_start(&(thread->thread_timer));
  424. /* enable interrupt */
  425. rt_hw_interrupt_enable(temp);
  426. rt_schedule();
  427. /* clear error number of this thread to RT_EOK */
  428. if (thread->error == -RT_ETIMEOUT)
  429. thread->error = RT_EOK;
  430. return RT_EOK;
  431. }
  432. /**
  433. * This function will let current thread delay for some ticks.
  434. *
  435. * @param tick the delay ticks
  436. *
  437. * @return RT_EOK
  438. */
  439. rt_err_t rt_thread_delay(rt_tick_t tick)
  440. {
  441. return rt_thread_sleep(tick);
  442. }
  443. RTM_EXPORT(rt_thread_delay);
  444. /**
  445. * This function will let current thread delay for some milliseconds.
  446. *
  447. * @param tick the delay time
  448. *
  449. * @return RT_EOK
  450. */
  451. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  452. {
  453. rt_tick_t tick;
  454. tick = rt_tick_from_millisecond(ms);
  455. return rt_thread_sleep(tick);
  456. }
  457. RTM_EXPORT(rt_thread_mdelay);
  458. /**
  459. * This function will control thread behaviors according to control command.
  460. *
  461. * @param thread the specified thread to be controlled
  462. * @param cmd the control command, which includes
  463. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  464. * RT_THREAD_CTRL_STARTUP for starting a thread;
  465. * RT_THREAD_CTRL_CLOSE for delete a thread.
  466. * @param arg the argument of control command
  467. *
  468. * @return RT_EOK
  469. */
  470. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  471. {
  472. register rt_base_t temp;
  473. /* thread check */
  474. RT_ASSERT(thread != RT_NULL);
  475. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  476. switch (cmd)
  477. {
  478. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  479. /* disable interrupt */
  480. temp = rt_hw_interrupt_disable();
  481. /* for ready thread, change queue */
  482. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  483. {
  484. /* remove thread from schedule queue first */
  485. rt_schedule_remove_thread(thread);
  486. /* change thread priority */
  487. thread->current_priority = *(rt_uint8_t *)arg;
  488. /* recalculate priority attribute */
  489. #if RT_THREAD_PRIORITY_MAX > 32
  490. thread->number = thread->current_priority >> 3; /* 5bit */
  491. thread->number_mask = 1 << thread->number;
  492. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  493. #else
  494. thread->number_mask = 1 << thread->current_priority;
  495. #endif
  496. /* insert thread to schedule queue again */
  497. rt_schedule_insert_thread(thread);
  498. }
  499. else
  500. {
  501. thread->current_priority = *(rt_uint8_t *)arg;
  502. /* recalculate priority attribute */
  503. #if RT_THREAD_PRIORITY_MAX > 32
  504. thread->number = thread->current_priority >> 3; /* 5bit */
  505. thread->number_mask = 1 << thread->number;
  506. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  507. #else
  508. thread->number_mask = 1 << thread->current_priority;
  509. #endif
  510. }
  511. /* enable interrupt */
  512. rt_hw_interrupt_enable(temp);
  513. break;
  514. case RT_THREAD_CTRL_STARTUP:
  515. return rt_thread_startup(thread);
  516. #ifdef RT_USING_HEAP
  517. case RT_THREAD_CTRL_CLOSE:
  518. return rt_thread_delete(thread);
  519. #endif
  520. default:
  521. break;
  522. }
  523. return RT_EOK;
  524. }
  525. RTM_EXPORT(rt_thread_control);
  526. /**
  527. * This function will suspend the specified thread.
  528. *
  529. * @param thread the thread to be suspended
  530. *
  531. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  532. *
  533. * @note if suspend self thread, after this function call, the
  534. * rt_schedule() must be invoked.
  535. */
  536. rt_err_t rt_thread_suspend(rt_thread_t thread)
  537. {
  538. register rt_base_t temp;
  539. /* thread check */
  540. RT_ASSERT(thread != RT_NULL);
  541. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  542. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  543. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
  544. {
  545. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
  546. thread->stat));
  547. return -RT_ERROR;
  548. }
  549. /* disable interrupt */
  550. temp = rt_hw_interrupt_disable();
  551. /* change thread stat */
  552. thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
  553. rt_schedule_remove_thread(thread);
  554. /* stop thread timer anyway */
  555. rt_timer_stop(&(thread->thread_timer));
  556. /* enable interrupt */
  557. rt_hw_interrupt_enable(temp);
  558. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  559. return RT_EOK;
  560. }
  561. RTM_EXPORT(rt_thread_suspend);
  562. /**
  563. * This function will resume a thread and put it to system ready queue.
  564. *
  565. * @param thread the thread to be resumed
  566. *
  567. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  568. */
  569. rt_err_t rt_thread_resume(rt_thread_t thread)
  570. {
  571. register rt_base_t temp;
  572. /* thread check */
  573. RT_ASSERT(thread != RT_NULL);
  574. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  575. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  576. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
  577. {
  578. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  579. thread->stat));
  580. return -RT_ERROR;
  581. }
  582. /* disable interrupt */
  583. temp = rt_hw_interrupt_disable();
  584. /* remove from suspend list */
  585. rt_list_remove(&(thread->tlist));
  586. rt_timer_stop(&thread->thread_timer);
  587. /* enable interrupt */
  588. rt_hw_interrupt_enable(temp);
  589. /* insert to schedule ready list */
  590. rt_schedule_insert_thread(thread);
  591. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  592. return RT_EOK;
  593. }
  594. RTM_EXPORT(rt_thread_resume);
  595. /**
  596. * This function is the timeout function for thread, normally which is invoked
  597. * when thread is timeout to wait some resource.
  598. *
  599. * @param parameter the parameter of thread timeout function
  600. */
  601. void rt_thread_timeout(void *parameter)
  602. {
  603. struct rt_thread *thread;
  604. thread = (struct rt_thread *)parameter;
  605. /* thread check */
  606. RT_ASSERT(thread != RT_NULL);
  607. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
  608. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  609. /* set error number */
  610. thread->error = -RT_ETIMEOUT;
  611. /* remove from suspend list */
  612. rt_list_remove(&(thread->tlist));
  613. /* insert to schedule ready list */
  614. rt_schedule_insert_thread(thread);
  615. /* do schedule */
  616. rt_schedule();
  617. }
  618. RTM_EXPORT(rt_thread_timeout);
  619. /**
  620. * This function will find the specified thread.
  621. *
  622. * @param name the name of thread finding
  623. *
  624. * @return the found thread
  625. *
  626. * @note please don't invoke this function in interrupt status.
  627. */
  628. rt_thread_t rt_thread_find(char *name)
  629. {
  630. struct rt_object_information *information;
  631. struct rt_object *object;
  632. struct rt_list_node *node;
  633. /* enter critical */
  634. if (rt_thread_self() != RT_NULL)
  635. rt_enter_critical();
  636. /* try to find device object */
  637. information = rt_object_get_information(RT_Object_Class_Thread);
  638. RT_ASSERT(information != RT_NULL);
  639. for (node = information->object_list.next;
  640. node != &(information->object_list);
  641. node = node->next)
  642. {
  643. object = rt_list_entry(node, struct rt_object, list);
  644. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  645. {
  646. /* leave critical */
  647. if (rt_thread_self() != RT_NULL)
  648. rt_exit_critical();
  649. return (rt_thread_t)object;
  650. }
  651. }
  652. /* leave critical */
  653. if (rt_thread_self() != RT_NULL)
  654. rt_exit_critical();
  655. /* not found */
  656. return RT_NULL;
  657. }
  658. RTM_EXPORT(rt_thread_find);
  659. /**@}*/