blob: 202c96524d28e857eed98a8a3f78a4a83c1c4f74 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
use std::{io, ptr};
use windows_sys::Win32::{
Foundation::{CloseHandle, BOOL, HANDLE},
System::Threading::{CreateEventW, SetEvent},
};
/// Windows event object
pub struct Event(HANDLE);
unsafe impl Send for Event {}
unsafe impl Sync for Event {}
impl Event {
/// Create a new event object using `CreateEventW`
pub fn new(manual_reset: bool, initial_state: bool) -> io::Result<Self> {
let event = unsafe {
CreateEventW(
ptr::null_mut(),
bool_to_winbool(manual_reset),
bool_to_winbool(initial_state),
ptr::null(),
)
};
if event == 0 {
return Err(io::Error::last_os_error());
}
Ok(Self(event))
}
/// Signal the event object
pub fn set(&self) -> io::Result<()> {
if unsafe { SetEvent(self.0) } == 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
/// Return raw event object
pub fn as_raw(&self) -> HANDLE {
self.0
}
}
impl Drop for Event {
fn drop(&mut self) {
unsafe { CloseHandle(self.0) };
}
}
const fn bool_to_winbool(val: bool) -> BOOL {
match val {
true => 1,
false => 0,
}
}
|