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
use std::sync::RwLock;
use sync_wait_object::SignalWaitable;
use crate::{
Result, platform, platform::ManualResetEvent, CallbackHint
};
#[allow(dead_code)]
pub(crate) struct MutWrapper<'q, 'h> {
pub main_queue: &'q platform::TimerQueue,
pub hints: Option<CallbackHint>,
f: Box<dyn FnMut() + 'h>,
pub idling: ManualResetEvent,
pub mark_deleted: RwLock<bool>
}
struct CriticalSection<'e> {
idling: &'e mut ManualResetEvent
}
impl<'q,'h> MutWrapper<'q,'h> {
pub fn new<F>(main_queue: &'q platform::TimerQueue, hints: Option<CallbackHint>, handler: F) -> Self where F: FnMut() + Send + 'h {
MutWrapper::<'q,'h> {
main_queue,
hints,
f: Box::new(handler),
idling: ManualResetEvent::new_init(true),
mark_deleted: RwLock::new(false)
}
}
pub fn call(&mut self) -> Result<()> {
let is_deleted = self.mark_deleted.read().unwrap();
if !*is_deleted {
let _ = &CriticalSection::new(&mut self.idling); (self.f)();
}
Ok(())
}
}
impl<'e> CriticalSection<'e> {
fn new(idling: &'e mut ManualResetEvent) -> Self {
idling.reset().unwrap();
Self { idling }
}
}
impl<'e> Drop for CriticalSection<'e> {
fn drop(&mut self) {
self.idling.set().unwrap();
}
}