Files
k-os/crates/domain/src/scheduler/per_core.rs

53 lines
1.2 KiB
Rust

use super::priority_queue::PriorityQueue;
use types::bounded::Priority;
use types::id::{CoreId, ThreadId};
pub struct PerCoreScheduler {
core: CoreId,
queue: PriorityQueue,
current: Option<ThreadId>,
}
impl PerCoreScheduler {
pub fn new(core: CoreId) -> Self {
Self {
core,
queue: PriorityQueue::new(),
current: None,
}
}
pub fn core(&self) -> CoreId {
self.core
}
pub fn current(&self) -> Option<ThreadId> {
self.current
}
pub fn add(&mut self, thread: ThreadId, priority: Priority) {
self.queue.add(thread, priority);
}
pub fn remove(&mut self, thread: ThreadId, priority: Priority) {
self.queue.remove(thread, priority);
}
pub fn pick_next(&mut self) -> Option<ThreadId> {
let (thread, _priority) = self.queue.next()?;
self.current = Some(thread);
Some(thread)
}
pub fn clear_current(&mut self) {
self.current = None;
}
pub fn len(&self) -> usize {
// for load balancing — count of ready threads
// could track this incrementally instead of computing
// but for now, bitmap popcount works
self.queue.bitmap().count_ones() as usize
}
}