scheduler_mp.c 27 KB

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