thread.c 22 KB

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