1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! A concurrent intrusive list for building synchronization
//! primitives.
//!
//! *Maillon is the French word for a chain link.*
//!
//! # Features
//!
//! - 100% safe API
//! - `#![no_std]`, no allocation
//! - Atomic emptiness check to avoid acquiring the mutex if the list is empty
//! - Lock-free insertion: multiple nodes can be inserted concurrently while another is being
//! removed; removal requires locking
//! - Optional atomic state embedded in the list when empty (to carry a semaphore counter, a closed
//! flag, etc.)
//! - [`WaitList`], a high-level asynchronous wait list with customizable synchronization built on
//! top of the low-level [`List`]
//!
//! # Usage
//!
//! [`WaitList`] is a ready-to-use asynchronous wait list, built on top of [`List`]:
//!
//! ```
//! use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
//!
//! use maillon::WaitList;
//!
//! #[derive(Default)]
//! pub struct Event {
//! done: AtomicBool,
//! wait_list: WaitList,
//! }
//!
//! impl Event {
//! pub async fn wait(&self) {
//! let _ = self.wait_list.wait_until(|_| self.done.load(Relaxed)).await;
//! }
//!
//! pub fn set(&self) {
//! self.done.store(true, Relaxed);
//! self.wait_list.notify_all();
//! }
//! }
//! ```
//!
//! [`List`] is the building block: [`Node`]s are pinned, carry user data implementing
//! [`NodeData`], and are pushed to the back without locking, while every other operation goes
//! through [`List::lock`]. Here is a minimal wait list, supporting only `notify_one`:
//!
//! ```
//! use std::{
//! future::Future,
//! mem,
//! pin::Pin,
//! sync::atomic::{
//! Ordering::{Relaxed, SeqCst},
//! fence,
//! },
//! task::{Context, Poll, Waker},
//! };
//!
//! use maillon::{List, Node, NodeData, NodeState, list::LockedList, node_wrapper};
//!
//! #[derive(Default)]
//! pub struct WaitList {
//! list: List<Waiter>,
//! }
//!
//! #[derive(Default)]
//! struct Waiter {
//! waker: Option<Waker>,
//! notified: bool,
//! }
//!
//! impl WaitList {
//! pub fn notify_one(&self) {
//! fence(SeqCst);
//! if !self.list.is_empty(Relaxed) {
//! Self::notify_one_cold(&self.list);
//! }
//! }
//!
//! #[cold]
//! fn notify_one_cold(list: &List<Waiter>) {
//! Self::notify_one_locked(list.lock());
//! }
//!
//! fn notify_one_locked(mut locked: LockedList<'_, Waiter>) {
//! let Some(mut front) = locked.front() else {
//! return;
//! };
//! front.notified = true;
//! let waker = front.waker.take();
//! front.unlink();
//! drop(locked);
//! if let Some(waker) = waker {
//! waker.wake();
//! }
//! }
//!
//! pub fn wait(&self) -> Wait<'_> {
//! Wait(Node::new(&self.list))
//! }
//! }
//!
//! node_wrapper! {
//! pub struct Wait<'a>(Node<&'a List<Waiter>>);
//! }
//!
//! impl Future for Wait<'_> {
//! type Output = ();
//!
//! fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
//! match self.node_mut().state() {
//! NodeState::Unlinked(mut node) => {
//! if mem::take(&mut node.notified) {
//! return Poll::Ready(());
//! }
//! node.waker = Some(cx.waker().clone());
//! node.push_back(Relaxed);
//! fence(SeqCst);
//! Poll::Pending
//! }
//! NodeState::Linked(mut node) => node.update_waker(cx, |node| &mut node.waker),
//! }
//! }
//! }
//!
//! impl NodeData<&List<Waiter>> for Waiter {
//! fn new_state_if_last_node_on_drop(
//! self: Pin<&mut Self>,
//! _list: &&List<Waiter>,
//! _list_data: &mut (),
//! ) {
//! }
//!
//! fn on_drop<'list>(
//! self: Pin<&mut Self>,
//! list: &'list &List<Waiter>,
//! locked: Option<LockedList<'list, Self>>,
//! _state_updated_on_unlink: bool,
//! ) {
//! if self.notified {
//! match locked {
//! Some(locked) => WaitList::notify_one_locked(locked),
//! None => WaitList::notify_one_cold(list),
//! }
//! }
//! }
//! }
//! ```
//!
//! See the [examples] for full implementations of `tokio::sync::Notify` and
//! `tokio::sync::Semaphore` built with `maillon`, with fully identical API and behavior.
//!
//! [examples]: https://github.com/wyfo/maillon/tree/main/examples
extern crate std;
pub use atomic_backoff as backoff;
pub use ;
pub use ;
pub use WaitList;
pub use WakerBatch;