event-listener (5.4.1)
Published 2026-03-26 10:57:49 +00:00 by siennathesane
Installation
[registry]
default = "gitea"
[registries.gitea]
index = "sparse+ " # Sparse index
# index = " " # Git
[net]
git-fetch-with-cli = truecargo add event-listener@5.4.1About this package
Notify async tasks or threads
event-listener
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 event_listener::Event;
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 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();
}
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
| ID | Version |
|---|---|
| concurrent-queue | ^2.4.0 |
| critical-section | ^1.2.0 |
| pin-project-lite | ^0.2.12 |
| portable-atomic-util | ^0.2.0 |
| portable_atomic_crate | ^1.2.0 |
| criterion | ^0.7 |
| critical-section | ^1.2.0 |
| futures-lite | ^2.0.0 |
| try-lock | ^0.2.5 |
| waker-fn | ^1 |
| loom | ^0.7 |
| parking | ^2.0.0 |
| wasm-bindgen-test | ^0.3 |
Keywords
condvar
eventcount
wake
blocking
park
Details
2026-03-26 10:57:49 +00:00
Assets (1)
Versions (1)
View all
Cargo
0
Stjepan Glavina <stjepang@gmail.com>
John Nunley <dev@notgull.net>
Apache-2.0 OR MIT
42 KiB
event-listener-5.4.1.crate
42 KiB
5.4.1
2026-03-26