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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
///! Windows implementation of `ManualResetEvent` and `AutoResetEvent` which directly wraps over Win32 API.

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 };

// --------------------------------------- DATA STRUCTURE ---------------------------------------------
#[derive(Clone)]
pub struct WaitEvent(HANDLE);

/// Wrapper of [`WaitEvent`] of type `bool`, which focuses on waiting for `true` without resetting.
///
/// *Examples*
/// The example does not make sense (as [`sync_wait_object::WaitEvent`] of `u8` makes more sense for this scenario), it is just for demonstration.
///
/// ```rust
/// # use sync_wait_object::windows::ManualResetEvent;
/// use std::{ thread, sync::Arc, sync::atomic::{ AtomicU8, Ordering } };
/// use sync_wait_object::SignalWaitable;
///
/// let ev = ManualResetEvent::new();
/// let mut signal = ev.clone();
/// let v = Arc::new(AtomicU8::new(0));
/// let v_setter = v.clone();
///
/// thread::spawn(move || {
///     v_setter.store(1, Ordering::SeqCst);
///     signal.set().unwrap();
/// });
///
/// ev.wait_until_set().unwrap();
/// assert_eq!(v.load(Ordering::SeqCst), 1);
///
/// ```
#[derive(Clone)]
pub struct ManualResetEvent(WaitEvent);

/// Wrapper of [`WaitEvent`] of type `bool`, which focuses on waiting for `true` with automatic reset to `false`.
///
/// *Examples*
///
/// ```rust
/// # use sync_wait_object::windows::AutoResetEvent;
/// use std::{ thread, sync::Arc, sync::atomic::{ AtomicU8, Ordering } };
/// use sync_wait_object::SignalWaitable;
///
/// let ev = AutoResetEvent::new();
/// let mut signal = ev.clone();
///
/// let mut next = AutoResetEvent::new();
/// let wait_next = next.clone();
///
/// let v = Arc::new(AtomicU8::new(0));
/// let v_setter = v.clone();
///
/// thread::spawn(move || {
///     v_setter.store(1, Ordering::SeqCst);
///     signal.set().unwrap();
///
///     wait_next.wait_until_set().unwrap();
///
///     v_setter.store(2, Ordering::SeqCst);
///     signal.set().unwrap();
/// });
///
/// ev.wait_until_set().unwrap();
/// assert_eq!(v.load(Ordering::SeqCst), 1);
///
/// next.set().unwrap();
///
/// ev.wait_until_set().unwrap();
/// assert_eq!(v.load(Ordering::SeqCst), 2);
/// ```
#[derive(Clone)]
pub struct AutoResetEvent(WaitEvent);

// ---------------------------------------- FUNCTIONS -------------------------------------------------
#[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;
}

// ---------------------------------------- IMPLEMENTATIONS -------------------------------------------
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<bool> {
        let ret = unsafe { WaitForSingleObject(self.0, timeout) };
        match ret {
            WAIT_OBJECT_0 => Ok(true),
            WAIT_TIMEOUT => Ok(false),
            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<bool> {
        self.native_wait(INFINITE)
    }

    #[inline] fn wait(&self, timeout: Duration) -> Result<bool> {
        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 {
}