scheduler_mp.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-17 Bernard the first version
  9. * 2006-04-28 Bernard fix the scheduler algorthm
  10. * 2006-04-30 Bernard add SCHEDULER_DEBUG
  11. * 2006-05-27 Bernard fix the scheduler algorthm for same priority
  12. * thread schedule
  13. * 2006-06-04 Bernard rewrite the scheduler algorithm
  14. * 2006-08-03 Bernard add hook support
  15. * 2006-09-05 Bernard add 32 priority level support
  16. * 2006-09-24 Bernard add rt_system_scheduler_start function
  17. * 2009-09-16 Bernard fix _rt_scheduler_stack_check
  18. * 2010-04-11 yi.qiu add module feature
  19. * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest
  20. * issue found by kuronca
  21. * 2010-12-13 Bernard add defunct list initialization even if not use heap.
  22. * 2011-05-10 Bernard clean scheduler debug log.
  23. * 2013-12-21 Grissiom add rt_critical_level
  24. * 2018-11-22 Jesven remove the current task from ready queue
  25. * add per cpu ready queue
  26. * add _scheduler_get_highest_priority_thread to find highest priority task
  27. * rt_schedule_insert_thread won't insert current task to ready queue
  28. * in smp version, rt_hw_context_switch_interrupt maybe switch to
  29. * new task directly
  30. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to scheduler.c
  31. * 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c
  32. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  33. * 2023-12-10 xqyjlj use rt_hw_spinlock
  34. * 2024-01-05 Shell Fixup of data racing in rt_critical_level
  35. * 2024-01-18 Shell support rt_sched_thread of scheduling status for better mt protection
  36. */
  37. #include <rtthread.h>
  38. #include <rthw.h>
  39. #define DBG_TAG "kernel.scheduler"
  40. #define DBG_LVL DBG_INFO
  41. #include <rtdbg.h>
  42. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  43. static struct rt_spinlock _mp_scheduler_lock;
  44. #define SCHEDULER_LOCK_FLAG(percpu) ((percpu)->sched_lock_flag)
  45. #define SCHEDULER_ENTER_CRITICAL(curthr) \
  46. do \
  47. { \
  48. if (curthr) RT_SCHED_CTX(curthr).critical_lock_nest++; \
  49. } while (0)
  50. #define SCHEDULER_EXIT_CRITICAL(curthr) \
  51. do \
  52. { \
  53. if (curthr) RT_SCHED_CTX(curthr).critical_lock_nest--; \
  54. } while (0)
  55. #define SCHEDULER_CONTEXT_LOCK(percpu) \
  56. do \
  57. { \
  58. RT_ASSERT(SCHEDULER_LOCK_FLAG(percpu) == 0); \
  59. _fast_spin_lock(&_mp_scheduler_lock); \
  60. SCHEDULER_LOCK_FLAG(percpu) = 1; \
  61. } while (0)
  62. #define SCHEDULER_CONTEXT_UNLOCK(percpu) \
  63. do \
  64. { \
  65. RT_ASSERT(SCHEDULER_LOCK_FLAG(percpu) == 1); \
  66. SCHEDULER_LOCK_FLAG(percpu) = 0; \
  67. _fast_spin_unlock(&_mp_scheduler_lock); \
  68. } while (0)
  69. #define SCHEDULER_LOCK(level) \
  70. do \
  71. { \
  72. rt_thread_t _curthr; \
  73. struct rt_cpu *_percpu; \
  74. level = rt_hw_local_irq_disable(); \
  75. _percpu = rt_cpu_self(); \
  76. _curthr = _percpu->current_thread; \
  77. SCHEDULER_ENTER_CRITICAL(_curthr); \
  78. SCHEDULER_CONTEXT_LOCK(_percpu); \
  79. } while (0)
  80. #define SCHEDULER_UNLOCK(level) \
  81. do \
  82. { \
  83. rt_thread_t _curthr; \
  84. struct rt_cpu *_percpu; \
  85. _percpu = rt_cpu_self(); \
  86. _curthr = _percpu->current_thread; \
  87. SCHEDULER_CONTEXT_UNLOCK(_percpu); \
  88. SCHEDULER_EXIT_CRITICAL(_curthr); \
  89. rt_hw_local_irq_enable(level); \
  90. } while (0)
  91. static rt_uint32_t rt_thread_ready_priority_group;
  92. #if RT_THREAD_PRIORITY_MAX > 32
  93. /* Maximum priority level, 256 */
  94. static rt_uint8_t rt_thread_ready_table[32];
  95. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  96. /**
  97. * Used only on scheduler for optimization of control flows, where the critical
  98. * region is already guaranteed.
  99. */
  100. rt_inline void _fast_spin_lock(struct rt_spinlock *lock)
  101. {
  102. rt_hw_spin_lock(&lock->lock);
  103. RT_SPIN_LOCK_DEBUG(lock);
  104. }
  105. rt_inline void _fast_spin_unlock(struct rt_spinlock *lock)
  106. {
  107. rt_base_t critical_level;
  108. RT_SPIN_UNLOCK_DEBUG(lock, critical_level);
  109. /* for the scenario of sched, we don't check critical level */
  110. RT_UNUSED(critical_level);
  111. rt_hw_spin_unlock(&lock->lock);
  112. }
  113. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  114. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  115. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  116. /**
  117. * @addtogroup Hook
  118. */
  119. /**@{*/
  120. /**
  121. * @brief This function will set a hook function, which will be invoked when thread
  122. * switch happens.
  123. *
  124. * @param hook is the hook function.
  125. */
  126. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  127. {
  128. rt_scheduler_hook = hook;
  129. }
  130. /**
  131. * @brief This function will set a hook function, which will be invoked when context
  132. * switch happens.
  133. *
  134. * @param hook is the hook function.
  135. */
  136. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  137. {
  138. rt_scheduler_switch_hook = hook;
  139. }
  140. /**@}*/
  141. #endif /* RT_USING_HOOK */
  142. #if RT_THREAD_PRIORITY_MAX > 32
  143. rt_inline rt_base_t _get_global_highest_ready_prio(void)
  144. {
  145. rt_ubase_t number;
  146. rt_ubase_t highest_ready_priority;
  147. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  148. if (number != -1)
  149. {
  150. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  151. }
  152. else
  153. {
  154. highest_ready_priority = -1;
  155. }
  156. return highest_ready_priority;
  157. }
  158. rt_inline rt_base_t _get_local_highest_ready_prio(struct rt_cpu* pcpu)
  159. {
  160. rt_ubase_t number;
  161. rt_ubase_t local_highest_ready_priority;
  162. number = __rt_ffs(pcpu->priority_group) - 1;
  163. if (number != -1)
  164. {
  165. local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1;
  166. }
  167. else
  168. {
  169. local_highest_ready_priority = -1;
  170. }
  171. return local_highest_ready_priority;
  172. }
  173. #else /* if RT_THREAD_PRIORITY_MAX <= 32 */
  174. rt_inline rt_base_t _get_global_highest_ready_prio(void)
  175. {
  176. return __rt_ffs(rt_thread_ready_priority_group) - 1;
  177. }
  178. rt_inline rt_base_t _get_local_highest_ready_prio(struct rt_cpu* pcpu)
  179. {
  180. return __rt_ffs(pcpu->priority_group) - 1;
  181. }
  182. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  183. /*
  184. * get the highest priority thread in ready queue
  185. */
  186. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  187. {
  188. struct rt_thread *highest_priority_thread;
  189. rt_ubase_t highest_ready_priority, local_highest_ready_priority;
  190. struct rt_cpu* pcpu = rt_cpu_self();
  191. highest_ready_priority = _get_global_highest_ready_prio();
  192. local_highest_ready_priority = _get_local_highest_ready_prio(pcpu);
  193. /* get highest ready priority thread */
  194. if (highest_ready_priority < local_highest_ready_priority)
  195. {
  196. *highest_prio = highest_ready_priority;
  197. highest_priority_thread = RT_THREAD_LIST_NODE_ENTRY(
  198. rt_thread_priority_table[highest_ready_priority].next);
  199. }
  200. else
  201. {
  202. *highest_prio = local_highest_ready_priority;
  203. if (local_highest_ready_priority != -1)
  204. {
  205. highest_priority_thread = RT_THREAD_LIST_NODE_ENTRY(
  206. pcpu->priority_table[local_highest_ready_priority].next);
  207. }
  208. else
  209. {
  210. highest_priority_thread = RT_NULL;
  211. }
  212. }
  213. RT_ASSERT(!highest_priority_thread ||
  214. rt_object_get_type(&highest_priority_thread->parent) == RT_Object_Class_Thread);
  215. return highest_priority_thread;
  216. }
  217. /**
  218. * @brief set READY and insert thread to ready queue
  219. *
  220. * @note caller must holding the `_mp_scheduler_lock` lock
  221. */
  222. static void _sched_insert_thread_locked(struct rt_thread *thread)
  223. {
  224. int cpu_id;
  225. int bind_cpu;
  226. rt_uint32_t cpu_mask;
  227. if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  228. {
  229. /* already in ready queue */
  230. return ;
  231. }
  232. else if (RT_SCHED_CTX(thread).oncpu != RT_CPU_DETACHED)
  233. {
  234. /**
  235. * only YIELD -> READY, SUSPEND -> READY is allowed by this API. However,
  236. * this is a RUNNING thread. So here we reset it's status and let it go.
  237. */
  238. RT_SCHED_CTX(thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  239. return ;
  240. }
  241. /* READY thread, insert to ready queue */
  242. RT_SCHED_CTX(thread).stat = RT_THREAD_READY | (RT_SCHED_CTX(thread).stat & ~RT_THREAD_STAT_MASK);
  243. cpu_id = rt_hw_cpu_id();
  244. bind_cpu = RT_SCHED_CTX(thread).bind_cpu;
  245. /* insert thread to ready list */
  246. if (bind_cpu == RT_CPUS_NR)
  247. {
  248. #if RT_THREAD_PRIORITY_MAX > 32
  249. rt_thread_ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask;
  250. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  251. rt_thread_ready_priority_group |= RT_SCHED_PRIV(thread).number_mask;
  252. /* there is no time slices left(YIELD), inserting thread before ready list*/
  253. if((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  254. {
  255. rt_list_insert_before(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]),
  256. &RT_THREAD_LIST_NODE(thread));
  257. }
  258. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  259. else
  260. {
  261. rt_list_insert_after(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority]),
  262. &RT_THREAD_LIST_NODE(thread));
  263. }
  264. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  265. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  266. }
  267. else
  268. {
  269. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  270. #if RT_THREAD_PRIORITY_MAX > 32
  271. pcpu->ready_table[RT_SCHED_PRIV(thread).number] |= RT_SCHED_PRIV(thread).high_mask;
  272. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  273. pcpu->priority_group |= RT_SCHED_PRIV(thread).number_mask;
  274. /* there is no time slices left(YIELD), inserting thread before ready list*/
  275. if((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  276. {
  277. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[RT_SCHED_PRIV(thread).current_priority]),
  278. &RT_THREAD_LIST_NODE(thread));
  279. }
  280. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  281. else
  282. {
  283. rt_list_insert_after(&(rt_cpu_index(bind_cpu)->priority_table[RT_SCHED_PRIV(thread).current_priority]),
  284. &RT_THREAD_LIST_NODE(thread));
  285. }
  286. if (cpu_id != bind_cpu)
  287. {
  288. cpu_mask = 1 << bind_cpu;
  289. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  290. }
  291. }
  292. LOG_D("insert thread[%.*s], the priority: %d",
  293. RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(thread).current_priority);
  294. }
  295. /* remove thread from ready queue */
  296. static void _sched_remove_thread_locked(struct rt_thread *thread)
  297. {
  298. LOG_D("%s [%.*s], the priority: %d", __func__,
  299. RT_NAME_MAX, thread->parent.name,
  300. RT_SCHED_PRIV(thread).current_priority);
  301. /* remove thread from ready list */
  302. rt_list_remove(&RT_THREAD_LIST_NODE(thread));
  303. if (RT_SCHED_CTX(thread).bind_cpu == RT_CPUS_NR)
  304. {
  305. if (rt_list_isempty(&(rt_thread_priority_table[RT_SCHED_PRIV(thread).current_priority])))
  306. {
  307. #if RT_THREAD_PRIORITY_MAX > 32
  308. rt_thread_ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask;
  309. if (rt_thread_ready_table[RT_SCHED_PRIV(thread).number] == 0)
  310. {
  311. rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  312. }
  313. #else
  314. rt_thread_ready_priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  315. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  316. }
  317. }
  318. else
  319. {
  320. struct rt_cpu *pcpu = rt_cpu_index(RT_SCHED_CTX(thread).bind_cpu);
  321. if (rt_list_isempty(&(pcpu->priority_table[RT_SCHED_PRIV(thread).current_priority])))
  322. {
  323. #if RT_THREAD_PRIORITY_MAX > 32
  324. pcpu->ready_table[RT_SCHED_PRIV(thread).number] &= ~RT_SCHED_PRIV(thread).high_mask;
  325. if (pcpu->ready_table[RT_SCHED_PRIV(thread).number] == 0)
  326. {
  327. pcpu->priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  328. }
  329. #else
  330. pcpu->priority_group &= ~RT_SCHED_PRIV(thread).number_mask;
  331. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  332. }
  333. }
  334. }
  335. /**
  336. * @brief This function will initialize the system scheduler.
  337. */
  338. void rt_system_scheduler_init(void)
  339. {
  340. int cpu;
  341. rt_base_t offset;
  342. LOG_D("start scheduler: max priority 0x%02x",
  343. RT_THREAD_PRIORITY_MAX);
  344. rt_spin_lock_init(&_mp_scheduler_lock);
  345. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  346. {
  347. rt_list_init(&rt_thread_priority_table[offset]);
  348. }
  349. for (cpu = 0; cpu < RT_CPUS_NR; cpu++)
  350. {
  351. struct rt_cpu *pcpu = rt_cpu_index(cpu);
  352. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  353. {
  354. rt_list_init(&pcpu->priority_table[offset]);
  355. }
  356. pcpu->irq_switch_flag = 0;
  357. pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1;
  358. pcpu->current_thread = RT_NULL;
  359. pcpu->priority_group = 0;
  360. #if RT_THREAD_PRIORITY_MAX > 32
  361. rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table));
  362. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  363. #ifdef RT_USING_SMART
  364. rt_spin_lock_init(&(pcpu->spinlock));
  365. #endif
  366. }
  367. /* initialize ready priority group */
  368. rt_thread_ready_priority_group = 0;
  369. #if RT_THREAD_PRIORITY_MAX > 32
  370. /* initialize ready table */
  371. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  372. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  373. }
  374. /**
  375. * @brief This function will startup the scheduler. It will select one thread
  376. * with the highest priority level, then switch to it.
  377. */
  378. void rt_system_scheduler_start(void)
  379. {
  380. struct rt_thread *to_thread;
  381. rt_ubase_t highest_ready_priority;
  382. /**
  383. * legacy rt_cpus_lock. some bsp codes still use it as for it's critical
  384. * region. Since scheduler is never touching this, here we just release it
  385. * on the entry.
  386. */
  387. rt_hw_spin_unlock(&_cpus_lock);
  388. /* ISR will corrupt the coherency of running frame */
  389. rt_hw_local_irq_disable();
  390. /**
  391. * for the accessing of the scheduler context. Noted that we don't have
  392. * current_thread at this point
  393. */
  394. _fast_spin_lock(&_mp_scheduler_lock);
  395. /* get the thread scheduling to */
  396. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  397. RT_ASSERT(to_thread);
  398. /* to_thread is picked to running on current core, so remove it from ready queue */
  399. _sched_remove_thread_locked(to_thread);
  400. /* dedigate current core to `to_thread` */
  401. RT_SCHED_CTX(to_thread).oncpu = rt_hw_cpu_id();
  402. RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING;
  403. LOG_D("[cpu#%d] switch to priority#%d thread:%.*s(sp:0x%08x)",
  404. rt_hw_cpu_id(), RT_SCHED_PRIV(to_thread).current_priority,
  405. RT_NAME_MAX, to_thread->parent.name, to_thread->sp);
  406. _fast_spin_unlock(&_mp_scheduler_lock);
  407. /* switch to new thread */
  408. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread);
  409. /* never come back */
  410. }
  411. /**
  412. * @addtogroup Thread
  413. * @cond
  414. */
  415. /**@{*/
  416. /**
  417. * @brief This function will handle IPI interrupt and do a scheduling in system.
  418. *
  419. * @param vector is the number of IPI interrupt for system scheduling.
  420. *
  421. * @param param is not used, and can be set to RT_NULL.
  422. *
  423. * @note this function should be invoke or register as ISR in BSP.
  424. */
  425. void rt_scheduler_ipi_handler(int vector, void *param)
  426. {
  427. rt_schedule();
  428. }
  429. /**
  430. * @brief Lock the system scheduler
  431. *
  432. * @param plvl pointer to the object where lock level stores to
  433. *
  434. * @return rt_err_t RT_EOK
  435. */
  436. rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
  437. {
  438. rt_base_t level;
  439. if (!plvl)
  440. return -RT_EINVAL;
  441. SCHEDULER_LOCK(level);
  442. *plvl = level;
  443. return RT_EOK;
  444. }
  445. /**
  446. * @brief Unlock the system scheduler
  447. * @note this will not cause the scheduler to do a reschedule
  448. *
  449. * @param level the lock level of previous call to rt_sched_lock()
  450. *
  451. * @return rt_err_t RT_EOK
  452. */
  453. rt_err_t rt_sched_unlock(rt_sched_lock_level_t level)
  454. {
  455. SCHEDULER_UNLOCK(level);
  456. return RT_EOK;
  457. }
  458. rt_bool_t rt_sched_is_locked(void)
  459. {
  460. rt_bool_t rc;
  461. rt_base_t level;
  462. struct rt_cpu *pcpu;
  463. level = rt_hw_local_irq_disable();
  464. pcpu = rt_cpu_self();
  465. /* get lock stat which is a boolean value */
  466. rc = pcpu->sched_lock_flag;
  467. rt_hw_local_irq_enable(level);
  468. return rc;
  469. }
  470. /**
  471. * @brief Pick the highest runnable thread, and pass the control to it
  472. *
  473. * @note caller should hold the scheduler context lock. lock will be released
  474. * before return from this routine
  475. */
  476. static rt_thread_t _prepare_context_switch_locked(int cpu_id,
  477. struct rt_cpu *pcpu,
  478. rt_thread_t current_thread)
  479. {
  480. rt_thread_t to_thread = RT_NULL;
  481. rt_ubase_t highest_ready_priority;
  482. /* quickly check if any other ready threads queuing */
  483. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  484. {
  485. /* pick the highest ready thread */
  486. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  487. /* detach current thread from percpu scheduling context */
  488. RT_SCHED_CTX(current_thread).oncpu = RT_CPU_DETACHED;
  489. /* check if current thread should be put to ready queue, or scheduling again */
  490. if ((RT_SCHED_CTX(current_thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  491. {
  492. /* check if current thread can be running on current core again */
  493. if (RT_SCHED_CTX(current_thread).bind_cpu == RT_CPUS_NR
  494. || RT_SCHED_CTX(current_thread).bind_cpu == cpu_id)
  495. {
  496. /* if current_thread is the highest runnable thread */
  497. if (RT_SCHED_PRIV(current_thread).current_priority < highest_ready_priority)
  498. {
  499. to_thread = current_thread;
  500. }
  501. /* or no higher-priority thread existed and it has remaining ticks */
  502. else if (RT_SCHED_PRIV(current_thread).current_priority == highest_ready_priority &&
  503. (RT_SCHED_CTX(current_thread).stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  504. {
  505. to_thread = current_thread;
  506. }
  507. /* otherwise give out the core */
  508. else
  509. {
  510. _sched_insert_thread_locked(current_thread);
  511. }
  512. }
  513. else
  514. {
  515. /* put current_thread to ready queue of another core */
  516. _sched_insert_thread_locked(current_thread);
  517. }
  518. /* consume the yield flags after scheduling */
  519. RT_SCHED_CTX(current_thread).stat &= ~RT_THREAD_STAT_YIELD_MASK;
  520. }
  521. /**
  522. * Now destination thread is determined, core is passed to it. Though
  523. * the percpu scheduling context is not updated here, since the cpu
  524. * is locked contiguously before all the scheduling works are done, it's
  525. * safe to observe that current thread as the running thread on this
  526. * core for any observers if they properly do the synchronization
  527. * (take the SCHEDULER_LOCK).
  528. */
  529. RT_SCHED_CTX(to_thread).oncpu = cpu_id;
  530. /* check if context switch is required */
  531. if (to_thread != current_thread)
  532. {
  533. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  534. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  535. /* remove to_thread from ready queue and update its status to RUNNING */
  536. _sched_remove_thread_locked(to_thread);
  537. RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING | (RT_SCHED_CTX(to_thread).stat & ~RT_THREAD_STAT_MASK);
  538. RT_SCHEDULER_STACK_CHECK(to_thread);
  539. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  540. }
  541. else
  542. {
  543. /* current thread is still the best runnable thread */
  544. to_thread = RT_NULL;
  545. }
  546. }
  547. else
  548. {
  549. /* no ready threads */
  550. to_thread = RT_NULL;
  551. }
  552. return to_thread;
  553. }
  554. #ifdef RT_USING_SIGNALS
  555. static void _sched_thread_preprocess_signal(struct rt_thread *current_thread)
  556. {
  557. /* should process signal? */
  558. if (rt_sched_thread_is_suspended(current_thread))
  559. {
  560. /* if current_thread signal is in pending */
  561. if ((RT_SCHED_CTX(current_thread).stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  562. {
  563. #ifdef RT_USING_SMART
  564. rt_thread_wakeup(current_thread);
  565. #else
  566. rt_thread_resume(current_thread);
  567. #endif
  568. }
  569. }
  570. }
  571. static void _sched_thread_process_signal(struct rt_thread *current_thread)
  572. {
  573. rt_base_t level;
  574. SCHEDULER_LOCK(level);
  575. /* check stat of thread for signal */
  576. if (RT_SCHED_CTX(current_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING)
  577. {
  578. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  579. RT_SCHED_CTX(current_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  580. SCHEDULER_UNLOCK(level);
  581. /* check signal status */
  582. rt_thread_handle_sig(RT_TRUE);
  583. }
  584. else
  585. {
  586. SCHEDULER_UNLOCK(level);
  587. }
  588. /* lock is released above */
  589. }
  590. #define SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, curthr) \
  591. do \
  592. { \
  593. SCHEDULER_CONTEXT_LOCK(pcpu); \
  594. _sched_thread_preprocess_signal(curthr); \
  595. SCHEDULER_CONTEXT_UNLOCK(pcpu); \
  596. } while (0)
  597. #define SCHED_THREAD_PREPROCESS_SIGNAL_LOCKED(curthr) \
  598. _sched_thread_preprocess_signal(curthr)
  599. #define SCHED_THREAD_PROCESS_SIGNAL(curthr) _sched_thread_process_signal(curthr)
  600. #else /* ! RT_USING_SIGNALS */
  601. #define SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, curthr)
  602. #define SCHED_THREAD_PREPROCESS_SIGNAL_LOCKED(curthr)
  603. #define SCHED_THREAD_PROCESS_SIGNAL(curthr)
  604. #endif /* RT_USING_SIGNALS */
  605. rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
  606. {
  607. struct rt_thread *to_thread;
  608. struct rt_thread *current_thread;
  609. struct rt_cpu *pcpu;
  610. int cpu_id;
  611. rt_err_t error = RT_EOK;
  612. cpu_id = rt_hw_cpu_id();
  613. pcpu = rt_cpu_index(cpu_id);
  614. current_thread = pcpu->current_thread;
  615. if (!current_thread)
  616. {
  617. /* scheduler is unavailable yet */
  618. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  619. SCHEDULER_EXIT_CRITICAL(current_thread);
  620. rt_hw_local_irq_enable(level);
  621. return -RT_EBUSY;
  622. }
  623. /* whether do switch in interrupt */
  624. if (rt_atomic_load(&(pcpu->irq_nest)))
  625. {
  626. pcpu->irq_switch_flag = 1;
  627. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  628. SCHEDULER_EXIT_CRITICAL(current_thread);
  629. rt_hw_local_irq_enable(level);
  630. return -RT_ESCHEDISR;
  631. }
  632. /* prepare current_thread for processing if signals existed */
  633. SCHED_THREAD_PREPROCESS_SIGNAL_LOCKED(current_thread);
  634. /* whether caller had locked the local scheduler already */
  635. if (RT_SCHED_CTX(current_thread).critical_lock_nest > 1)
  636. {
  637. /* leaving critical region of global context since we can't schedule */
  638. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  639. pcpu->critical_switch_flag = 1;
  640. error = -RT_ESCHEDLOCKED;
  641. SCHEDULER_EXIT_CRITICAL(current_thread);
  642. }
  643. else
  644. {
  645. /* flush critical switch flag since a scheduling is done */
  646. pcpu->critical_switch_flag = 0;
  647. /* pick the highest runnable thread, and pass the control to it */
  648. to_thread = _prepare_context_switch_locked(cpu_id, pcpu, current_thread);
  649. if (to_thread)
  650. {
  651. /* switch to new thread */
  652. LOG_D("[cpu#%d] UNLOCK switch to priority#%d "
  653. "thread:%.*s(sp:0x%08x), "
  654. "from thread:%.*s(sp: 0x%08x)",
  655. cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
  656. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  657. RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
  658. rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
  659. (rt_ubase_t)&to_thread->sp, to_thread);
  660. }
  661. else
  662. {
  663. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  664. SCHEDULER_EXIT_CRITICAL(current_thread);
  665. }
  666. }
  667. /* leaving critical region of percpu scheduling context */
  668. rt_hw_local_irq_enable(level);
  669. /* process signals on thread if any existed */
  670. SCHED_THREAD_PROCESS_SIGNAL(current_thread);
  671. return error;
  672. }
  673. /**
  674. * @brief This function will perform one scheduling. It will select one thread
  675. * with the highest priority level in global ready queue or local ready queue,
  676. * then switch to it.
  677. */
  678. void rt_schedule(void)
  679. {
  680. rt_base_t level;
  681. struct rt_thread *to_thread;
  682. struct rt_thread *current_thread;
  683. struct rt_cpu *pcpu;
  684. int cpu_id;
  685. /* enter ciritical region of percpu scheduling context */
  686. level = rt_hw_local_irq_disable();
  687. /* get percpu scheduling context */
  688. cpu_id = rt_hw_cpu_id();
  689. pcpu = rt_cpu_index(cpu_id);
  690. current_thread = pcpu->current_thread;
  691. /* whether do switch in interrupt */
  692. if (rt_atomic_load(&(pcpu->irq_nest)))
  693. {
  694. pcpu->irq_switch_flag = 1;
  695. rt_hw_local_irq_enable(level);
  696. return ; /* -RT_ESCHEDISR */
  697. }
  698. /* forbid any recursive entries of schedule() */
  699. SCHEDULER_ENTER_CRITICAL(current_thread);
  700. /* prepare current_thread for processing if signals existed */
  701. SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, current_thread);
  702. /* whether caller had locked the local scheduler already */
  703. if (RT_SCHED_CTX(current_thread).critical_lock_nest > 1)
  704. {
  705. pcpu->critical_switch_flag = 1;
  706. SCHEDULER_EXIT_CRITICAL(current_thread);
  707. /* -RT_ESCHEDLOCKED */
  708. }
  709. else
  710. {
  711. /* flush critical switch flag since a scheduling is done */
  712. pcpu->critical_switch_flag = 0;
  713. pcpu->irq_switch_flag = 0;
  714. /**
  715. * take the context lock before we do the real scheduling works. Context
  716. * lock will be released before returning from this _schedule_locked()
  717. */
  718. SCHEDULER_CONTEXT_LOCK(pcpu);
  719. /* pick the highest runnable thread, and pass the control to it */
  720. to_thread = _prepare_context_switch_locked(cpu_id, pcpu, current_thread);
  721. if (to_thread)
  722. {
  723. LOG_D("[cpu#%d] switch to priority#%d "
  724. "thread:%.*s(sp:0x%08x), "
  725. "from thread:%.*s(sp: 0x%08x)",
  726. cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
  727. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  728. RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
  729. rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
  730. (rt_ubase_t)&to_thread->sp, to_thread);
  731. }
  732. else
  733. {
  734. /* current thread continue to take the core */
  735. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  736. SCHEDULER_EXIT_CRITICAL(current_thread);
  737. }
  738. }
  739. /* leaving critical region of percpu scheduling context */
  740. rt_hw_local_irq_enable(level);
  741. /* process signals on thread if any existed */
  742. SCHED_THREAD_PROCESS_SIGNAL(current_thread);
  743. }
  744. /**
  745. * @brief This function checks whether a scheduling is needed after an IRQ context switching. If yes,
  746. * it will select one thread with the highest priority level, and then switch
  747. * to it.
  748. */
  749. void rt_scheduler_do_irq_switch(void *context)
  750. {
  751. int cpu_id;
  752. rt_base_t level;
  753. struct rt_cpu *pcpu;
  754. struct rt_thread *to_thread;
  755. struct rt_thread *current_thread;
  756. level = rt_hw_local_irq_disable();
  757. cpu_id = rt_hw_cpu_id();
  758. pcpu = rt_cpu_index(cpu_id);
  759. current_thread = pcpu->current_thread;
  760. /* forbid any recursive entries of schedule() */
  761. SCHEDULER_ENTER_CRITICAL(current_thread);
  762. SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, current_thread);
  763. /* any pending scheduling existed? */
  764. if (pcpu->irq_switch_flag == 0)
  765. {
  766. /* if no, just continue execution of current_thread */
  767. SCHEDULER_EXIT_CRITICAL(current_thread);
  768. rt_hw_local_irq_enable(level);
  769. return;
  770. }
  771. /* whether caller had locked the local scheduler already */
  772. if (RT_SCHED_CTX(current_thread).critical_lock_nest > 1)
  773. {
  774. pcpu->critical_switch_flag = 1;
  775. SCHEDULER_EXIT_CRITICAL(current_thread);
  776. }
  777. else if (rt_atomic_load(&(pcpu->irq_nest)) == 0)
  778. {
  779. /* flush critical & irq switch flag since a scheduling is done */
  780. pcpu->critical_switch_flag = 0;
  781. pcpu->irq_switch_flag = 0;
  782. SCHEDULER_CONTEXT_LOCK(pcpu);
  783. /* pick the highest runnable thread, and pass the control to it */
  784. to_thread = _prepare_context_switch_locked(cpu_id, pcpu, current_thread);
  785. if (to_thread)
  786. {
  787. LOG_D("[cpu#%d] IRQ switch to priority#%d "
  788. "thread:%.*s(sp:0x%08x), "
  789. "from thread:%.*s(sp: 0x%08x)",
  790. cpu_id, RT_SCHED_PRIV(to_thread).current_priority,
  791. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  792. RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
  793. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  794. (rt_ubase_t)&to_thread->sp, to_thread);
  795. }
  796. else
  797. {
  798. /* current thread continue to take the core */
  799. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  800. SCHEDULER_EXIT_CRITICAL(current_thread);
  801. }
  802. }
  803. else
  804. {
  805. SCHEDULER_EXIT_CRITICAL(current_thread);
  806. }
  807. /* leaving critical region of percpu scheduling context */
  808. rt_hw_local_irq_enable(level);
  809. }
  810. /**
  811. * @brief This function will insert a thread to the system ready queue. The state of
  812. * thread will be set as READY and the thread will be removed from suspend queue.
  813. *
  814. * @param thread is the thread to be inserted.
  815. *
  816. * @note Please do not invoke this function in user application.
  817. * Caller must hold the scheduler lock
  818. */
  819. void rt_sched_insert_thread(struct rt_thread *thread)
  820. {
  821. RT_ASSERT(thread != RT_NULL);
  822. RT_SCHED_DEBUG_IS_LOCKED;
  823. /* set READY and insert thread to ready queue */
  824. _sched_insert_thread_locked(thread);
  825. }
  826. /**
  827. * @brief This function will remove a thread from system ready queue.
  828. *
  829. * @param thread is the thread to be removed.
  830. *
  831. * @note Please do not invoke this function in user application.
  832. */
  833. void rt_sched_remove_thread(struct rt_thread *thread)
  834. {
  835. RT_ASSERT(thread != RT_NULL);
  836. RT_SCHED_DEBUG_IS_LOCKED;
  837. /* remove thread from scheduler ready list */
  838. _sched_remove_thread_locked(thread);
  839. }
  840. /* thread status initialization and setting up on startup */
  841. void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority)
  842. {
  843. rt_list_init(&RT_THREAD_LIST_NODE(thread));
  844. /* priority init */
  845. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  846. RT_SCHED_PRIV(thread).init_priority = priority;
  847. RT_SCHED_PRIV(thread).current_priority = priority;
  848. /* don't add to scheduler queue as init thread */
  849. RT_SCHED_PRIV(thread).number_mask = 0;
  850. #if RT_THREAD_PRIORITY_MAX > 32
  851. RT_SCHED_PRIV(thread).number = 0;
  852. RT_SCHED_PRIV(thread).high_mask = 0;
  853. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  854. /* tick init */
  855. RT_SCHED_PRIV(thread).init_tick = tick;
  856. RT_SCHED_PRIV(thread).remaining_tick = tick;
  857. #ifdef RT_USING_SMP
  858. /* lock init */
  859. RT_SCHED_CTX(thread).critical_lock_nest = 0;
  860. #endif /* RT_USING_SMP */
  861. }
  862. /* Normally, there isn't anyone racing with us so this operation is lockless */
  863. void rt_sched_thread_startup(struct rt_thread *thread)
  864. {
  865. #if RT_THREAD_PRIORITY_MAX > 32
  866. RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */
  867. RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).number;
  868. RT_SCHED_PRIV(thread).high_mask = 1L << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */
  869. #else
  870. RT_SCHED_PRIV(thread).number_mask = 1L << RT_SCHED_PRIV(thread).current_priority;
  871. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  872. /* change thread stat, so we can resume it */
  873. RT_SCHED_CTX(thread).stat = RT_THREAD_SUSPEND;
  874. }
  875. /**
  876. * @brief Update scheduling status of thread. this operation is taken as an
  877. * atomic operation of the update of SP. Since the local irq is disabled,
  878. * it's okay to assume that the stack will not be modified meanwhile.
  879. */
  880. void rt_sched_post_ctx_switch(struct rt_thread *thread)
  881. {
  882. struct rt_cpu* pcpu = rt_cpu_self();
  883. rt_thread_t from_thread = pcpu->current_thread;
  884. RT_ASSERT(rt_hw_interrupt_is_disabled());
  885. if (from_thread)
  886. {
  887. RT_ASSERT(RT_SCHED_CTX(from_thread).critical_lock_nest == 1);
  888. /* release the scheduler lock since we are done with critical region */
  889. RT_SCHED_CTX(from_thread).critical_lock_nest = 0;
  890. SCHEDULER_CONTEXT_UNLOCK(pcpu);
  891. }
  892. /* safe to access since irq is masked out */
  893. pcpu->current_thread = thread;
  894. }
  895. #ifdef RT_DEBUGING_CRITICAL
  896. static volatile int _critical_error_occurred = 0;
  897. void rt_exit_critical_safe(rt_base_t critical_level)
  898. {
  899. struct rt_cpu *pcpu = rt_cpu_self();
  900. rt_thread_t current_thread = pcpu->current_thread;
  901. if (current_thread && !_critical_error_occurred)
  902. {
  903. if (critical_level != RT_SCHED_CTX(current_thread).critical_lock_nest)
  904. {
  905. int dummy = 1;
  906. _critical_error_occurred = 1;
  907. rt_kprintf("%s: un-compatible critical level\n" \
  908. "\tCurrent %d\n\tCaller %d\n",
  909. __func__, RT_SCHED_CTX(current_thread).critical_lock_nest,
  910. critical_level);
  911. rt_backtrace();
  912. while (dummy) ;
  913. }
  914. }
  915. rt_exit_critical();
  916. }
  917. #else /* !RT_DEBUGING_CRITICAL */
  918. void rt_exit_critical_safe(rt_base_t critical_level)
  919. {
  920. return rt_exit_critical();
  921. }
  922. #endif /* RT_DEBUGING_CRITICAL */
  923. RTM_EXPORT(rt_exit_critical_safe);
  924. /**
  925. * @brief This function will lock the thread scheduler.
  926. */
  927. rt_base_t rt_enter_critical(void)
  928. {
  929. rt_base_t level;
  930. rt_base_t critical_level;
  931. struct rt_thread *current_thread;
  932. struct rt_cpu *pcpu;
  933. /* disable interrupt */
  934. level = rt_hw_local_irq_disable();
  935. pcpu = rt_cpu_self();
  936. current_thread = pcpu->current_thread;
  937. if (!current_thread)
  938. {
  939. rt_hw_local_irq_enable(level);
  940. /* scheduler unavailable */
  941. return -RT_EINVAL;
  942. }
  943. /* critical for local cpu */
  944. RT_SCHED_CTX(current_thread).critical_lock_nest++;
  945. critical_level = RT_SCHED_CTX(current_thread).critical_lock_nest;
  946. /* enable interrupt */
  947. rt_hw_local_irq_enable(level);
  948. return critical_level;
  949. }
  950. RTM_EXPORT(rt_enter_critical);
  951. /**
  952. * @brief This function will unlock the thread scheduler.
  953. */
  954. void rt_exit_critical(void)
  955. {
  956. rt_base_t level;
  957. struct rt_thread *current_thread;
  958. rt_bool_t need_resched;
  959. struct rt_cpu *pcpu;
  960. /* disable interrupt */
  961. level = rt_hw_local_irq_disable();
  962. pcpu = rt_cpu_self();
  963. current_thread = pcpu->current_thread;
  964. if (!current_thread)
  965. {
  966. rt_hw_local_irq_enable(level);
  967. return;
  968. }
  969. /* the necessary memory barrier is done on irq_(dis|en)able */
  970. RT_SCHED_CTX(current_thread).critical_lock_nest--;
  971. /* may need a rescheduling */
  972. if (RT_SCHED_CTX(current_thread).critical_lock_nest == 0)
  973. {
  974. /* is there any scheduling request unfinished? */
  975. need_resched = pcpu->critical_switch_flag;
  976. pcpu->critical_switch_flag = 0;
  977. /* enable interrupt */
  978. rt_hw_local_irq_enable(level);
  979. if (need_resched)
  980. rt_schedule();
  981. }
  982. else
  983. {
  984. /* each exit_critical is strictly corresponding to an enter_critical */
  985. RT_ASSERT(RT_SCHED_CTX(current_thread).critical_lock_nest > 0);
  986. /* enable interrupt */
  987. rt_hw_local_irq_enable(level);
  988. }
  989. }
  990. RTM_EXPORT(rt_exit_critical);
  991. /**
  992. * @brief Get the scheduler lock level.
  993. *
  994. * @return the level of the scheduler lock. 0 means unlocked.
  995. */
  996. rt_uint16_t rt_critical_level(void)
  997. {
  998. rt_base_t level;
  999. rt_uint16_t critical_lvl;
  1000. struct rt_thread *current_thread;
  1001. level = rt_hw_local_irq_disable();
  1002. current_thread = rt_cpu_self()->current_thread;
  1003. if (current_thread)
  1004. {
  1005. /* the necessary memory barrier is done on irq_(dis|en)able */
  1006. critical_lvl = RT_SCHED_CTX(current_thread).critical_lock_nest;
  1007. }
  1008. else
  1009. {
  1010. critical_lvl = 0;
  1011. }
  1012. rt_hw_local_irq_enable(level);
  1013. return critical_lvl;
  1014. }
  1015. RTM_EXPORT(rt_critical_level);
  1016. rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu)
  1017. {
  1018. rt_sched_lock_level_t slvl;
  1019. rt_uint8_t thread_stat;
  1020. RT_SCHED_DEBUG_IS_UNLOCKED;
  1021. if (cpu >= RT_CPUS_NR)
  1022. {
  1023. cpu = RT_CPUS_NR;
  1024. }
  1025. rt_sched_lock(&slvl);
  1026. thread_stat = rt_sched_thread_get_stat(thread);
  1027. if (thread_stat == RT_THREAD_READY)
  1028. {
  1029. /* unbind */
  1030. /* remove from old ready queue */
  1031. rt_sched_remove_thread(thread);
  1032. /* change thread bind cpu */
  1033. RT_SCHED_CTX(thread).bind_cpu = cpu;
  1034. /* add to new ready queue */
  1035. rt_sched_insert_thread(thread);
  1036. if (rt_thread_self() != RT_NULL)
  1037. {
  1038. rt_sched_unlock_n_resched(slvl);
  1039. }
  1040. else
  1041. {
  1042. rt_sched_unlock(slvl);
  1043. }
  1044. }
  1045. else
  1046. {
  1047. RT_SCHED_CTX(thread).bind_cpu = cpu;
  1048. if (thread_stat == RT_THREAD_RUNNING)
  1049. {
  1050. /* thread is running on a cpu */
  1051. int current_cpu = rt_hw_cpu_id();
  1052. if (cpu != RT_CPUS_NR)
  1053. {
  1054. if (RT_SCHED_CTX(thread).oncpu == current_cpu)
  1055. {
  1056. /* current thread on current cpu */
  1057. if (cpu != current_cpu)
  1058. {
  1059. /* bind to other cpu */
  1060. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << cpu);
  1061. /* self cpu need reschedule */
  1062. rt_sched_unlock_n_resched(slvl);
  1063. }
  1064. else
  1065. {
  1066. /* else do nothing */
  1067. rt_sched_unlock(slvl);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. /* no running on self cpu, but dest cpu can be itself */
  1073. rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << RT_SCHED_CTX(thread).oncpu);
  1074. rt_sched_unlock(slvl);
  1075. }
  1076. }
  1077. else
  1078. {
  1079. /* else do nothing */
  1080. rt_sched_unlock(slvl);
  1081. }
  1082. }
  1083. else
  1084. {
  1085. rt_sched_unlock(slvl);
  1086. }
  1087. }
  1088. return RT_EOK;
  1089. }
  1090. /**@}*/
  1091. /**@endcond*/