Notify async tasks or threads
Find a file
Jason Volk b2c19bcaf5
Some checks failed
CI / test (beta) (push) Failing after 3s
CI / test (nightly) (push) Failing after 4s
CI / test (nightly, i686-unknown-linux-gnu) (push) Failing after 3s
CI / test (stable) (push) Failing after 3s
CI / msrv (push) Failing after 3s
CI / clippy (push) Failing after 10s
CI / loom (push) Failing after 9s
CI / miri (push) Successful in 4m12s
CI / fmt (push) Has been cancelled
CI / security_audit (push) Has been cancelled
feat: Add queuing strategies for listener lists.
Two strategies are available:
- FIFO: The original round-robin queuing; listeners are inserted at the back.
- LIFO: The new most-recent queuing; listeners are inserted at the front.

LIFO queuing is beneficial for cache-efficiency with workloads that are
tolerant of starvation. The same listener is repeatedly drawn from the list
until the load dictates additional listeners be drawn from the list. These
listeners expand outward as a "hot set" for optimal reuse of resources rather
than continuously drawing from the coldest resources in a FIFO schedule.

Signed-off-by: Jason Volk <jason@zemos.net>
2026-03-27 17:23:47 +00:00
.github Remove slab-based implementation of event-listener 2026-02-07 07:49:33 -08:00
benches chore: Polish the implementation of the new API 2024-02-03 09:49:22 -08:00
examples feat: Add critical-section based implementation 2024-12-04 08:17:27 -08:00
src feat: Add queuing strategies for listener lists. 2026-03-27 17:23:47 +00:00
tests m: Fix new nightly Clippy warnings 2024-05-19 11:18:16 -07:00
.gitignore Remove Cargo.lock 2020-05-16 19:46:27 +02:00
Cargo.toml Remove slab-based implementation of event-listener 2026-02-07 07:49:33 -08:00
CHANGELOG.md v5.4.1 2025-08-03 14:36:32 -07:00
LICENSE-APACHE Initial commit 2020-05-16 19:44:50 +02:00
LICENSE-MIT Initial commit 2020-05-16 19:44:50 +02:00
README.md Remove slab-based implementation of event-listener 2026-02-07 07:49:33 -08:00

event-listener

Build License Cargo Documentation

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();
}

Features

  • The std feature (enabled by default) enables the use of the Rust standard library. Disable it for no_std support.

  • The critical-section feature enables usage of the critical-section crate to enable a more efficient implementation of event-listener for no_std platforms.

  • The portable-atomic feature enables the use of the portable-atomic crate to provide atomic operations on platforms that don't support them.

In production environments, at least one of std or critical-section should be enabled. This ensures that the internal locking mechanism has a critical section of some kind to fall back on. Otherwise, it falls back to a spinlock implementation. This implementation is dangerous to rely on.

License

Licensed under either of

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.