scheduler_mp.c 42 KB

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