Debian Codegraph @

README.md

raw symbols SCIP
1 # event-listener
2
3 [![Build](https://github.com/smol-rs/event-listener/workflows/CI/badge.svg)](
4 https://github.com/smol-rs/event-listener/actions)
5 [![License](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg)](
6 https://github.com/smol-rs/event-listener)
7 [![Cargo](https://img.shields.io/crates/v/event-listener.svg)](
8 https://crates.io/crates/event-listener)
9 [![Documentation](https://docs.rs/event-listener/badge.svg)](
10 https://docs.rs/event-listener)
11
12 Notify async tasks or threads.
13
14 This is a synchronization primitive similar to [eventcounts] invented by Dmitry Vyukov.
15
16 You can use this crate to turn non-blocking data structures into async or blocking data
17 structures. See a [simple mutex] implementation that exposes an async and a blocking interface
18 for acquiring locks.
19
20 [eventcounts]: https://www.1024cores.net/home/lock-free-algorithms/eventcounts
21 [simple mutex]: ./examples/mutex.rs
22
23 ## Examples
24
25 Wait until another thread sets a boolean flag:
26
27 ```rust
28 use std::sync::atomic::{AtomicBool, Ordering};
29 use std::sync::Arc;
30 use std::thread;
31 use std::time::Duration;
32 use event_listener::Event;
33
34 let flag = Arc::new(AtomicBool::new(false));
35 let event = Arc::new(Event::new());
36
37 // Spawn a thread that will set the flag after 1 second.
38 thread::spawn({
39 let flag = flag.clone();
40 let event = event.clone();
41 move || {
42 // Wait for a second.
43 thread::sleep(Duration::from_secs(1));
44
45 // Set the flag.
46 flag.store(true, Ordering::SeqCst);
47
48 // Notify all listeners that the flag has been set.
49 event.notify(usize::MAX);
50 }
51 });
52
53 // Wait until the flag is set.
54 loop {
55 // Check the flag.
56 if flag.load(Ordering::SeqCst) {
57 break;
58 }
59
60 // Start listening for events.
61 let listener = event.listen();
62
63 // Check the flag again after creating the listener.
64 if flag.load(Ordering::SeqCst) {
65 break;
66 }
67
68 // Wait for a notification and continue the loop.
69 listener.wait();
70 }
71 ```
72
73 ## Features
74
75 - The `std` feature (enabled by default) enables the use of the Rust standard library. Disable it
76 for `no_std` support.
77
78 - The `critical-section` feature enables usage of the [`critical-section`] crate to enable
79 a more efficient implementation of `event-listener` for `no_std` platforms.
80
81 - The `portable-atomic` feature enables the use of the [`portable-atomic`] crate to provide
82 atomic operations on platforms that don't support them.
83
84 In production environments, at least one of `std` or `critical-section` should be
85 enabled. This ensures that the internal locking mechanism has a critical section of some
86 kind to fall back on. Otherwise, it falls back to a spinlock implementation. This
87 implementation is [dangerous] to rely on.
88
89 [`critical-section`]: https://crates.io/crates/critical-section
90 [`portable-atomic`]: https://crates.io/crates/portable-atomic
91 [dangerous]: https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html
92
93 ## License
94
95 Licensed under either of
96
97 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
98 * MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
99
100 at your option.
101
102 #### Contribution
103
104 Unless you explicitly state otherwise, any contribution intentionally submitted
105 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
106 dual licensed as above, without any additional terms or conditions.