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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! A barrier that tasks wait at until enough of them have arrived.
//!
//! [`Barrier`] makes a number of tasks wait for each other and releases them together. The
//! [module documentation](super) describes what all the primitives of this module promise.
use ;
use crateEvent;
/// A barrier that makes tasks wait for each other, without blocking the thread.
///
/// A barrier is made for `n` tasks. Each task that calls [`wait`](Barrier::wait) waits until the
/// `n`-th task arrives. Then all `n` tasks are released together. This is one round, and the
/// barrier is ready for the next round at once, so a single barrier serves tasks that meet over and
/// over.
///
/// Each round has one leader: the task whose arrival completes the round. It is not held up, and
/// its [`BarrierWaitResult`] says so, so that one task can do work that only one task should do
/// after the meeting.
///
/// A barrier made for zero tasks behaves like one made for one task, as with
/// [`std::sync::Barrier`]: every `wait` completes at once, and the task is the leader of its own
/// round.
///
/// The barrier needs no runtime and works under any executor. It is `Send` and `Sync`, and so are
/// the futures that wait at it. A task that panics instead of arriving leaves the others waiting
/// for it, and nothing is poisoned. A task that gives up its wait is no longer counted: see
/// [giving up a wait](crate::lock#giving-up-a-wait).
///
/// # Example
///
/// Three threads wait for each other. Exactly one of them is the leader:
///
/// ```
/// use std::{sync::Arc, thread};
///
/// use futures::executor::block_on;
/// use zruntime::lock::Barrier;
///
/// let barrier = Arc::new(Barrier::new(3));
/// let threads: Vec<_> = (0..3)
/// .map(|_| {
/// let barrier = barrier.clone();
/// thread::spawn(move || block_on(barrier.wait()).is_leader())
/// })
/// .collect();
///
/// let leaders = threads
/// .into_iter()
/// .map(|thread| thread.join().expect("the other thread did not panic"))
/// .filter(|&leader| leader)
/// .count();
///
/// assert_eq!(leaders, 1);
/// ```
/// The result of waiting at a [`Barrier`]: whether the task led its round.
///
/// Created by [`Barrier::wait`].
/// What every `wait` decides on.
///
/// The blocking mutex around it is held for the few instructions that check and update it, never
/// across an await.
/// A task that waits at a [`Barrier`], counted in the round it arrived in for as long as its `wait`
/// future lives.
///
/// A round that is complete is over for everyone in it, and counts no one any more, so the count
/// of a task is taken back only where its round is still the current one: a `wait` dropped before
/// its round was completed leaves the barrier needing the arrival it had made, and one dropped
/// after leaves the next round's count alone.
/// The state behind the barrier, taken whether or not a panic poisoned it.
///
/// Poisoning says nothing here: no path can panic part of the way through its changes to the
/// state, so a panic elsewhere cannot leave it half-updated.