thread.c 23 KB

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