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
use std::{
    time::Duration,
    ffi::c_void,
    process::abort
};
use sync_wait_object::SignalWaitable;
use windows::Win32::{
    Foundation::{HANDLE, BOOLEAN, ERROR_IO_PENDING, WIN32_ERROR, GetLastError},
    System::Threading::*,
};
use super::timer::{CallbackHint, Result, DEFAULT_ACCEPTABLE_EXECUTION_TIME};
use crate::common::MutWrapper;
use super::TimerError;

pub(crate) use sync_wait_object::windows::ManualResetEvent;

// ------------------ DATA STRUCTURE -------------------------------
/// Wrapper of Windows Timer Queue API
///
/// Windows OS provides a default Timer Queue which can be retrieved by using [`TimerQueue::default`]. The default queue has `'static` lifetime, and can
/// be assigned as a constant.
///
/// ```rust
/// # use std::thread::sleep;
/// # use std::time::Duration;
/// # use native_timer::TimerQueue;
/// let mut count = 0;
/// let default_queue = TimerQueue::default();
/// let t = default_queue.schedule_timer(Duration::from_millis(100), Duration::default(), None, || count += 1);
/// sleep(Duration::from_millis(200));
/// drop(t);
/// assert_eq!(count, 1);
/// ```
pub struct TimerQueue {
    handle: HANDLE
}

/// Windows Timer
pub struct Timer<'q, 'h> {
    queue: &'q TimerQueue,
    handle: HANDLE,
    callback: Box<MutWrapper<'q,'h>>,
    acceptable_execution_time: Duration
}
// ----------------------------------------- FUNCTIONS ------------------------------------------------
#[inline]
pub(crate) fn get_win32_last_error() -> WIN32_ERROR {
    unsafe { GetLastError() }
}

#[inline]
pub(crate) fn get_last_error() -> TimerError {
    get_win32_last_error().into()
}

pub(crate) fn to_result(ret: bool) -> Result<()> {
    if ret { Ok(()) }
    else { Err(get_last_error()) }
}

// -------------------------------------- IMPLEMENTATIONS --------------------------------------------
static DEFAULT_QUEUE: TimerQueue = TimerQueue { handle: HANDLE(0) };

impl TimerQueue {
    /// Create a new TimerQueue
    pub fn new() -> Self {
        unsafe { Self { handle: CreateTimerQueue().unwrap() } }
    }

    /// Schedule a timer, either a one-shot timer, or a periodical timer.
    pub fn schedule_timer<'q, 'h, F>(&'q self, due: Duration, period: Duration, hints: Option<CallbackHint>, handler: F) -> Result<Timer<'q, 'h>>
        where F: FnMut() + Send + 'h
    {
        let period = period.as_millis() as u32;
        let (option, acceptable_execution_time) = hints.map(|o| match o {
            CallbackHint::QuickFunction => (WT_EXECUTEINPERSISTENTTHREAD, DEFAULT_ACCEPTABLE_EXECUTION_TIME),
            CallbackHint::SlowFunction(t) => (WT_EXECUTELONGFUNCTION, t)
        }).unwrap_or((WT_EXECUTEDEFAULT, DEFAULT_ACCEPTABLE_EXECUTION_TIME));
        let option = if period == 0 { option | WT_EXECUTEONLYONCE } else { option };

        let mut timer_handle = HANDLE::default();
        let callback = Box::new(MutWrapper::new(self, hints, handler));
        let callback_ref = callback.as_ref() as *const MutWrapper as *const c_void;

        let create_timer_queue_timer_result = unsafe {
            CreateTimerQueueTimer(&mut timer_handle, self.handle, Some(timer_callback), Some(callback_ref),
                                  due.as_millis() as u32, period, option).as_bool()
        };
        if create_timer_queue_timer_result {
            Ok(Timer::<'q,'h> { queue: self, handle: timer_handle, callback, acceptable_execution_time })
        } else {
            Err(get_last_error())
        }
    }

    /// Default OS common timer queue
    #[inline]
    pub fn default() -> &'static TimerQueue {
        &DEFAULT_QUEUE
    }
}

extern "system" fn timer_callback(ctx: *mut c_void, _: BOOLEAN) {
    let wrapper = unsafe { &mut *(ctx as *mut MutWrapper) };
    if let Err(e) = wrapper.call() {
        println!("WARNING: Error occurred during timer callback: {e:?}");
    }
}

impl Drop for TimerQueue {
    fn drop(&mut self) {
        if !self.handle.is_invalid() {
            assert!(unsafe { DeleteTimerQueue(self.handle).as_bool() });
            self.handle = HANDLE::default();
        }
    }
}

impl<'q,'h> Timer<'q,'h> {
    pub fn change_period(&self, due: Duration, period: Duration) -> Result<()> {
        to_result(unsafe { ChangeTimerQueueTimer(self.queue.handle, self.handle, due.as_millis() as u32, period.as_millis() as u32).as_bool() })
    }
}

impl<'q, 'h> Drop for Timer<'q, 'h> {
    fn drop(&mut self) {
        if !self.handle.is_invalid() {
            let mut is_deleted = self.callback.mark_deleted.write().unwrap();
            *is_deleted = true;

            // ensure no callback during destruction
            self.change_period(Duration::default(), Duration::default()).unwrap();

            if !self.callback.idling.wait(self.acceptable_execution_time).unwrap() {
                println!("ERROR: Wait for execution timed out! Timer handler is being executed while timer is also being destroyed! Program aborts!");
                abort();
            }

            let result = unsafe { DeleteTimerQueueTimer(self.queue.handle, self.handle, None).as_bool() };

            if !result {
                let e = get_win32_last_error();
                if e != ERROR_IO_PENDING {
                    println!("WARNING: Delete timer failed with error {e:?}. No retry attempt. Memory might leak!");
                }
            }
            self.handle = HANDLE::default();
        }
    }
}

impl From<WIN32_ERROR> for TimerError {
    fn from(value: WIN32_ERROR) -> Self {
        TimerError::OsError(value.0 as isize, value.to_hresult().message().to_string())
    }
}

// ------------------ TESTS -------------------------------
#[cfg(test)]
mod test {
    use std::{
        thread::sleep,
        time::Duration
    };
    use super::TimerQueue;

    #[test]
    fn test_singleshot(){
        let my_queue = TimerQueue::new();
        let mut called = 0;
        let timer = my_queue.schedule_timer(Duration::from_millis(400), Duration::ZERO, None, || called += 1).unwrap();
        sleep(Duration::from_secs(1));
        drop(timer);
        assert_eq!(called, 1);
    }

    #[test]
    fn test_period(){
        let my_queue = TimerQueue::new();
        let mut called = 0;
        let duration = Duration::from_millis(300);
        let t = my_queue.schedule_timer(duration, duration, None, || called += 1).unwrap();
        sleep(Duration::from_secs(1));
        drop(t);
        assert_eq!(called, 3);
    }
}