zircon 實作兩種調度機制,一種就是fair 其實作在fair_scheduler.cpp中,一種是基于時間片的其實作在sched.cpp 中,調度器的入口都在sche_reschedule()這個函式中,
例如fair的實作如下:
void sched_reschedule() {
FairScheduler::Reschedule();
}
fair的實作是一個cpp的類,
另一中sche_reschedule()的實作在sched.cpp 中,我們簡單看下
void sched_reschedule() {
current_thread->state = THREAD_READY;
// idle thread doesn't go in the run queue
if (likely(!thread_is_idle(current_thread))) {
#可見會首先判斷當前執行緒的時間片是否用盡,用盡的話,則加入到當前cpu 運行佇列的末尾,否則就插入到head,這樣下次呼叫這個函式的時候就會
優先呼叫這個函式
if (current_thread->remaining_time_slice > 0) {
insert_in_run_queue_head(curr_cpu, current_thread);
} else {
insert_in_run_queue_tail(curr_cpu, current_thread);
}
}
sched_resched_internal();
}
void sched_resched_internal() {
thread_t* current_thread = get_current_thread();
uint cpu = arch_curr_cpu_num();
CPU_STATS_INC(reschedules);
// pick a new thread to run
#從當前cpu 挑選一個thread 來運行,類似于linux的RR調度
thread_t* newthread = sched_get_top_thread(cpu);
DEBUG_ASSERT(newthread);
newthread->state = THREAD_RUNNING;
thread_t* oldthread = current_thread;
oldthread->preempt_pending = false;
#計算久行程的時間記賬
zx_time_t now = current_time();
// account for time used on the old thread
DEBUG_ASSERT(now >= oldthread->last_started_running);
zx_duration_t old_runtime = zx_time_sub_time(now, oldthread->last_started_running);
oldthread->runtime_ns = zx_duration_add_duration(oldthread->runtime_ns, old_runtime);
oldthread->remaining_time_slice = zx_duration_sub_duration(
oldthread->remaining_time_slice, MIN(old_runtime, oldthread->remaining_time_slice));
// set up quantum for the new thread if it was consumed
if (newthread->remaining_time_slice == 0) {
newthread->remaining_time_slice = THREAD_INITIAL_TIME_SLICE;
}
newthread->last_started_running = now;
#切換mmu
// see if we need to swap mmu context
if (newthread->aspace != oldthread->aspace) {
vmm_context_switch(oldthread->aspace, newthread->aspace);
}
#行程切換
// do the low level context switch
final_context_switch(oldthread, newthread);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/18955.html
標籤:其他
上一篇:Timer定時器用法詳解
下一篇:回圈與分支——python
