Expand description
Notify async tasks or threads.
This is a synchronization primitive similar to eventcounts invented by Dmitry Vyukov.
You can use this crate to turn non-blocking data structures into async or blocking data structures. See a simple mutex implementation that exposes an async and a blocking interface for acquiring locks.
Β§Examples
Wait until another thread sets a boolean flag:
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::usize;
use event_listener::{Event, Listener};
let flag = Arc::new(AtomicBool::new(false));
let event = Arc::new(Event::new());
// Spawn a thread that will set the flag after 1 second.
thread::spawn({
let flag = flag.clone();
let event = event.clone();
move || {
// Wait for a second.
thread::sleep(Duration::from_secs(1));
// Set the flag.
flag.store(true, Ordering::SeqCst);
// Notify all listeners that the flag has been set.
event.notify(usize::MAX);
}
});
// Wait until the flag is set.
loop {
// Check the flag.
if flag.load(Ordering::SeqCst) {
break;
}
// Start listening for events.
let mut listener = event.listen();
// Check the flag again after creating the listener.
if flag.load(Ordering::SeqCst) {
break;
}
// Wait for a notification and continue the loop.
listener.wait();
}Β§Features
-
The
stdfeature (enabled by default) enables the use of the Rust standard library. Disable it forno_stdsupport. -
The
critical-sectionfeature enables usage of thecritical-sectioncrate to enable a more efficient implementation ofevent-listenerforno_stdplatforms. -
The
portable-atomicfeature enables the use of theportable-atomiccrate to provide atomic operations on platforms that donβt support them.
ModulesΒ§
- notify π
- The
Notificationtrait for specifying notification. - sync π
- Synchronization primitive implementation.
- sys π
- Intrusive linked list-based implementation of
event-listener.
MacrosΒ§
- forward_
impl_ πto_ listener - Implement the
Listenertrait using the underlyingInnerListener. - listener
- Create a stack-based event listener for an
Event.
StructsΒ§
- Event
- A synchronization primitive for notifying async tasks and threads.
- Event
Listener - A guard waiting for a notification from an
Event. - Inner π
- Inner state of
Event. - Inner
Listener π
EnumsΒ§
- Register
Result π - The result of registering a listener.
- State π
- The state of a listener.
- Task π
- A task that can be woken up.
- TaskRef π
- A reference to a task.
ConstantsΒ§
- NEVER_
INSERTED_ πPANIC
TraitsΒ§
- Into
Notification - A value that can be converted into a
Notification. - Listener
- A handle that is listening to an
Event. - Notification
- A notification that can be used to notify an
Event.
FunctionsΒ§
- __
test_ πsend_ and_ sync