Crate event_listener

Crate event_listener 

Source
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 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.

ModulesΒ§

notify πŸ”’
The Notification trait for specifying notification.
sync πŸ”’
Synchronization primitive implementation.
sys πŸ”’
Intrusive linked list-based implementation of event-listener.

MacrosΒ§

forward_impl_to_listener πŸ”’
Implement the Listener trait using the underlying InnerListener.
listener
Create a stack-based event listener for an Event.

StructsΒ§

Event
A synchronization primitive for notifying async tasks and threads.
EventListener
A guard waiting for a notification from an Event.
Inner πŸ”’
Inner state of Event.
InnerListener πŸ”’

EnumsΒ§

RegisterResult πŸ”’
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Β§

IntoNotification
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 πŸ”’