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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! An abstraction over platform-specific event primitives:
//!
//! Windows: [event objects](https://learn.microsoft.com/en-us/windows/win32/sync/event-objects)
//! Linux: [eventfd](https://man7.org/linux/man-pages/man2/eventfd.2.html)
//! Other Unix: [fifo](https://man.openbsd.org/mkfifo.2)
#![warn(missing_docs)]
// UNSAFETY: FFI into platform-specific APIs.
#![allow(unsafe_code)]
mod unix;
mod windows;
#[cfg(unix)]
use unix as sys;
#[cfg(windows)]
use windows as sys;
/// A platform-specific synchronization event.
#[derive(Debug)]
pub struct Event(sys::Inner);
impl Event {
/// Creates a new event.
///
/// Panics if the event cannot be created. This should only be due to low
/// resources.
pub fn new() -> Self {
match Self::new_inner() {
Ok(event) => event,
Err(err) => panic!("failed to create event: {}", err),
}
}
/// Signals the event.
pub fn signal(&self) {
self.signal_inner();
}
/// Waits for the event to be signaled and consumes the signal.
pub fn wait(&self) {
self.wait_inner();
}
/// Tries to consume the event signal.
///
/// Returns `false` if the event is not currently signaled.
pub fn try_wait(&self) -> bool {
self.try_wait_inner()
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_event() {
let event = crate::Event::new();
event.signal();
event.wait();
}
}