use std::io;
use crate::{
driver::{post_driver_raw, Overlapped, RawFd},
key::Key,
task::{op::OpFuture, RUNTIME},
};
#[derive(Debug)]
pub struct Event {
user_data: Key<()>,
}
impl Event {
pub fn new() -> io::Result<Self> {
let user_data = RUNTIME.with(|runtime| runtime.submit_dummy());
Ok(Self { user_data })
}
pub fn handle(&self) -> io::Result<EventHandle> {
Ok(EventHandle::new(&self.user_data))
}
pub async fn wait(&self) -> io::Result<()> {
let future = OpFuture::new(self.user_data);
future.await?;
Ok(())
}
}
pub struct EventHandle {
handle: RawFd,
overlapped: Overlapped,
}
unsafe impl Send for EventHandle {}
unsafe impl Sync for EventHandle {}
impl EventHandle {
pub(crate) fn new(user_data: &Key<()>) -> Self {
let handle = RUNTIME.with(|runtime| runtime.raw_driver());
let overlapped = Overlapped::new(**user_data);
Self { handle, overlapped }
}
pub fn notify(&mut self) -> io::Result<()> {
unsafe { post_driver_raw(self.handle, Ok(0), &mut self.overlapped.base) }
}
}