thread.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. #ifdef RT_USING_SMP
  532. static void rt_thread_cpu_bind(rt_thread_t thread, int cpu)
  533. {
  534. rt_base_t level;
  535. if (cpu >= RT_CPUS_NR)
  536. {
  537. cpu = RT_CPUS_NR;
  538. }
  539. level = rt_hw_interrupt_disable();
  540. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  541. {
  542. /* unbind */
  543. /* remove from old ready queue */
  544. rt_schedule_remove_thread(thread);
  545. /* change thread bind cpu */
  546. thread->bind_cpu = cpu;
  547. /* add to new ready queue */
  548. rt_schedule_insert_thread(thread);
  549. if (rt_thread_self() != RT_NULL)
  550. {
  551. rt_schedule();
  552. }
  553. }
  554. else
  555. {
  556. thread->bind_cpu = cpu;
  557. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  558. {
  559. /* thread is running on a cpu */
  560. int current_cpu = rt_hw_cpu_id();
  561. if (cpu != RT_CPUS_NR)
  562. {
  563. if (thread->oncpu == current_cpu)
  564. {
  565. /* current thread on current cpu */
  566. if (cpu != current_cpu)
  567. {
  568. /* bind to other cpu */
  569. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << cpu);
  570. /* self cpu need reschedule */
  571. rt_schedule();
  572. }
  573. /* else do nothing */
  574. }
  575. else
  576. {
  577. /* no running on self cpu, but dest cpu can be itself */
  578. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << thread->oncpu);
  579. }
  580. }
  581. /* else do nothing */
  582. }
  583. }
  584. rt_hw_interrupt_enable(level);
  585. }
  586. #endif
  587. /**
  588. * This function will control thread behaviors according to control command.
  589. *
  590. * @param thread the specified thread to be controlled
  591. * @param cmd the control command, which includes
  592. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  593. * RT_THREAD_CTRL_STARTUP for starting a thread;
  594. * RT_THREAD_CTRL_CLOSE for delete a thread;
  595. * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  596. * @param arg the argument of control command
  597. *
  598. * @return RT_EOK
  599. */
  600. rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
  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. switch (cmd)
  607. {
  608. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  609. /* disable interrupt */
  610. temp = rt_hw_interrupt_disable();
  611. /* for ready thread, change queue */
  612. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  613. {
  614. /* remove thread from schedule queue first */
  615. rt_schedule_remove_thread(thread);
  616. /* change thread priority */
  617. thread->current_priority = *(rt_uint8_t *)arg;
  618. /* recalculate priority attribute */
  619. #if RT_THREAD_PRIORITY_MAX > 32
  620. thread->number = thread->current_priority >> 3; /* 5bit */
  621. thread->number_mask = 1 << thread->number;
  622. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  623. #else
  624. thread->number_mask = 1 << thread->current_priority;
  625. #endif
  626. /* insert thread to schedule queue again */
  627. rt_schedule_insert_thread(thread);
  628. }
  629. else
  630. {
  631. thread->current_priority = *(rt_uint8_t *)arg;
  632. /* recalculate priority attribute */
  633. #if RT_THREAD_PRIORITY_MAX > 32
  634. thread->number = thread->current_priority >> 3; /* 5bit */
  635. thread->number_mask = 1 << thread->number;
  636. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  637. #else
  638. thread->number_mask = 1 << thread->current_priority;
  639. #endif
  640. }
  641. /* enable interrupt */
  642. rt_hw_interrupt_enable(temp);
  643. break;
  644. case RT_THREAD_CTRL_STARTUP:
  645. return rt_thread_startup(thread);
  646. case RT_THREAD_CTRL_CLOSE:
  647. if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
  648. {
  649. return rt_thread_detach(thread);
  650. }
  651. #ifdef RT_USING_HEAP
  652. else
  653. {
  654. return rt_thread_delete(thread);
  655. }
  656. #endif
  657. #ifdef RT_USING_SMP
  658. case RT_THREAD_CTRL_BIND_CPU:
  659. {
  660. rt_uint8_t cpu;
  661. cpu = (rt_uint8_t)(size_t)arg;
  662. rt_thread_cpu_bind(thread, cpu);
  663. break;
  664. }
  665. #endif /*RT_USING_SMP*/
  666. default:
  667. break;
  668. }
  669. return RT_EOK;
  670. }
  671. RTM_EXPORT(rt_thread_control);
  672. #ifdef RT_USING_LWP
  673. int lwp_suspend_sigcheck(rt_thread_t thread, int suspend_flag);
  674. #endif
  675. static void rt_thread_set_suspend_state(struct rt_thread *thread, int suspend_flag)
  676. {
  677. rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  678. RT_ASSERT(thread != RT_NULL);
  679. switch (suspend_flag)
  680. {
  681. case RT_INTERRUPTIBLE:
  682. stat = RT_THREAD_SUSPEND_INTERRUPTIBLE;
  683. break;
  684. case RT_KILLABLE:
  685. stat = RT_THREAD_SUSPEND_KILLABLE;
  686. break;
  687. case RT_UNINTERRUPTIBLE:
  688. stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE;
  689. break;
  690. default:
  691. RT_ASSERT(0);
  692. break;
  693. }
  694. thread->stat = stat | (thread->stat & ~RT_THREAD_STAT_MASK);
  695. }
  696. /**
  697. * This function will suspend the specified thread.
  698. *
  699. * @param thread the thread to be suspended
  700. * @param suspend_flag status flag of the thread to be suspended
  701. *
  702. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  703. *
  704. * @note if suspend self thread, after this function call, the
  705. * rt_schedule() must be invoked.
  706. */
  707. rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
  708. {
  709. register rt_base_t stat;
  710. register rt_base_t temp;
  711. /* thread check */
  712. RT_ASSERT(thread != RT_NULL);
  713. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  714. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  715. stat = thread->stat & RT_THREAD_STAT_MASK;
  716. if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
  717. {
  718. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
  719. thread->stat));
  720. return -RT_ERROR;
  721. }
  722. /* disable interrupt */
  723. temp = rt_hw_interrupt_disable();
  724. if (stat == RT_THREAD_RUNNING)
  725. {
  726. /* not suspend running status thread on other core */
  727. RT_ASSERT(thread == rt_thread_self());
  728. }
  729. #ifdef RT_USING_LWP
  730. if (lwp_suspend_sigcheck(thread, suspend_flag) == 0)
  731. {
  732. /* not to suspend */
  733. rt_hw_interrupt_enable(temp);
  734. return -RT_EINTR;
  735. }
  736. #endif
  737. /* change thread stat */
  738. rt_schedule_remove_thread(thread);
  739. rt_thread_set_suspend_state(thread, suspend_flag);
  740. /* stop thread timer anyway */
  741. rt_timer_stop(&(thread->thread_timer));
  742. /* enable interrupt */
  743. rt_hw_interrupt_enable(temp);
  744. RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
  745. return RT_EOK;
  746. }
  747. RTM_EXPORT(rt_thread_suspend_with_flag);
  748. rt_err_t rt_thread_suspend(rt_thread_t thread)
  749. {
  750. return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  751. }
  752. RTM_EXPORT(rt_thread_suspend);
  753. /**
  754. * This function will resume a thread and put it to system ready queue.
  755. *
  756. * @param thread the thread to be resumed
  757. *
  758. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  759. */
  760. rt_err_t rt_thread_resume(rt_thread_t thread)
  761. {
  762. register rt_base_t temp;
  763. /* thread check */
  764. RT_ASSERT(thread != RT_NULL);
  765. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  766. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  767. if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK)
  768. {
  769. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
  770. thread->stat));
  771. return -RT_ERROR;
  772. }
  773. /* disable interrupt */
  774. temp = rt_hw_interrupt_disable();
  775. /* remove from suspend list */
  776. rt_list_remove(&(thread->tlist));
  777. rt_timer_stop(&thread->thread_timer);
  778. #ifdef RT_USING_LWP
  779. thread->wakeup.func = RT_NULL;
  780. #endif
  781. /* enable interrupt */
  782. rt_hw_interrupt_enable(temp);
  783. /* insert to schedule ready list */
  784. rt_schedule_insert_thread(thread);
  785. RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
  786. return RT_EOK;
  787. }
  788. RTM_EXPORT(rt_thread_resume);
  789. #ifdef RT_USING_LWP
  790. /**
  791. * This function will wakeup a thread with customized operation.
  792. *
  793. * @param thread the thread to be resumed
  794. *
  795. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  796. */
  797. rt_err_t rt_thread_wakeup(rt_thread_t thread)
  798. {
  799. register rt_base_t temp;
  800. rt_err_t ret;
  801. RT_ASSERT(thread != RT_NULL);
  802. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  803. /* disable interrupt */
  804. temp = rt_hw_interrupt_disable();
  805. if (thread->wakeup.func)
  806. {
  807. ret = thread->wakeup.func(thread->wakeup.user_data, thread);
  808. thread->wakeup.func = RT_NULL;
  809. }
  810. else
  811. {
  812. ret = rt_thread_resume(thread);
  813. }
  814. rt_hw_interrupt_enable(temp);
  815. return ret;
  816. }
  817. RTM_EXPORT(rt_thread_wakeup);
  818. void rt_thread_wakeup_set(struct rt_thread *thread, rt_wakeup_func_t func, void* user_data)
  819. {
  820. register rt_base_t temp;
  821. RT_ASSERT(thread != RT_NULL);
  822. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  823. temp = rt_hw_interrupt_disable();
  824. thread->wakeup.func = func;
  825. thread->wakeup.user_data = user_data;
  826. rt_hw_interrupt_enable(temp);
  827. }
  828. RTM_EXPORT(rt_thread_wakeup_set);
  829. #endif
  830. /**
  831. * This function is the timeout function for thread, normally which is invoked
  832. * when thread is timeout to wait some resource.
  833. *
  834. * @param parameter the parameter of thread timeout function
  835. */
  836. void rt_thread_timeout(void *parameter)
  837. {
  838. struct rt_thread *thread;
  839. thread = (struct rt_thread *)parameter;
  840. /* thread check */
  841. RT_ASSERT(thread != RT_NULL);
  842. RT_ASSERT((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK);
  843. RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
  844. /* set error number */
  845. thread->error = -RT_ETIMEOUT;
  846. /* remove from suspend list */
  847. rt_list_remove(&(thread->tlist));
  848. /* insert to schedule ready list */
  849. rt_schedule_insert_thread(thread);
  850. /* do schedule */
  851. rt_schedule();
  852. }
  853. RTM_EXPORT(rt_thread_timeout);
  854. /**
  855. * This function will find the specified thread.
  856. *
  857. * @param name the name of thread finding
  858. *
  859. * @return the found thread
  860. *
  861. * @note please don't invoke this function in interrupt status.
  862. */
  863. rt_thread_t rt_thread_find(char *name)
  864. {
  865. struct rt_object_information *information;
  866. struct rt_object *object;
  867. struct rt_list_node *node;
  868. /* enter critical */
  869. if (rt_thread_self() != RT_NULL)
  870. rt_enter_critical();
  871. /* try to find device object */
  872. information = rt_object_get_information(RT_Object_Class_Thread);
  873. RT_ASSERT(information != RT_NULL);
  874. for (node = information->object_list.next;
  875. node != &(information->object_list);
  876. node = node->next)
  877. {
  878. object = rt_list_entry(node, struct rt_object, list);
  879. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  880. {
  881. /* leave critical */
  882. if (rt_thread_self() != RT_NULL)
  883. rt_exit_critical();
  884. return (rt_thread_t)object;
  885. }
  886. }
  887. /* leave critical */
  888. if (rt_thread_self() != RT_NULL)
  889. rt_exit_critical();
  890. /* not found */
  891. return RT_NULL;
  892. }
  893. RTM_EXPORT(rt_thread_find);
  894. /**@}*/