thread.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * Copyright (c) 2006-2021, 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. #ifdef RT_USING_HOOK
  33. static void (*rt_thread_suspend_hook)(rt_thread_t thread);
  34. static void (*rt_thread_resume_hook) (rt_thread_t thread);
  35. static void (*rt_thread_inited_hook) (rt_thread_t thread);
  36. /**
  37. * @ingroup Hook
  38. * This function sets a hook function when the system suspend a thread.
  39. *
  40. * @param hook the specified hook function
  41. *
  42. * @note the hook function must be simple and never be blocked or suspend.
  43. */
  44. void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
  45. {
  46. rt_thread_suspend_hook = hook;
  47. }
  48. /**
  49. * @ingroup Hook
  50. * This function sets a hook function when the system resume a thread.
  51. *
  52. * @param hook the specified hook function
  53. *
  54. * @note the hook function must be simple and never be blocked or suspend.
  55. */
  56. void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
  57. {
  58. rt_thread_resume_hook = hook;
  59. }
  60. /**
  61. * @ingroup Hook
  62. * This function sets a hook function when a thread is initialized.
  63. *
  64. * @param hook the specified hook function
  65. */
  66. void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
  67. {
  68. rt_thread_inited_hook = hook;
  69. }
  70. #endif
  71. void rt_thread_exit(void)
  72. {
  73. struct rt_thread *thread;
  74. register rt_base_t level;
  75. /* get current thread */
  76. thread = rt_thread_self();
  77. /* disable interrupt */
  78. level = rt_hw_interrupt_disable();
  79. /* remove from schedule */
  80. rt_schedule_remove_thread(thread);
  81. /* change stat */
  82. thread->stat = RT_THREAD_CLOSE;
  83. /* remove it from timer list */
  84. rt_timer_detach(&thread->thread_timer);
  85. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
  86. thread->cleanup == RT_NULL)
  87. {
  88. rt_object_detach((rt_object_t)thread);
  89. }
  90. else
  91. {
  92. /* insert to defunct thread list */
  93. rt_thread_defunct_enqueue(thread);
  94. }
  95. /* switch to next task */
  96. rt_schedule();
  97. /* enable interrupt */
  98. rt_hw_interrupt_enable(level);
  99. }
  100. static rt_err_t _rt_thread_init(struct rt_thread *thread,
  101. const char *name,
  102. void (*entry)(void *parameter),
  103. void *parameter,
  104. void *stack_start,
  105. rt_uint32_t stack_size,
  106. rt_uint8_t priority,
  107. rt_uint32_t tick)
  108. {
  109. /* init thread list */
  110. rt_list_init(&(thread->tlist));
  111. #ifdef RT_USING_LWP
  112. thread->wakeup.func = RT_NULL;
  113. #endif
  114. thread->entry = (void *)entry;
  115. thread->parameter = parameter;
  116. /* stack init */
  117. thread->stack_addr = stack_start;
  118. thread->stack_size = stack_size;
  119. /* init thread stack */
  120. rt_memset(thread->stack_addr, '#', thread->stack_size);
  121. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  122. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  123. (void *)((char *)thread->stack_addr),
  124. (void *)rt_thread_exit);
  125. #else
  126. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  127. (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
  128. (void *)rt_thread_exit);
  129. #endif
  130. /* priority init */
  131. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  132. thread->init_priority = priority;
  133. thread->current_priority = priority;
  134. thread->number_mask = 0;
  135. #if RT_THREAD_PRIORITY_MAX > 32
  136. thread->number = 0;
  137. thread->high_mask = 0;
  138. #endif
  139. /* tick init */
  140. thread->init_tick = tick;
  141. thread->remaining_tick = tick;
  142. /* error and flags */
  143. thread->error = RT_EOK;
  144. thread->stat = RT_THREAD_INIT;
  145. #ifdef RT_USING_SMP
  146. /* not bind on any cpu */
  147. thread->bind_cpu = RT_CPUS_NR;
  148. thread->oncpu = RT_CPU_DETACHED;
  149. /* lock init */
  150. thread->scheduler_lock_nest = 0;
  151. thread->cpus_lock_nest = 0;
  152. thread->critical_lock_nest = 0;
  153. #endif /*RT_USING_SMP*/
  154. /* initialize cleanup function and user data */
  155. thread->cleanup = 0;
  156. thread->user_data = 0;
  157. /* initialize thread timer */
  158. rt_timer_init(&(thread->thread_timer),
  159. thread->name,
  160. rt_thread_timeout,
  161. thread,
  162. 0,
  163. RT_TIMER_FLAG_ONE_SHOT);
  164. /* initialize signal */
  165. #ifdef RT_USING_SIGNALS
  166. thread->sig_mask = 0x00;
  167. thread->sig_pending = 0x00;
  168. #ifndef RT_USING_SMP
  169. thread->sig_ret = RT_NULL;
  170. #endif
  171. thread->sig_vectors = RT_NULL;
  172. thread->si_list = RT_NULL;
  173. #endif
  174. #ifdef RT_USING_LWP
  175. thread->lwp = RT_NULL;
  176. rt_list_init(&(thread->sibling));
  177. rt_memset(&thread->signal, 0, sizeof(lwp_sigset_t));
  178. rt_memset(&thread->signal_mask, 0, sizeof(lwp_sigset_t));
  179. thread->signal_mask_bak = 0;
  180. thread->signal_in_process = 0;
  181. rt_memset(&thread->user_ctx, 0, sizeof thread->user_ctx);
  182. #endif
  183. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
  184. return RT_EOK;
  185. }
  186. /**
  187. * @addtogroup Thread
  188. */
  189. /**@{*/
  190. /**
  191. * This function will initialize a thread, normally it's used to initialize a
  192. * static thread object.
  193. *
  194. * @param thread the static thread object
  195. * @param name the name of thread, which shall be unique
  196. * @param entry the entry function of thread
  197. * @param parameter the parameter of thread enter function
  198. * @param stack_start the start address of thread stack
  199. * @param stack_size the size of thread stack
  200. * @param priority the priority of thread
  201. * @param tick the time slice if there are same priority thread
  202. *
  203. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  204. */
  205. rt_err_t rt_thread_init(struct rt_thread *thread,
  206. const char *name,
  207. void (*entry)(void *parameter),
  208. void *parameter,
  209. void *stack_start,
  210. rt_uint32_t stack_size,
  211. rt_uint8_t priority,
  212. rt_uint32_t tick)
  213. {
  214. /* thread check */
  215. RT_ASSERT(thread != RT_NULL);
  216. RT_ASSERT(stack_start != RT_NULL);
  217. /* initialize thread object */
  218. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  219. return _rt_thread_init(thread,
  220. name,
  221. entry,
  222. parameter,
  223. stack_start,
  224. stack_size,
  225. priority,
  226. tick);
  227. }
  228. RTM_EXPORT(rt_thread_init);
  229. /**
  230. * This function will return self thread object
  231. *
  232. * @return the self thread object
  233. */
  234. rt_thread_t rt_thread_self(void)
  235. {
  236. #ifdef RT_USING_SMP
  237. rt_base_t lock;
  238. rt_thread_t self;
  239. lock = rt_hw_local_irq_disable();
  240. self = rt_cpu_self()->current_thread;
  241. rt_hw_local_irq_enable(lock);
  242. return self;
  243. #else
  244. extern rt_thread_t rt_current_thread;
  245. return rt_current_thread;
  246. #endif
  247. }
  248. RTM_EXPORT(rt_thread_self);
  249. /**
  250. * This function will start a thread and put it to system ready queue
  251. *
  252. * @param thread the thread to be started
  253. *
  254. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  255. */
  256. rt_err_t rt_thread_startup(rt_thread_t thread)
  257. {
  258. /* thread check */
  259. RT_ASSERT(thread != RT_NULL);
  260. RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
  261. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  262. /* set current priority to initialize priority */
  263. thread->current_priority = thread->init_priority;
  264. /* calculate priority attribute */
  265. #if RT_THREAD_PRIORITY_MAX > 32
  266. thread->number = thread->current_priority >> 3; /* 5bit */
  267. thread->number_mask = 1L << thread->number;
  268. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  269. #else
  270. thread->number_mask = 1L << thread->current_priority;
  271. #endif
  272. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
  273. thread->name, thread->init_priority));
  274. /* change thread stat */
  275. thread->stat = RT_THREAD_SUSPEND;
  276. /* then resume it */
  277. rt_thread_resume(thread);
  278. if (rt_thread_self() != RT_NULL)
  279. {
  280. /* do a scheduling */
  281. rt_schedule();
  282. }
  283. return RT_EOK;
  284. }
  285. RTM_EXPORT(rt_thread_startup);
  286. /**
  287. * This function will detach a thread. The thread object will be removed from
  288. * thread queue and detached/deleted from system object management.
  289. *
  290. * @param thread the thread to be deleted
  291. *
  292. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  293. */
  294. rt_err_t rt_thread_detach(rt_thread_t thread)
  295. {
  296. rt_base_t lock;
  297. /* thread check */
  298. RT_ASSERT(thread != RT_NULL);
  299. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  300. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
  301. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  302. return RT_EOK;
  303. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  304. {
  305. /* remove from schedule */
  306. rt_schedule_remove_thread(thread);
  307. }
  308. /* release thread timer */
  309. rt_timer_detach(&(thread->thread_timer));
  310. /* change stat */
  311. thread->stat = RT_THREAD_CLOSE;
  312. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
  313. thread->cleanup == RT_NULL)
  314. {
  315. rt_object_detach((rt_object_t)thread);
  316. }
  317. else
  318. {
  319. /* disable interrupt */
  320. lock = rt_hw_interrupt_disable();
  321. /* insert to defunct thread list */
  322. rt_thread_defunct_enqueue(thread);
  323. /* enable interrupt */
  324. rt_hw_interrupt_enable(lock);
  325. }
  326. return RT_EOK;
  327. }
  328. RTM_EXPORT(rt_thread_detach);
  329. #ifdef RT_USING_HEAP
  330. /**
  331. * This function will create a thread object and allocate thread object memory
  332. * and stack.
  333. *
  334. * @param name the name of thread, which shall be unique
  335. * @param entry the entry function of thread
  336. * @param parameter the parameter of thread enter function
  337. * @param stack_size the size of thread stack
  338. * @param priority the priority of thread
  339. * @param tick the time slice if there are same priority thread
  340. *
  341. * @return the created thread object
  342. */
  343. rt_thread_t rt_thread_create(const char *name,
  344. void (*entry)(void *parameter),
  345. void *parameter,
  346. rt_uint32_t stack_size,
  347. rt_uint8_t priority,
  348. rt_uint32_t tick)
  349. {
  350. struct rt_thread *thread;
  351. void *stack_start;
  352. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
  353. name);
  354. if (thread == RT_NULL)
  355. return RT_NULL;
  356. stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
  357. if (stack_start == RT_NULL)
  358. {
  359. /* allocate stack failure */
  360. rt_object_delete((rt_object_t)thread);
  361. return RT_NULL;
  362. }
  363. _rt_thread_init(thread,
  364. name,
  365. entry,
  366. parameter,
  367. stack_start,
  368. stack_size,
  369. priority,
  370. tick);
  371. return thread;
  372. }
  373. RTM_EXPORT(rt_thread_create);
  374. /**
  375. * This function will delete a thread. The thread object will be removed from
  376. * thread queue and deleted from system object management in the idle thread.
  377. *
  378. * @param thread the thread to be deleted
  379. *
  380. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  381. */
  382. rt_err_t rt_thread_delete(rt_thread_t thread)
  383. {
  384. rt_base_t lock;
  385. /* thread check */
  386. RT_ASSERT(thread != RT_NULL);
  387. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  388. RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
  389. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
  390. return RT_EOK;
  391. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  392. {
  393. /* remove from schedule */
  394. rt_schedule_remove_thread(thread);
  395. }
  396. /* release thread timer */
  397. rt_timer_detach(&(thread->thread_timer));
  398. /* disable interrupt */
  399. lock = rt_hw_interrupt_disable();
  400. /* change stat */
  401. thread->stat = RT_THREAD_CLOSE;
  402. /* insert to defunct thread list */
  403. rt_thread_defunct_enqueue(thread);
  404. /* enable interrupt */
  405. rt_hw_interrupt_enable(lock);
  406. return RT_EOK;
  407. }
  408. RTM_EXPORT(rt_thread_delete);
  409. #endif
  410. /**
  411. * This function will let current thread yield processor, and scheduler will
  412. * choose a highest thread to run. After yield processor, the current thread
  413. * is still in READY state.
  414. *
  415. * @return RT_EOK
  416. */
  417. rt_err_t rt_thread_yield(void)
  418. {
  419. rt_base_t level;
  420. struct rt_thread *thread;
  421. thread = rt_thread_self();
  422. level = rt_hw_interrupt_disable();
  423. thread->remaining_tick = thread->init_tick;
  424. thread->stat |= RT_THREAD_STAT_YIELD;
  425. rt_schedule();
  426. rt_hw_interrupt_enable(level);
  427. return RT_EOK;
  428. }
  429. RTM_EXPORT(rt_thread_yield);
  430. /**
  431. * This function will let current thread sleep for some ticks.
  432. *
  433. * @param tick the sleep ticks
  434. *
  435. * @return RT_EOK
  436. */
  437. rt_err_t rt_thread_sleep(rt_tick_t tick)
  438. {
  439. register rt_base_t temp;
  440. struct rt_thread *thread;
  441. /* set to current thread */
  442. thread = rt_thread_self();
  443. RT_ASSERT(thread != RT_NULL);
  444. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  445. /* disable interrupt */
  446. temp = rt_hw_interrupt_disable();
  447. /* suspend thread */
  448. rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  449. /* reset the timeout of thread timer and start it */
  450. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  451. rt_timer_start(&(thread->thread_timer));
  452. /* enable interrupt */
  453. rt_hw_interrupt_enable(temp);
  454. rt_schedule();
  455. /* clear error number of this thread to RT_EOK */
  456. if (thread->error == -RT_ETIMEOUT)
  457. thread->error = RT_EOK;
  458. return RT_EOK;
  459. }
  460. /**
  461. * This function will let current thread delay for some ticks.
  462. *
  463. * @param tick the delay ticks
  464. *
  465. * @return RT_EOK
  466. */
  467. rt_err_t rt_thread_delay(rt_tick_t tick)
  468. {
  469. return rt_thread_sleep(tick);
  470. }
  471. RTM_EXPORT(rt_thread_delay);
  472. /**
  473. * This function will let current thread delay until (*tick + inc_tick).
  474. *
  475. * @param tick the tick of last wakeup.
  476. * @param inc_tick the increment tick
  477. *
  478. * @return RT_EOK
  479. */
  480. rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
  481. {
  482. register rt_base_t level;
  483. struct rt_thread *thread;
  484. RT_ASSERT(tick != RT_NULL);
  485. /* set to current thread */
  486. thread = rt_thread_self();
  487. RT_ASSERT(thread != RT_NULL);
  488. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  489. /* disable interrupt */
  490. level = rt_hw_interrupt_disable();
  491. if (rt_tick_get() - *tick < inc_tick)
  492. {
  493. *tick = *tick + inc_tick - rt_tick_get();
  494. /* suspend thread */
  495. rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  496. /* reset the timeout of thread timer and start it */
  497. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, tick);
  498. rt_timer_start(&(thread->thread_timer));
  499. /* enable interrupt */
  500. rt_hw_interrupt_enable(level);
  501. rt_schedule();
  502. /* clear error number of this thread to RT_EOK */
  503. if (thread->error == -RT_ETIMEOUT)
  504. {
  505. thread->error = RT_EOK;
  506. }
  507. }
  508. else
  509. {
  510. rt_hw_interrupt_enable(level);
  511. }
  512. /* get the wakeup tick */
  513. *tick = rt_tick_get();
  514. return RT_EOK;
  515. }
  516. RTM_EXPORT(rt_thread_delay_until);
  517. /**
  518. * This function will let current thread delay for some milliseconds.
  519. *
  520. * @param ms the delay ms time
  521. *
  522. * @return RT_EOK
  523. */
  524. rt_err_t rt_thread_mdelay(rt_int32_t ms)
  525. {
  526. rt_tick_t tick;
  527. tick = rt_tick_from_millisecond(ms);
  528. return rt_thread_sleep(tick);
  529. }
  530. RTM_EXPORT(rt_thread_mdelay);
  531. /**
  532. * This function will control thread behaviors according to control command.
  533. *
  534. * @param thread the specified thread to be controlled
  535. * @param cmd the control command, which includes
  536. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  537. * RT_THREAD_CTRL_STARTUP for starting a thread;
  538. * RT_THREAD_CTRL_CLOSE for delete a thread;
  539. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  540. * @param arg the argument of control command
  541. *
  542. * @return RT_EOK
  543. */
  544. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  545. {
  546. register rt_base_t temp;
  547. /* thread check */
  548. RT_ASSERT(thread != RT_NULL);
  549. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  550. switch (cmd)
  551. {
  552. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  553. /* disable interrupt */
  554. temp = rt_hw_interrupt_disable();
  555. /* for ready thread, change queue */
  556. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  557. {
  558. /* remove thread from schedule queue first */
  559. rt_schedule_remove_thread(thread);
  560. /* change thread priority */
  561. thread->current_priority = *(rt_uint8_t *)arg;
  562. /* recalculate priority attribute */
  563. #if RT_THREAD_PRIORITY_MAX > 32
  564. thread->number = thread->current_priority >> 3; /* 5bit */
  565. thread->number_mask = 1 << thread->number;
  566. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  567. #else
  568. thread->number_mask = 1 << thread->current_priority;
  569. #endif
  570. /* insert thread to schedule queue again */
  571. rt_schedule_insert_thread(thread);
  572. }
  573. else
  574. {
  575. thread->current_priority = *(rt_uint8_t *)arg;
  576. /* recalculate priority attribute */
  577. #if RT_THREAD_PRIORITY_MAX > 32
  578. thread->number = thread->current_priority >> 3; /* 5bit */
  579. thread->number_mask = 1 << thread->number;
  580. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  581. #else
  582. thread->number_mask = 1 << thread->current_priority;
  583. #endif
  584. }
  585. /* enable interrupt */
  586. rt_hw_interrupt_enable(temp);
  587. break;
  588. case RT_THREAD_CTRL_STARTUP:
  589. return rt_thread_startup(thread);
  590. case RT_THREAD_CTRL_CLOSE:
  591. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  592. {
  593. return rt_thread_detach(thread);
  594. }
  595. #ifdef RT_USING_HEAP
  596. else
  597. {
  598. return rt_thread_delete(thread);
  599. }
  600. #endif
  601. #ifdef RT_USING_SMP
  602. case RT_THREAD_CTRL_BIND_CPU:
  603. {
  604. rt_uint8_t cpu;
  605. if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
  606. {
  607. /* we only support bind cpu before started phase. */
  608. return RT_ERROR;
  609. }
  610. cpu = (rt_uint8_t)(size_t)arg;
  611. thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;
  612. break;
  613. }
  614. #endif /*RT_USING_SMP*/
  615. default:
  616. break;
  617. }
  618. return RT_EOK;
  619. }
  620. RTM_EXPORT(rt_thread_control);
  621. #ifdef RT_USING_LWP
  622. int lwp_suspend_sigcheck(rt_thread_t thread, int suspend_flag);
  623. #endif
  624. static void rt_thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
  625. {
  626. rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  627. RT_ASSERT(thread != RT_NULL);
  628. switch (suspend_flag)
  629. {
  630. case RT_INTERRUPTIBLE:
  631. stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
  632. break;
  633. case RT_KILLABLE:
  634. stat = RT_THREAD_SUSPEND_KILLABLE;
  635. break;
  636. case RT_UNINTERRUPTIBLE:
  637. stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  638. break;
  639. default:
  640. RT_ASSERT(0);
  641. break;
  642. }
  643. thread->stat = stat | (thread->stat & ~RT_THREAD_STAT_MASK);
  644. }
  645. /**
  646. * This function will suspend the specified thread.
  647. *
  648. * @param thread the thread to be suspended
  649. * @param suspend_flag status flag of the thread to be suspended
  650. *
  651. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  652. *
  653. * @note if suspend self thread, after this function call, the
  654. * rt_schedule() must be invoked.
  655. */
  656. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  657. {
  658. register rt_base_t stat;
  659. register rt_base_t temp;
  660. /* thread check */
  661. RT_ASSERT(thread != RT_NULL);
  662. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  663. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  664. stat = thread->stat & RT_THREAD_STAT_MASK;
  665. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  666. {
  667. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
  668. thread->stat));
  669. return -RT_ERROR;
  670. }
  671. /* disable interrupt */
  672. temp = rt_hw_interrupt_disable();
  673. if (stat == RT_THREAD_RUNNING)
  674. {
  675. /* not suspend running status thread on other core */
  676. RT_ASSERT(thread == rt_thread_self());
  677. }
  678. #ifdef RT_USING_LWP
  679. if (lwp_suspend_sigcheck(thread, suspend_flag) == 0)
  680. {
  681. /* not to suspend */
  682. rt_hw_interrupt_enable(temp);
  683. return -RT_EINTR;
  684. }
  685. #endif
  686. /* change thread stat */
  687. rt_schedule_remove_thread(thread);
  688. rt_thread_set_suspend_state(thread, suspend_flag);
  689. /* stop thread timer anyway */
  690. rt_timer_stop(&(thread->thread_timer));
  691. /* enable interrupt */
  692. rt_hw_interrupt_enable(temp);
  693. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  694. return RT_EOK;
  695. }
  696. RTM_EXPORT(rt_thread_suspend_with_flag);
  697. rt_err_t rt_thread_suspend(rt_thread_t thread)
  698. {
  699. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  700. }
  701. RTM_EXPORT(rt_thread_suspend);
  702. /**
  703. * This function will resume a thread and put it to system ready queue.
  704. *
  705. * @param thread the thread to be resumed
  706. *
  707. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  708. */
  709. rt_err_t rt_thread_resume(rt_thread_t thread)
  710. {
  711. register rt_base_t temp;
  712. /* thread check */
  713. RT_ASSERT(thread != RT_NULL);
  714. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  715. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  716. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  717. {
  718. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  719. thread->stat));
  720. return -RT_ERROR;
  721. }
  722. /* disable interrupt */
  723. temp = rt_hw_interrupt_disable();
  724. /* remove from suspend list */
  725. rt_list_remove(&(thread->tlist));
  726. rt_timer_stop(&thread->thread_timer);
  727. #ifdef RT_USING_LWP
  728. thread->wakeup.func = RT_NULL;
  729. #endif
  730. /* enable interrupt */
  731. rt_hw_interrupt_enable(temp);
  732. /* insert to schedule ready list */
  733. rt_schedule_insert_thread(thread);
  734. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  735. return RT_EOK;
  736. }
  737. RTM_EXPORT(rt_thread_resume);
  738. #ifdef RT_USING_LWP
  739. /**
  740. * This function will wakeup a thread with customized operation.
  741. *
  742. * @param thread the thread to be resumed
  743. *
  744. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  745. */
  746. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  747. {
  748. register rt_base_t temp;
  749. rt_err_t ret;
  750. RT_ASSERT(thread != RT_NULL);
  751. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  752. /* disable interrupt */
  753. temp = rt_hw_interrupt_disable();
  754. if (thread->wakeup.func)
  755. {
  756. ret = thread->wakeup.func(thread->wakeup.user_data, thread);
  757. thread->wakeup.func = RT_NULL;
  758. }
  759. else
  760. {
  761. ret = rt_thread_resume(thread);
  762. }
  763. rt_hw_interrupt_enable(temp);
  764. return ret;
  765. }
  766. RTM_EXPORT(rt_thread_wakeup);
  767. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  768. {
  769. register rt_base_t temp;
  770. RT_ASSERT(thread != RT_NULL);
  771. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  772. temp = rt_hw_interrupt_disable();
  773. thread->wakeup.func = func;
  774. thread->wakeup.user_data = user_data;
  775. rt_hw_interrupt_enable(temp);
  776. }
  777. RTM_EXPORT(rt_thread_wakeup_set);
  778. #endif
  779. /**
  780. * This function is the timeout function for thread, normally which is invoked
  781. * when thread is timeout to wait some resource.
  782. *
  783. * @param parameter the parameter of thread timeout function
  784. */
  785. void rt_thread_timeout(void *parameter)
  786. {
  787. struct rt_thread *thread;
  788. thread = (struct rt_thread *)parameter;
  789. /* thread check */
  790. RT_ASSERT(thread != RT_NULL);
  791. RT_ASSERT((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK);
  792. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  793. /* set error number */
  794. thread->error = -RT_ETIMEOUT;
  795. /* remove from suspend list */
  796. rt_list_remove(&(thread->tlist));
  797. /* insert to schedule ready list */
  798. rt_schedule_insert_thread(thread);
  799. /* do schedule */
  800. rt_schedule();
  801. }
  802. RTM_EXPORT(rt_thread_timeout);
  803. /**
  804. * This function will find the specified thread.
  805. *
  806. * @param name the name of thread finding
  807. *
  808. * @return the found thread
  809. *
  810. * @note please don't invoke this function in interrupt status.
  811. */
  812. rt_thread_t rt_thread_find(char *name)
  813. {
  814. struct rt_object_information *information;
  815. struct rt_object *object;
  816. struct rt_list_node *node;
  817. /* enter critical */
  818. if (rt_thread_self() != RT_NULL)
  819. rt_enter_critical();
  820. /* try to find device object */
  821. information = rt_object_get_information(RT_Object_Class_Thread);
  822. RT_ASSERT(information != RT_NULL);
  823. for (node = information->object_list.next;
  824. node != &(information->object_list);
  825. node = node->next)
  826. {
  827. object = rt_list_entry(node, struct rt_object, list);
  828. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  829. {
  830. /* leave critical */
  831. if (rt_thread_self() != RT_NULL)
  832. rt_exit_critical();
  833. return (rt_thread_t)object;
  834. }
  835. }
  836. /* leave critical */
  837. if (rt_thread_self() != RT_NULL)
  838. rt_exit_critical();
  839. /* not found */
  840. return RT_NULL;
  841. }
  842. RTM_EXPORT(rt_thread_find);
  843. /**@}*/