Scheduler
This commit is contained in:
@@ -1,14 +1 @@
|
|||||||
pub fn add(left: u64, right: u64) -> u64 {
|
|
||||||
left + right
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let result = add(2, 2);
|
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
use std::{cmp::Ordering, collections::BinaryHeap};
|
||||||
|
|
||||||
|
#[derive(Copy, Debug, Clone)]
|
||||||
|
pub enum EventType {
|
||||||
|
Pause,
|
||||||
|
Stop,
|
||||||
|
Reset,
|
||||||
|
PIBusWriteComplete,
|
||||||
|
PIDMAComplete,
|
||||||
|
SIDMAComplete,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Debug, Clone)]
|
||||||
|
pub struct Event {
|
||||||
|
time: u64,
|
||||||
|
event_type: EventType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Event {}
|
||||||
|
|
||||||
|
impl PartialEq for Event {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.time == other.time
|
||||||
|
&& matches!(
|
||||||
|
(self.event_type, other.event_type),
|
||||||
|
(EventType::Pause, EventType::Pause)
|
||||||
|
| (EventType::Stop, EventType::Stop)
|
||||||
|
| (EventType::Reset, EventType::Reset)
|
||||||
|
| (EventType::PIBusWriteComplete, EventType::PIBusWriteComplete)
|
||||||
|
| (EventType::PIDMAComplete, EventType::PIDMAComplete)
|
||||||
|
| (EventType::SIDMAComplete, EventType::SIDMAComplete)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for Event {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
self.time.cmp(&other.time).reverse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for Event {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Scheduler {
|
||||||
|
events: BinaryHeap<Event>,
|
||||||
|
ticks: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scheduler {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
events: BinaryHeap::new(),
|
||||||
|
ticks: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enqueue_relative(&mut self, time: u64, event_type: EventType) {
|
||||||
|
self.enqueue_absolute(time + self.ticks, event_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enqueue_absolute(&mut self, time: u64, event_type: EventType) {
|
||||||
|
self.events.push(Event { time, event_type })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(&mut self) -> Option<Event> {
|
||||||
|
self.events.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tick(&mut self, time: u64) {
|
||||||
|
self.ticks += time
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-7
@@ -1,7 +1 @@
|
|||||||
#![allow(unused_imports)]
|
fn main() {}
|
||||||
|
|
||||||
use backend::add;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
println!("Hello, world!");
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user