parent
37a6df252f
commit
c8a9eaf3e6
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "ucore-process"
|
||||
version = "0.1.0"
|
||||
authors = ["WangRunji <wangrunji0408@163.com>"]
|
||||
|
||||
[dependencies]
|
||||
log = { git = "https://github.com/riscv-and-rust-and-decaf/log.git" }
|
@ -0,0 +1,61 @@
|
||||
use alloc::BinaryHeap;
|
||||
use core::cmp::{Ordering, PartialOrd};
|
||||
|
||||
type Time = usize;
|
||||
|
||||
struct Timer<T> {
|
||||
time: Time,
|
||||
data: T,
|
||||
}
|
||||
|
||||
impl<T> PartialEq for Timer<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.time.eq(&other.time)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Eq for Timer<T> {}
|
||||
|
||||
impl<T> PartialOrd for Timer<T> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
other.time.partial_cmp(&self.time)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Ord for Timer<T> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.partial_cmp(&other).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EventHub<T> {
|
||||
tick: Time,
|
||||
timers: BinaryHeap<Timer<T>>,
|
||||
}
|
||||
|
||||
impl<T> EventHub<T> {
|
||||
pub fn new() -> Self {
|
||||
EventHub {
|
||||
tick: 0,
|
||||
timers: BinaryHeap::new(),
|
||||
}
|
||||
}
|
||||
pub fn tick(&mut self) {
|
||||
self.tick += 1;
|
||||
}
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
match self.timers.peek() {
|
||||
None => return None,
|
||||
Some(timer) if timer.time != self.tick => return None,
|
||||
_ => {}
|
||||
};
|
||||
self.timers.pop().map(|t| t.data)
|
||||
}
|
||||
pub fn push(&mut self, time_after: Time, data: T) {
|
||||
let time = self.tick + time_after;
|
||||
self.timers.push(Timer { time, data });
|
||||
}
|
||||
pub fn get_time(&self) -> Time {
|
||||
self.tick
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
#![no_std]
|
||||
#![feature(alloc)]
|
||||
#![feature(const_fn)]
|
||||
#![feature(linkage)]
|
||||
#![feature(universal_impl_trait, conservative_impl_trait)]
|
||||
|
||||
extern crate alloc;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
// To use `println!` in test
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate std;
|
||||
|
||||
pub mod processor;
|
||||
pub mod scheduler;
|
||||
mod util;
|
||||
mod event_hub;
|
@ -0,0 +1,15 @@
|
||||
use core::fmt::Debug;
|
||||
|
||||
/// Get values by 2 diff keys at the same time
|
||||
pub trait GetMut2<Idx: Debug + Eq> {
|
||||
type Output;
|
||||
fn get_mut(&mut self, id: Idx) -> &mut Self::Output;
|
||||
fn get_mut2(&mut self, id1: Idx, id2: Idx) -> (&mut Self::Output, &mut Self::Output) {
|
||||
assert_ne!(id1, id2);
|
||||
let self1 = self as *mut Self;
|
||||
let self2 = self1;
|
||||
let p1 = unsafe { &mut *self1 }.get_mut(id1);
|
||||
let p2 = unsafe { &mut *self2 }.get_mut(id2);
|
||||
(p1, p2)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue