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
/// Abstraction which provides a method of blocking and unblocking threads
/// through the interface of a manual reset event.
pub trait ThreadEvent: Sync + Default {
    /// Approximately check if the event is set or not.
    fn is_set(&self) -> bool;

    /// Reset the event back to an un-signalled state.
    fn reset(&self);

    /// Block for the event to become signaled.
    /// If the event is already signaled, this returns without blocking.
    fn wait(&self);

    /// Transition the event to a signaled state,
    /// unblocking any threads that were waiting on it.
    fn set(&self);
}

#[cfg(all(feature = "os", windows))]
mod windows;
#[cfg(all(feature = "os", windows))]
use windows::Event as SystemThreadEvent;

#[cfg(all(feature = "os", target_os = "linux"))]
mod linux;
#[cfg(all(feature = "os", target_os = "linux"))]
use linux::Event as SystemThreadEvent;

#[cfg(all(feature = "os", unix, not(target_os = "linux")))]
mod posix;
#[cfg(all(feature = "os", unix, not(target_os = "linux")))]
use posix::Event as SystemThreadEvent;

#[cfg(feature = "os")]
pub use self::if_os::*;
#[cfg(feature = "os")]
mod if_os {
    use super::{SystemThreadEvent, ThreadEvent};
    use core::fmt;

    /// The default ThreadEvent which uses the platform's blocking primitives.
    #[cfg_attr(feature = "nightly", doc(cfg(feature = "os")))]
    #[derive(Default)]
    pub struct OsThreadEvent(SystemThreadEvent);

    unsafe impl Sync for OsThreadEvent {}
    unsafe impl Send for OsThreadEvent {}

    impl fmt::Debug for OsThreadEvent {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_struct("OsThreadEvent")
                .field("is_set", &self.0.is_set())
                .finish()
        }
    }

    impl ThreadEvent for OsThreadEvent {
        fn is_set(&self) -> bool {
            self.0.is_set()
        }

        fn reset(&self) {
            self.0.reset()
        }

        fn set(&self) {
            self.0.set()
        }

        fn wait(&self) {
            self.0.wait()
        }
    }
}