diff --git a/crates/domain/src/scheduler/multi_core.rs b/crates/domain/src/scheduler/multi_core.rs index 3174ac4..3ddfcff 100644 --- a/crates/domain/src/scheduler/multi_core.rs +++ b/crates/domain/src/scheduler/multi_core.rs @@ -37,18 +37,16 @@ impl MultiCoreScheduler { .count() } - fn least_loaded_core(&self) -> CoreId { + fn least_loaded_core(&self) -> Option { (0..self.num_cores) .map(|i| CoreId::from_raw(i)) .min_by_key(|&c| self.thread_count_on(c)) - .unwrap() } - fn most_loaded_core(&self) -> CoreId { + fn most_loaded_core(&self) -> Option { (0..self.num_cores) .map(|i| CoreId::from_raw(i)) .max_by_key(|&c| self.thread_count_on(c)) - .unwrap() } } @@ -77,9 +75,10 @@ impl Scheduler for MultiCoreScheduler { let core_idx = core.raw() as usize; if let Some(highest) = self.cores[core_idx].highest_waiting_priority() { if highest > current_priority { - let next = self.cores[core_idx].pick_next().unwrap(); - self.cores[core_idx].add(current, current_priority); - return SchedulerDecision::Preempt(next); + if let Some(next) = self.cores[core_idx].pick_next() { + self.cores[core_idx].add(current, current_priority); + return SchedulerDecision::Preempt(next); + } } } } @@ -87,8 +86,10 @@ impl Scheduler for MultiCoreScheduler { } fn balance(&mut self) { - let busiest = self.most_loaded_core(); - let lightest = self.least_loaded_core(); + let (busiest, lightest) = match (self.most_loaded_core(), self.least_loaded_core()) { + (Some(b), Some(l)) => (b, l), + _ => return, + }; if self.thread_count_on(busiest) > self.thread_count_on(lightest) + BALANCE_THRESHOLD { let busiest_idx = busiest.raw() as usize; diff --git a/crates/domain/src/scheduler/priority_queue.rs b/crates/domain/src/scheduler/priority_queue.rs index 9d5dc13..f9c345a 100644 --- a/crates/domain/src/scheduler/priority_queue.rs +++ b/crates/domain/src/scheduler/priority_queue.rs @@ -99,7 +99,7 @@ impl PriorityQueue { if self.queues[idx].is_empty() { self.bitmap &= !(1 << idx); } - Some((thread, Priority::new(idx as u8).unwrap())) + Some((thread, Priority::new(idx as u8)?)) } pub fn highest_priority(&self) -> Option { @@ -107,7 +107,7 @@ impl PriorityQueue { return None; } let idx = 31 - self.bitmap.leading_zeros() as usize; - Some(Priority::new(idx as u8).unwrap()) + Priority::new(idx as u8) } pub fn steal_lowest(&mut self) -> Option<(ThreadId, Priority)> { @@ -119,7 +119,7 @@ impl PriorityQueue { if self.queues[idx].is_empty() { self.bitmap &= !(1 << idx); } - Some((thread, Priority::new(idx as u8).unwrap())) + Some((thread, Priority::new(idx as u8)?)) } pub fn is_empty(&self) -> bool {