thread.c 23 KB

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