* @brief Start the system scheduler and switch to the highest priority thread. * * @note This function will not return after successful execution. * It performs the initial thread switch during system startup. * The scheduler must be initialized before calling this function. * * @note This function has both MP version and UP version. */ * Don't include below in doxygen document
| 492 | * - Performs context switch to the selected thread |
| 493 | */ |
| 494 | void rt_system_scheduler_start(void) |
| 495 | { |
| 496 | struct rt_thread *to_thread; |
| 497 | rt_ubase_t highest_ready_priority; |
| 498 | |
| 499 | /* |
| 500 | * legacy rt_cpus_lock. some bsp codes still use it as for it's critical |
| 501 | * region. Since scheduler is never touching this, here we just release it |
| 502 | * on the entry. |
| 503 | */ |
| 504 | rt_hw_spin_unlock(&_cpus_lock); |
| 505 | |
| 506 | /* ISR will corrupt the coherency of running frame */ |
| 507 | rt_hw_local_irq_disable(); |
| 508 | |
| 509 | /* |
| 510 | * for the accessing of the scheduler context. Noted that we don't have |
| 511 | * current_thread at this point |
| 512 | */ |
| 513 | _fast_spin_lock(&_mp_scheduler_lock); |
| 514 | |
| 515 | /* get the thread scheduling to */ |
| 516 | to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority); |
| 517 | RT_ASSERT(to_thread); |
| 518 | |
| 519 | /* to_thread is picked to running on current core, so remove it from ready queue */ |
| 520 | _sched_remove_thread_locked(to_thread); |
| 521 | |
| 522 | /* dedigate current core to `to_thread` */ |
| 523 | RT_SCHED_CTX(to_thread).oncpu = rt_hw_cpu_id(); |
| 524 | RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; |
| 525 | |
| 526 | LOG_D("[cpu#%d] switch to priority#%d thread:%.*s(sp:0x%08x)", |
| 527 | rt_hw_cpu_id(), RT_SCHED_PRIV(to_thread).current_priority, |
| 528 | RT_NAME_MAX, to_thread->parent.name, to_thread->sp); |
| 529 | |
| 530 | _fast_spin_unlock(&_mp_scheduler_lock); |
| 531 | |
| 532 | /* switch to new thread */ |
| 533 | rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp, to_thread); |
| 534 | |
| 535 | /* never come back */ |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * @brief This function will handle IPI interrupt and do a scheduling in system. |
nothing calls this directly
no test coverage detected