Go to file
John Nunley ea0c7c61c2
Incomplete fix
2023-07-16 21:45:35 -07:00
.github Minimize GITHUB_TOKEN permissions 2023-04-30 06:05:18 +09:00
benches Fix code review/CI issues 2023-03-31 13:58:16 -07:00
examples Add a type parameter T to Event 2023-04-03 07:37:26 -07:00
src Make sure Event/EventListener are Send/Sync (#59) 2023-05-22 18:28:02 -07:00
strategy Incomplete fix 2023-07-16 21:45:35 -07:00
tests Make the notify() function return notified count 2023-05-17 15:12:13 -07:00
.gitignore Remove Cargo.lock 2020-05-16 19:46:27 +02:00
CHANGELOG.md Release 2.5.3 2022-07-27 11:26:40 +09:00
Cargo.toml Fix various CI errors 2023-04-04 09:44:23 -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 Fix build badge and update links (#45) 2022-12-18 07:05:53 -08:00

README.md

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

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.