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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use ;
use ;
use cratemacros;
/// A latch is a downward counter which can be used to synchronize threads. The
/// value of the counter is initialized on creation. Threads may block on the
/// latch until the counter is decremented to 0.
///
/// In contrast to [`Barrier`], it is a one-shot phenomenon, that mean the
/// counter will not be reset after reaching 0. However, it has a useful
/// property in that it does not make threads wait for the counter to reach 0
/// by calling [`count_down()`] or [`arrive()`].
///
/// It spins before each futex wait.
///
/// It is based on atomic futex, so:
///
/// - It doesn't support timeout for wait. Because MacOS doesn't yet have a
/// stable ABI for futex wait timeout.
/// - It only supports `u32` because the futex support only 32-bits. Of course,
/// we could make two atomic integers, an atomic `usize` for the counter and
/// an atomic `u32` for the futex wait/wake. See the `sync` implementation
/// with `atomic-wait` feature.
/// - Before each futex wait, it spins to wait.
///
/// From the above we can see how similar this latch implementation is to
/// popular `std::latch` implementations in C++. Therefore any C++ programmer
/// can smoothly migrate from `std::latch` to this latch implementation.
///
/// Note: the ordering of the futex wait/wake corresponds to [`SeqCst`].
///
/// # Examples
///
/// Created by `1` can be used as a simple gate, all threads calling [`wait()`]
/// will be blocked until a thread calls [`count_down()`].
///
/// Created by `N` can be used to make one or more threads wait until `N`
/// operations have completed, or an operation has completed 'N' times.
///
/// [`Barrier`]: std::sync::Barrier
/// [`SeqCst`]: std::sync::atomic::Ordering::SeqCst
/// [`arrive()`]: Latch::arrive
/// [`count_down()`]: Latch::count_down
/// [`wait()`]: Latch::wait
///
/// ```
/// // The std for example, the futex implementation can be used in no-std.
/// use std::{
/// sync::{
/// atomic::{AtomicU32, Ordering},
/// Arc, RwLock,
/// },
/// thread,
/// };
///
/// use latches::futex::Latch;
///
/// let init_gate = Arc::new(Latch::new(1));
/// let operation = Arc::new(Latch::new(30));
/// let results = Arc::new(RwLock::new(Vec::<AtomicU32>::new()));
///
/// for i in 0..10 {
/// let gate = init_gate.clone();
/// let part = operation.clone();
/// let res = results.clone();
///
/// // Each thread need to process 3 operations
/// thread::spawn(move || {
/// gate.wait();
///
/// let db = res.read().unwrap();
/// for j in 0..3 {
/// db[i * 3 + j].store((i * 3 + j) as u32, Ordering::Relaxed);
/// part.count_down();
/// }
/// });
/// }
///
/// let res = results.clone();
/// thread::spawn(move || {
/// // Init some statuses, e.g. DB, File System, etc.
/// let mut db = res.write().unwrap();
/// for _ in 0..30 {
/// db.push(AtomicU32::new(0));
/// }
/// init_gate.count_down();
/// });
///
/// // All 30 operations will be done after this line
/// // Or use operation.wait_timeout(Duration) to set the timeout
/// operation.wait();
///
/// let res: Vec<_> = results.read()
/// .unwrap()
/// .iter()
/// .map(|i| i.load(Ordering::Relaxed))
/// .collect();
/// assert_eq!(res, Vec::from_iter(0..30));
/// ```