yaar_lock/thread_event/
mod.rs

1/// Abstraction which provides a method of blocking and unblocking threads
2/// through the interface of a manual reset event.
3pub trait ThreadEvent: Sync + Default {
4    /// Approximately check if the event is set or not.
5    fn is_set(&self) -> bool;
6
7    /// Reset the event back to an un-signalled state.
8    fn reset(&self);
9
10    /// Block for the event to become signaled.
11    /// If the event is already signaled, this returns without blocking.
12    fn wait(&self);
13
14    /// Transition the event to a signaled state,
15    /// unblocking any threads that were waiting on it.
16    fn set(&self);
17}
18
19#[cfg(all(feature = "os", windows))]
20mod windows;
21#[cfg(all(feature = "os", windows))]
22use windows::Event as SystemThreadEvent;
23
24#[cfg(all(feature = "os", target_os = "linux"))]
25mod linux;
26#[cfg(all(feature = "os", target_os = "linux"))]
27use linux::Event as SystemThreadEvent;
28
29#[cfg(all(feature = "os", unix, not(target_os = "linux")))]
30mod posix;
31#[cfg(all(feature = "os", unix, not(target_os = "linux")))]
32use posix::Event as SystemThreadEvent;
33
34#[cfg(feature = "os")]
35pub use self::if_os::*;
36#[cfg(feature = "os")]
37mod if_os {
38    use super::{SystemThreadEvent, ThreadEvent};
39    use core::fmt;
40
41    /// The default ThreadEvent which uses the platform's blocking primitives.
42    #[cfg_attr(feature = "nightly", doc(cfg(feature = "os")))]
43    #[derive(Default)]
44    pub struct OsThreadEvent(SystemThreadEvent);
45
46    unsafe impl Sync for OsThreadEvent {}
47    unsafe impl Send for OsThreadEvent {}
48
49    impl fmt::Debug for OsThreadEvent {
50        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51            f.debug_struct("OsThreadEvent")
52                .field("is_set", &self.0.is_set())
53                .finish()
54        }
55    }
56
57    impl ThreadEvent for OsThreadEvent {
58        fn is_set(&self) -> bool {
59            self.0.is_set()
60        }
61
62        fn reset(&self) {
63            self.0.reset()
64        }
65
66        fn set(&self) {
67            self.0.set()
68        }
69
70        fn wait(&self) {
71            self.0.wait()
72        }
73    }
74}