#![allow(non_camel_case_types, non_snake_case)]
use crate::co;
use crate::decl::*;
use crate::guard::*;
use crate::kernel::{ffi, privs::*};
handle! { HEVENT;
}
impl HEVENT {
#[must_use]
pub fn CreateEvent(
security_attributes: Option<&SECURITY_ATTRIBUTES>,
manual_reset: bool,
initial_state: bool,
name: Option<&str>,
) -> SysResult<CloseHandleGuard<HEVENT>> {
unsafe {
PtrRet(ffi::CreateEventW(
pcvoid_or_null(security_attributes),
manual_reset as _,
initial_state as _,
WString::from_opt_str(name).as_ptr(),
))
.to_sysresult_handle()
.map(|h| CloseHandleGuard::new(h))
}
}
#[must_use]
pub fn CreateEventEx(
security_attributes: Option<&SECURITY_ATTRIBUTES>,
name: Option<&str>,
flags: co::CREATE_EVENT,
desired_access: co::EVENT_RIGHTS,
) -> SysResult<CloseHandleGuard<HEVENT>> {
unsafe {
PtrRet(ffi::CreateEventExW(
pcvoid_or_null(security_attributes),
WString::from_opt_str(name).as_ptr(),
flags.raw(),
desired_access.raw(),
))
.to_sysresult_handle()
.map(|h| CloseHandleGuard::new(h))
}
}
#[must_use]
pub fn OpenEvent(
&self,
desired_access: co::EVENT_RIGHTS,
inherit_handle: bool,
name: &str,
) -> SysResult<CloseHandleGuard<HEVENT>> {
unsafe {
PtrRet(ffi::OpenEventW(
desired_access.raw(),
inherit_handle as _,
WString::from_str(name).as_ptr(),
))
.to_sysresult_handle()
.map(|h| CloseHandleGuard::new(h))
}
}
pub fn PulseEvent(&self) -> SysResult<()> {
BoolRet(unsafe { ffi::PulseEvent(self.ptr()) }).to_sysresult()
}
pub fn ResetEvent(&self) -> SysResult<()> {
BoolRet(unsafe { ffi::ResetEvent(self.ptr()) }).to_sysresult()
}
pub fn SetEvent(&self) -> SysResult<()> {
BoolRet(unsafe { ffi::SetEvent(self.ptr()) }).to_sysresult()
}
pub fn WaitForSingleObject(&self, milliseconds: Option<u32>) -> SysResult<co::WAIT> {
match unsafe {
co::WAIT::from_raw(ffi::WaitForSingleObject(
self.ptr(),
milliseconds.unwrap_or(INFINITE),
))
} {
co::WAIT::FAILED => Err(GetLastError()),
wait => Ok(wait),
}
}
}