use std::{
time::Duration,
ops::{ Deref, DerefMut }
};
use windows::Win32::{
Foundation::{ HANDLE, CloseHandle, GetLastError, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_FAILED, WIN32_ERROR },
System::Threading::{ CreateEventA, WaitForSingleObject, ResetEvent, SetEvent },
System::WindowsProgramming::INFINITE
};
use crate::{ WaitObjectError, Result, SignalWaitable };
#[derive(Clone)]
pub struct WaitEvent(HANDLE);
#[derive(Clone)]
pub struct ManualResetEvent(WaitEvent);
#[derive(Clone)]
pub struct AutoResetEvent(WaitEvent);
#[inline]
pub(crate) fn get_win32_last_error() -> WIN32_ERROR {
unsafe { GetLastError() }
}
#[inline]
pub(crate) fn get_last_error() -> WaitObjectError {
get_win32_last_error().into()
}
pub(crate) fn to_result(ret: bool) -> Result<()> {
if ret { Ok(()) }
else { Err(get_last_error()) }
}
pub trait HandleWrapper {
fn handle(&self) -> HANDLE;
}
impl From<WIN32_ERROR> for WaitObjectError {
fn from(value: WIN32_ERROR) -> Self {
WaitObjectError::OsError(value.0 as isize, value.to_hresult().message().to_string())
}
}
impl WaitEvent {
fn native_wait(&self, timeout: u32) -> Result<()> {
let ret = unsafe { WaitForSingleObject(self.0, timeout) };
match ret {
WAIT_OBJECT_0 => Ok(()),
WAIT_TIMEOUT => Err(WaitObjectError::Timeout),
WAIT_FAILED => Err(get_last_error()),
_ => unreachable!()
}
}
}
impl HandleWrapper for WaitEvent {
#[inline]
fn handle(&self) -> HANDLE { self.0 }
}
impl SignalWaitable for WaitEvent {
#[inline]
fn wait_until_set(&self) -> Result<()> {
self.native_wait(INFINITE)
}
#[inline] fn wait(&self, timeout: Duration) -> Result<()> {
self.native_wait(timeout.as_millis() as u32)
}
fn set(&mut self) -> Result<()> {
to_result(unsafe { SetEvent(self.0).as_bool() })
}
fn reset(&mut self) -> Result<()> {
to_result(unsafe { ResetEvent(self.0).as_bool() })
}
}
impl Drop for WaitEvent {
fn drop(&mut self) {
if !self.0.is_invalid() {
unsafe { CloseHandle(self.0); }
self.0 = HANDLE::default();
}
}
}
impl ManualResetEvent {
#[inline]
pub fn new() -> Self { Self::new_init(false) }
pub fn new_init(initial_state: bool) -> Self {
let handle = unsafe { CreateEventA(None, true, initial_state, None).unwrap() };
Self(WaitEvent(handle))
}
}
impl Deref for ManualResetEvent {
type Target = WaitEvent;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ManualResetEvent {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AutoResetEvent {
#[inline]
pub fn new() -> Self { Self::new_init(false) }
pub fn new_init(initial_state: bool) -> Self {
let handle = unsafe { CreateEventA(None, false, initial_state, None).unwrap() };
Self(WaitEvent(handle))
}
}
impl Deref for AutoResetEvent {
type Target = WaitEvent;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for AutoResetEvent {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[cfg(test)]
mod test {
}