-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8518300
commit 9bb2080
Showing
8 changed files
with
158 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
pub use self::main::*; | ||
pub use self::vmm::*; | ||
|
||
mod main; | ||
mod vmm; | ||
|
||
/// Create a new channel to communicate with the VMM. | ||
pub fn create_channel() -> (VmmStream, MainStream) { | ||
// Create streams. | ||
let vmm = VmmStream::new(); | ||
let main = MainStream::new(); | ||
|
||
(vmm, main) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,106 @@ | ||
use crate::vmm::VmmEvent; | ||
use std::collections::{BTreeMap, VecDeque}; | ||
use std::future::Future; | ||
use std::num::NonZero; | ||
use std::pin::Pin; | ||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
use std::sync::{Condvar, Mutex}; | ||
use std::task::{Context, Poll, Waker}; | ||
|
||
/// Provides method to send and receive events from the VMM. | ||
pub struct VmmStream {} | ||
/// | ||
/// The main different from [`futures::channel::mpsc::channel()`] is our implementation will block | ||
/// the sender when the buffer is full. | ||
pub struct VmmStream<T> { | ||
max: NonZero<usize>, | ||
state: Mutex<State<T>>, | ||
cv: Condvar, | ||
} | ||
|
||
impl<T> VmmStream<T> { | ||
pub fn new(buffer: NonZero<usize>) -> Self { | ||
Self { | ||
max: buffer, | ||
state: Mutex::new(State { | ||
items: VecDeque::default(), | ||
wakers: BTreeMap::default(), | ||
}), | ||
cv: Condvar::default(), | ||
} | ||
} | ||
|
||
pub fn recv(&self) -> impl Future<Output = T> + '_ { | ||
Recv { | ||
stream: self, | ||
pending: None, | ||
} | ||
} | ||
|
||
pub fn send(&self, v: T) { | ||
// Wait for available room. | ||
let state = self.state.lock().unwrap(); | ||
let mut state = self | ||
.cv | ||
.wait_while(state, |s| s.items.len() >= self.max.get()) | ||
.unwrap(); | ||
|
||
impl VmmStream { | ||
pub(super) fn new() -> Self { | ||
Self {} | ||
// Store the value and wake one task. | ||
state.items.push_back(v); | ||
|
||
if let Some((_, w)) = state.wakers.pop_first() { | ||
w.wake(); | ||
} | ||
} | ||
} | ||
|
||
/// Implementation of [`Future`] to receive a value from [`VmmStream`]. | ||
struct Recv<'a, T> { | ||
stream: &'a VmmStream<T>, | ||
pending: Option<u64>, | ||
} | ||
|
||
impl<'a, T> Drop for Recv<'a, T> { | ||
fn drop(&mut self) { | ||
let id = match self.pending.take() { | ||
Some(v) => v, | ||
None => return, | ||
}; | ||
|
||
pub async fn recv(&mut self) -> Option<VmmEvent> { | ||
todo!() | ||
self.stream.state.lock().unwrap().wakers.remove(&id); | ||
} | ||
} | ||
|
||
impl<'a, T> Future for Recv<'a, T> { | ||
type Output = T; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
// Get item if available. | ||
let mut state = self.stream.state.lock().unwrap(); | ||
|
||
if let Some(v) = state.items.pop_front() { | ||
if let Some(id) = self.pending.take() { | ||
// The future may poll without a wakeup from a waker. | ||
state.wakers.remove(&id); | ||
} | ||
|
||
self.stream.cv.notify_one(); | ||
return Poll::Ready(v); | ||
} | ||
|
||
// Store waker. | ||
let id = self | ||
.pending | ||
.get_or_insert_with(|| WAKER_ID.fetch_add(1, Ordering::Relaxed)); | ||
|
||
state.wakers.insert(*id, cx.waker().clone()); | ||
|
||
Poll::Pending | ||
} | ||
} | ||
|
||
/// State of [`VmmStream`]. | ||
struct State<T> { | ||
items: VecDeque<T>, | ||
wakers: BTreeMap<u64, Waker>, | ||
} | ||
|
||
static WAKER_ID: AtomicU64 = AtomicU64::new(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.