scheduler_mp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. */
  35. #include <rtthread.h>
  36. #include <rthw.h>
  37. #define DBG_TAG "kernel.scheduler"
  38. #define DBG_LVL DBG_INFO
  39. #include <rtdbg.h>
  40. rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  41. static rt_hw_spinlock_t _mp_scheduler_spinlock;
  42. rt_uint32_t rt_thread_ready_priority_group;
  43. #if RT_THREAD_PRIORITY_MAX > 32
  44. /* Maximum priority level, 256 */
  45. rt_uint8_t rt_thread_ready_table[32];
  46. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  47. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  48. static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
  49. static void (*rt_scheduler_switch_hook)(struct rt_thread *tid);
  50. /**
  51. * @addtogroup Hook
  52. */
  53. /**@{*/
  54. /**
  55. * @brief This function will set a hook function, which will be invoked when thread
  56. * switch happens.
  57. *
  58. * @param hook is the hook function.
  59. */
  60. void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
  61. {
  62. rt_scheduler_hook = hook;
  63. }
  64. /**
  65. * @brief This function will set a hook function, which will be invoked when context
  66. * switch happens.
  67. *
  68. * @param hook is the hook function.
  69. */
  70. void rt_scheduler_switch_sethook(void (*hook)(struct rt_thread *tid))
  71. {
  72. rt_scheduler_switch_hook = hook;
  73. }
  74. /**@}*/
  75. #endif /* RT_USING_HOOK */
  76. #ifdef RT_USING_OVERFLOW_CHECK
  77. static void _scheduler_stack_check(struct rt_thread *thread)
  78. {
  79. RT_ASSERT(thread != RT_NULL);
  80. #ifdef RT_USING_SMART
  81. #ifndef ARCH_MM_MMU
  82. struct rt_lwp *lwp = thread ? (struct rt_lwp *)thread->lwp : 0;
  83. /* if stack pointer locate in user data section skip stack check. */
  84. if (lwp && ((rt_uint32_t)thread->sp > (rt_uint32_t)lwp->data_entry &&
  85. (rt_uint32_t)thread->sp <= (rt_uint32_t)lwp->data_entry + (rt_uint32_t)lwp->data_size))
  86. {
  87. return;
  88. }
  89. #endif /* not defined ARCH_MM_MMU */
  90. #endif /* RT_USING_SMART */
  91. #ifndef RT_USING_HW_STACK_GUARD
  92. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  93. if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
  94. #else
  95. if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
  96. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  97. (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
  98. (rt_ubase_t)thread->sp >
  99. (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
  100. {
  101. rt_base_t level;
  102. rt_kprintf("thread:%s stack overflow\n", thread->parent.name);
  103. level = rt_hw_local_irq_disable();
  104. rt_hw_spin_lock(&_mp_scheduler_spinlock);
  105. while (level);
  106. }
  107. #endif
  108. #ifdef ARCH_CPU_STACK_GROWS_UPWARD
  109. #ifndef RT_USING_HW_STACK_GUARD
  110. else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
  111. #else
  112. if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
  113. #endif
  114. {
  115. rt_kprintf("warning: %s stack is close to the top of stack address.\n",
  116. thread->parent.name);
  117. }
  118. #else
  119. #ifndef RT_USING_HW_STACK_GUARD
  120. else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
  121. #else
  122. if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
  123. #endif
  124. {
  125. rt_kprintf("warning: %s stack is close to end of stack address.\n",
  126. thread->parent.name);
  127. }
  128. #endif /* ARCH_CPU_STACK_GROWS_UPWARD */
  129. }
  130. #endif /* RT_USING_OVERFLOW_CHECK */
  131. /*
  132. * get the highest priority thread in ready queue
  133. */
  134. static struct rt_thread* _scheduler_get_highest_priority_thread(rt_ubase_t *highest_prio)
  135. {
  136. struct rt_thread *highest_priority_thread;
  137. rt_ubase_t highest_ready_priority, local_highest_ready_priority;
  138. struct rt_cpu* pcpu = rt_cpu_self();
  139. #if RT_THREAD_PRIORITY_MAX > 32
  140. rt_ubase_t number;
  141. number = __rt_ffs(rt_thread_ready_priority_group) - 1;
  142. highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
  143. number = __rt_ffs(pcpu->priority_group) - 1;
  144. local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1;
  145. #else
  146. highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
  147. local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1;
  148. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  149. /* get highest ready priority thread */
  150. if (highest_ready_priority < local_highest_ready_priority)
  151. {
  152. *highest_prio = highest_ready_priority;
  153. highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
  154. struct rt_thread,
  155. tlist_schedule);
  156. }
  157. else
  158. {
  159. *highest_prio = local_highest_ready_priority;
  160. highest_priority_thread = rt_list_entry(pcpu->priority_table[local_highest_ready_priority].next,
  161. struct rt_thread,
  162. tlist_schedule);
  163. }
  164. return highest_priority_thread;
  165. }
  166. /**
  167. * @brief This function will initialize the system scheduler.
  168. */
  169. void rt_system_scheduler_init(void)
  170. {
  171. int cpu;
  172. rt_base_t offset;
  173. LOG_D("start scheduler: max priority 0x%02x",
  174. RT_THREAD_PRIORITY_MAX);
  175. rt_hw_spin_lock_init(&_mp_scheduler_spinlock);
  176. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  177. {
  178. rt_list_init(&rt_thread_priority_table[offset]);
  179. }
  180. for (cpu = 0; cpu < RT_CPUS_NR; cpu++)
  181. {
  182. struct rt_cpu *pcpu = rt_cpu_index(cpu);
  183. for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
  184. {
  185. rt_list_init(&pcpu->priority_table[offset]);
  186. }
  187. pcpu->irq_switch_flag = 0;
  188. pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1;
  189. pcpu->current_thread = RT_NULL;
  190. pcpu->priority_group = 0;
  191. #if RT_THREAD_PRIORITY_MAX > 32
  192. rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table));
  193. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  194. rt_spin_lock_init(&(pcpu->spinlock));
  195. }
  196. /* initialize ready priority group */
  197. rt_thread_ready_priority_group = 0;
  198. #if RT_THREAD_PRIORITY_MAX > 32
  199. /* initialize ready table */
  200. rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
  201. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  202. }
  203. /**
  204. * @addtogroup Thread
  205. * @cond
  206. */
  207. /**@{*/
  208. /**
  209. * @brief This function will handle IPI interrupt and do a scheduling in system.
  210. *
  211. * @param vector is the number of IPI interrupt for system scheduling.
  212. *
  213. * @param param is not used, and can be set to RT_NULL.
  214. *
  215. * @note this function should be invoke or register as ISR in BSP.
  216. */
  217. void rt_scheduler_ipi_handler(int vector, void *param)
  218. {
  219. rt_schedule();
  220. }
  221. static void _rt_schedule_insert_thread(struct rt_thread *thread, rt_bool_t is_lock)
  222. {
  223. int cpu_id;
  224. int bind_cpu;
  225. rt_uint32_t cpu_mask;
  226. RT_ASSERT(thread != RT_NULL);
  227. /* disable interrupt */
  228. if(is_lock)
  229. {
  230. rt_hw_spin_lock(&(thread->spinlock.lock));
  231. }
  232. if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
  233. {
  234. if(is_lock)
  235. {
  236. rt_hw_spin_unlock(&(thread->spinlock.lock));
  237. }
  238. return;
  239. }
  240. /* it should be RUNNING thread */
  241. if (thread->oncpu != RT_CPU_DETACHED)
  242. {
  243. thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
  244. if(is_lock)
  245. {
  246. rt_hw_spin_unlock(&(thread->spinlock.lock));
  247. }
  248. return;
  249. }
  250. /* READY thread, insert to ready queue */
  251. thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
  252. cpu_id = rt_hw_cpu_id();
  253. bind_cpu = thread->bind_cpu ;
  254. /* insert thread to ready list */
  255. if (bind_cpu == RT_CPUS_NR)
  256. {
  257. #if RT_THREAD_PRIORITY_MAX > 32
  258. rt_thread_ready_table[thread->number] |= thread->high_mask;
  259. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  260. rt_thread_ready_priority_group |= thread->number_mask;
  261. /* there is no time slices left(YIELD), inserting thread before ready list*/
  262. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  263. {
  264. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  265. &(thread->tlist_schedule));
  266. }
  267. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  268. else
  269. {
  270. rt_list_insert_after(&(rt_thread_priority_table[thread->current_priority]),
  271. &(thread->tlist_schedule));
  272. }
  273. if(is_lock)
  274. {
  275. rt_hw_spin_unlock(&(thread->spinlock.lock));
  276. }
  277. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  278. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  279. }
  280. else
  281. {
  282. struct rt_cpu *pcpu = rt_cpu_index(bind_cpu);
  283. if(is_lock)
  284. {
  285. rt_hw_spin_lock(&(pcpu->spinlock.lock));
  286. }
  287. #if RT_THREAD_PRIORITY_MAX > 32
  288. pcpu->ready_table[thread->number] |= thread->high_mask;
  289. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  290. pcpu->priority_group |= thread->number_mask;
  291. /* there is no time slices left(YIELD), inserting thread before ready list*/
  292. if((thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
  293. {
  294. rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  295. &(thread->tlist_schedule));
  296. }
  297. /* there are some time slices left, inserting thread after ready list to schedule it firstly at next time*/
  298. else
  299. {
  300. rt_list_insert_after(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]),
  301. &(thread->tlist_schedule));
  302. }
  303. if(is_lock)
  304. {
  305. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  306. rt_hw_spin_unlock(&(thread->spinlock.lock));
  307. }
  308. if (cpu_id != bind_cpu)
  309. {
  310. cpu_mask = 1 << bind_cpu;
  311. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  312. }
  313. }
  314. LOG_D("insert thread[%.*s], the priority: %d",
  315. RT_NAME_MAX, thread->parent.name, thread->current_priority);
  316. }
  317. static void _rt_schedule_remove_thread(struct rt_thread *thread, rt_bool_t is_lock)
  318. {
  319. RT_ASSERT(thread != RT_NULL);
  320. LOG_D("remove thread[%.*s], the priority: %d",
  321. RT_NAME_MAX, thread->parent.name,
  322. thread->current_priority);
  323. /* remove thread from ready list */
  324. rt_list_remove(&(thread->tlist_schedule));
  325. if (thread->bind_cpu == RT_CPUS_NR)
  326. {
  327. if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
  328. {
  329. #if RT_THREAD_PRIORITY_MAX > 32
  330. rt_thread_ready_table[thread->number] &= ~thread->high_mask;
  331. if (rt_thread_ready_table[thread->number] == 0)
  332. {
  333. rt_thread_ready_priority_group &= ~thread->number_mask;
  334. }
  335. #else
  336. rt_thread_ready_priority_group &= ~thread->number_mask;
  337. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  338. }
  339. }
  340. else
  341. {
  342. struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu);
  343. if(is_lock)
  344. {
  345. rt_hw_spin_lock(&(pcpu->spinlock.lock));
  346. }
  347. if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority])))
  348. {
  349. #if RT_THREAD_PRIORITY_MAX > 32
  350. pcpu->ready_table[thread->number] &= ~thread->high_mask;
  351. if (pcpu->ready_table[thread->number] == 0)
  352. {
  353. pcpu->priority_group &= ~thread->number_mask;
  354. }
  355. #else
  356. pcpu->priority_group &= ~thread->number_mask;
  357. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  358. }
  359. if(is_lock)
  360. {
  361. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  362. }
  363. }
  364. }
  365. /**
  366. * @brief This function will startup the scheduler. It will select one thread
  367. * with the highest priority level, then switch to it.
  368. */
  369. void rt_system_scheduler_start(void)
  370. {
  371. struct rt_thread *to_thread;
  372. rt_ubase_t highest_ready_priority;
  373. rt_hw_local_irq_disable();
  374. rt_hw_spin_lock(&_mp_scheduler_spinlock);
  375. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  376. rt_hw_spin_lock(&(to_thread->spinlock.lock));
  377. to_thread->oncpu = rt_hw_cpu_id();
  378. _rt_schedule_remove_thread(to_thread, RT_TRUE);
  379. to_thread->stat = RT_THREAD_RUNNING;
  380. rt_hw_spin_unlock(&(to_thread->spinlock.lock));
  381. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  382. rt_hw_spin_unlock(&_cpus_lock);
  383. /* switch to new thread */
  384. rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread);
  385. /* never come back */
  386. }
  387. /**
  388. * @brief This function will perform one scheduling. It will select one thread
  389. * with the highest priority level in global ready queue or local ready queue,
  390. * then switch to it.
  391. */
  392. void rt_schedule(void)
  393. {
  394. rt_base_t level;
  395. struct rt_thread *to_thread;
  396. struct rt_thread *current_thread;
  397. struct rt_cpu *pcpu;
  398. int cpu_id;
  399. rt_bool_t need_unlock = RT_TRUE;
  400. /* disable interrupt */
  401. level = rt_hw_local_irq_disable();
  402. rt_hw_spin_lock(&_mp_scheduler_spinlock);
  403. cpu_id = rt_hw_cpu_id();
  404. pcpu = rt_cpu_index(cpu_id);
  405. rt_hw_spin_lock(&(pcpu->spinlock.lock));
  406. current_thread = pcpu->current_thread;
  407. /* whether do switch in interrupt */
  408. if (rt_atomic_load(&(pcpu->irq_nest)))
  409. {
  410. pcpu->irq_switch_flag = 1;
  411. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  412. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  413. rt_hw_local_irq_enable(level);
  414. goto __exit;
  415. }
  416. #ifdef RT_USING_SIGNALS
  417. if ((current_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  418. {
  419. /* if current_thread signal is in pending */
  420. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  421. {
  422. #ifdef RT_USING_SMART
  423. rt_thread_wakeup(current_thread);
  424. #else
  425. rt_thread_resume(current_thread);
  426. #endif
  427. }
  428. }
  429. #endif /* RT_USING_SIGNALS */
  430. /* whether lock scheduler */
  431. if (rt_atomic_load(&(current_thread->critical_lock_nest)) != 0)
  432. {
  433. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  434. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  435. rt_hw_local_irq_enable(level);
  436. goto __exit;
  437. }
  438. rt_hw_spin_lock(&(current_thread->spinlock.lock));
  439. {
  440. rt_ubase_t highest_ready_priority;
  441. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  442. {
  443. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  444. current_thread->oncpu = RT_CPU_DETACHED;
  445. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  446. {
  447. if (current_thread->bind_cpu == RT_CPUS_NR || current_thread->bind_cpu == cpu_id)
  448. {
  449. if (current_thread->current_priority < highest_ready_priority)
  450. {
  451. to_thread = current_thread;
  452. }
  453. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  454. {
  455. to_thread = current_thread;
  456. }
  457. else
  458. {
  459. _rt_schedule_insert_thread(current_thread, RT_FALSE);
  460. }
  461. }
  462. else
  463. {
  464. _rt_schedule_insert_thread(current_thread, RT_FALSE);
  465. }
  466. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  467. }
  468. if (to_thread != current_thread)
  469. {
  470. rt_hw_spin_lock(&(to_thread->spinlock.lock));
  471. }
  472. to_thread->oncpu = cpu_id;
  473. if (to_thread != current_thread)
  474. {
  475. /* if the destination thread is not the same as current thread */
  476. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  477. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  478. _rt_schedule_remove_thread(to_thread, RT_FALSE);
  479. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  480. /* switch to new thread */
  481. LOG_D("[%d]switch to priority#%d "
  482. "thread:%.*s(sp:0x%08x), "
  483. "from thread:%.*s(sp: 0x%08x)",
  484. rt_atomic_load(&(pcpu->irq_nest)), highest_ready_priority,
  485. RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
  486. RT_NAME_MAX, current_thread->parent.name, current_thread->sp);
  487. #ifdef RT_USING_OVERFLOW_CHECK
  488. _scheduler_stack_check(to_thread);
  489. #endif /* RT_USING_OVERFLOW_CHECK */
  490. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  491. rt_hw_spin_unlock(&(to_thread->spinlock.lock));
  492. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  493. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  494. need_unlock = RT_FALSE;
  495. rt_hw_context_switch((rt_ubase_t)&current_thread->sp,
  496. (rt_ubase_t)&to_thread->sp, to_thread);
  497. }
  498. }
  499. }
  500. if(need_unlock)
  501. {
  502. rt_hw_spin_unlock(&(current_thread->spinlock.lock));
  503. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  504. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  505. }
  506. rt_hw_local_irq_enable(level);
  507. #ifdef RT_USING_SIGNALS
  508. /* check stat of thread for signal */
  509. rt_hw_spin_lock(&(current_thread->spinlock));
  510. if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
  511. {
  512. extern void rt_thread_handle_sig(rt_bool_t clean_state);
  513. current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  514. rt_hw_spin_unlock(&(current_thread->spinlock));
  515. /* check signal status */
  516. rt_thread_handle_sig(RT_TRUE);
  517. }
  518. else
  519. {
  520. rt_hw_spin_unlock(&(current_thread->spinlock));
  521. }
  522. #endif /* RT_USING_SIGNALS */
  523. __exit:
  524. return ;
  525. }
  526. /**
  527. * @brief This function checks whether a scheduling is needed after an IRQ context switching. If yes,
  528. * it will select one thread with the highest priority level, and then switch
  529. * to it.
  530. */
  531. void rt_scheduler_do_irq_switch(void *context)
  532. {
  533. int cpu_id;
  534. rt_base_t level;
  535. struct rt_cpu *pcpu;
  536. struct rt_thread *to_thread;
  537. struct rt_thread *current_thread;
  538. rt_bool_t need_unlock = RT_TRUE;
  539. level = rt_hw_local_irq_disable();
  540. rt_hw_spin_lock(&_mp_scheduler_spinlock);
  541. cpu_id = rt_hw_cpu_id();
  542. pcpu = rt_cpu_index(cpu_id);
  543. rt_hw_spin_lock(&(pcpu->spinlock.lock));
  544. current_thread = pcpu->current_thread;
  545. #ifdef RT_USING_SIGNALS
  546. if ((current_thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  547. {
  548. /* if current_thread signal is in pending */
  549. if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
  550. {
  551. #ifdef RT_USING_SMART
  552. rt_thread_wakeup(current_thread);
  553. #else
  554. rt_thread_resume(current_thread);
  555. #endif
  556. }
  557. }
  558. #endif /* RT_USING_SIGNALS */
  559. if (pcpu->irq_switch_flag == 0)
  560. {
  561. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  562. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  563. rt_hw_local_irq_enable(level);
  564. return;
  565. }
  566. /* whether lock scheduler */
  567. if (rt_atomic_load(&(current_thread->critical_lock_nest)) != 0)
  568. {
  569. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  570. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  571. rt_hw_local_irq_enable(level);
  572. return;
  573. }
  574. rt_hw_spin_lock(&(current_thread->spinlock.lock));
  575. if (rt_atomic_load(&(pcpu->irq_nest)) == 0)
  576. {
  577. rt_ubase_t highest_ready_priority;
  578. /* clear irq switch flag */
  579. pcpu->irq_switch_flag = 0;
  580. if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0)
  581. {
  582. to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);
  583. current_thread->oncpu = RT_CPU_DETACHED;
  584. if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
  585. {
  586. if (current_thread->bind_cpu == RT_CPUS_NR || current_thread->bind_cpu == cpu_id)
  587. {
  588. if (current_thread->current_priority < highest_ready_priority)
  589. {
  590. to_thread = current_thread;
  591. }
  592. else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
  593. {
  594. to_thread = current_thread;
  595. }
  596. else
  597. {
  598. _rt_schedule_insert_thread(current_thread, RT_FALSE);
  599. }
  600. }
  601. else
  602. {
  603. _rt_schedule_insert_thread(current_thread, RT_FALSE);
  604. }
  605. current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
  606. }
  607. if (to_thread != current_thread)
  608. {
  609. rt_hw_spin_lock(&(to_thread->spinlock.lock));
  610. }
  611. to_thread->oncpu = cpu_id;
  612. if (to_thread != current_thread)
  613. {
  614. /* if the destination thread is not the same as current thread */
  615. pcpu->current_priority = (rt_uint8_t)highest_ready_priority;
  616. RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
  617. _rt_schedule_remove_thread(to_thread, RT_FALSE);
  618. to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
  619. #ifdef RT_USING_OVERFLOW_CHECK
  620. _scheduler_stack_check(to_thread);
  621. #endif /* RT_USING_OVERFLOW_CHECK */
  622. LOG_D("switch in interrupt");
  623. RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (current_thread));
  624. rt_hw_spin_unlock(&(to_thread->spinlock.lock));
  625. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  626. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  627. need_unlock = RT_FALSE;
  628. rt_hw_context_switch_interrupt(context, (rt_ubase_t)&current_thread->sp,
  629. (rt_ubase_t)&to_thread->sp, to_thread);
  630. }
  631. }
  632. }
  633. if(need_unlock)
  634. {
  635. rt_hw_spin_unlock(&(current_thread->spinlock.lock));
  636. rt_hw_spin_unlock(&(pcpu->spinlock.lock));
  637. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  638. }
  639. rt_hw_local_irq_enable(level);
  640. }
  641. /**
  642. * @brief This function will insert a thread to the system ready queue. The state of
  643. * thread will be set as READY and the thread will be removed from suspend queue.
  644. *
  645. * @param thread is the thread to be inserted.
  646. *
  647. * @note Please do not invoke this function in user application.
  648. */
  649. void rt_schedule_insert_thread(struct rt_thread *thread)
  650. {
  651. rt_base_t level;
  652. level = rt_hw_local_irq_disable();
  653. rt_hw_spin_lock(&_mp_scheduler_spinlock);
  654. _rt_schedule_insert_thread(thread, RT_TRUE);
  655. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  656. rt_hw_local_irq_enable(level);
  657. }
  658. /**
  659. * @brief This function will remove a thread from system ready queue.
  660. *
  661. * @param thread is the thread to be removed.
  662. *
  663. * @note Please do not invoke this function in user application.
  664. */
  665. void rt_schedule_remove_thread(struct rt_thread *thread)
  666. {
  667. rt_base_t level;
  668. level = rt_hw_local_irq_disable();
  669. rt_hw_spin_lock(&_mp_scheduler_spinlock);
  670. rt_hw_spin_lock(&(thread->spinlock.lock));
  671. _rt_schedule_remove_thread(thread, RT_TRUE);
  672. rt_hw_spin_unlock(&(thread->spinlock.lock));
  673. rt_hw_spin_unlock(&_mp_scheduler_spinlock);
  674. rt_hw_local_irq_enable(level);
  675. }
  676. /**
  677. * @brief This function will lock the thread scheduler.
  678. */
  679. void rt_enter_critical(void)
  680. {
  681. rt_base_t level;
  682. struct rt_thread *current_thread;
  683. /* disable interrupt */
  684. level = rt_hw_local_irq_disable();
  685. current_thread = rt_cpu_self()->current_thread;
  686. if (!current_thread)
  687. {
  688. rt_hw_local_irq_enable(level);
  689. return;
  690. }
  691. /* critical for local cpu */
  692. rt_atomic_add(&(current_thread->critical_lock_nest), 1);
  693. /* enable interrupt */
  694. rt_hw_local_irq_enable(level);
  695. }
  696. RTM_EXPORT(rt_enter_critical);
  697. /**
  698. * @brief This function will unlock the thread scheduler.
  699. */
  700. void rt_exit_critical(void)
  701. {
  702. rt_base_t level;
  703. struct rt_thread *current_thread;
  704. /* disable interrupt */
  705. level = rt_hw_local_irq_disable();
  706. current_thread = rt_cpu_self()->current_thread;
  707. if (!current_thread)
  708. {
  709. rt_hw_local_irq_enable(level);
  710. return;
  711. }
  712. rt_atomic_sub(&(current_thread->critical_lock_nest), 1);
  713. if (rt_atomic_load(&(current_thread->critical_lock_nest)) <= 0)
  714. {
  715. rt_atomic_store(&(current_thread->critical_lock_nest), 0);
  716. /* enable interrupt */
  717. rt_hw_local_irq_enable(level);
  718. rt_schedule();
  719. }
  720. else
  721. {
  722. /* enable interrupt */
  723. rt_hw_local_irq_enable(level);
  724. }
  725. }
  726. RTM_EXPORT(rt_exit_critical);
  727. /**
  728. * @brief Get the scheduler lock level.
  729. *
  730. * @return the level of the scheduler lock. 0 means unlocked.
  731. */
  732. rt_uint16_t rt_critical_level(void)
  733. {
  734. struct rt_thread *current_thread = rt_cpu_self()->current_thread;
  735. return rt_atomic_load(&(current_thread->critical_lock_nest));
  736. }
  737. RTM_EXPORT(rt_critical_level);
  738. /**@}*/
  739. /**@endcond*/