latches 0.1.0

A downward counter (CountDownLatch) which can be used to synchronize threads or coordinate tasks
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#[cfg(feature = "std")]
use std::{
    fmt,
    future::Future,
    hint,
    pin::Pin,
    sync::atomic::{
        AtomicUsize,
        Ordering::{Acquire, Relaxed, Release},
    },
    task::{Context, Poll},
};

#[cfg(not(feature = "std"))]
use core::{
    fmt,
    future::Future,
    hint,
    pin::Pin,
    sync::atomic::{
        AtomicUsize,
        Ordering::{Acquire, Relaxed, Release},
    },
    task::{Context, Poll},
};

use crate::{lock::Mutex, macros, WaitTimeoutResult};

use self::waiters::Waiters;

#[cfg(test)]
mod tests;

mod waiters;

/// A latch is a downward counter which can be used to coordinate tasks. The
/// value of the counter is initialized on creation. Tasks may suspend 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 tasks wait for the counter to reach 0 by
/// calling [`count_down()`] or [`arrive()`].
///
/// It spins on every polling of waiting futures.
///
/// # Examples
///
/// Created by `1` can be used as a simple gate, all tasks calling [`wait()`]
/// will be suspended until a task calls [`count_down()`].
///
/// Created by `N` can be used to make one or more tasks wait until `N`
/// operations have completed, or an operation has completed 'N' times.
///
/// [`Barrier`]: std::sync::Barrier
/// [`Future`]: std::future::Future
/// [`arrive()`]: Latch::arrive
/// [`count_down()`]: Latch::count_down
/// [`wait()`]: Latch::wait
///
/// ```
/// # use tokio::{runtime::Builder, task};
/// use std::sync::{
///     atomic::{AtomicU32, Ordering},
///     Arc, RwLock,
/// };
///
/// use latches::task::Latch;
///
/// # Builder::new_multi_thread().build().unwrap().block_on(async move {
/// 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 task need to process 3 operations
///     task::spawn(async move {
///         gate.wait().await;
///
///         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();
/// task::spawn(async 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.watch(T) to set the timeout
/// operation.wait().await;
///
/// let res: Vec<_> = results.read()
///     .unwrap()
///     .iter()
///     .map(|i| i.load(Ordering::Relaxed))
///     .collect();
/// assert_eq!(res, Vec::from_iter(0..30));
/// # });
/// ```
pub struct Latch {
    stat: AtomicUsize,
    lock: Mutex<Waiters>,
}

impl Latch {
    /// Creates a new latch initialized with the given count.
    ///
    /// # Examples
    ///
    /// ```
    /// use latches::task::Latch;
    ///
    /// let latch = Latch::new(10);
    /// # drop(latch);
    /// ```
    #[cfg(not(latches_no_const_sync))]
    #[must_use]
    #[inline]
    pub const fn new(count: usize) -> Self {
        Self {
            stat: AtomicUsize::new(count),
            lock: Mutex::new(Waiters::new()),
        }
    }
    /// Creates a new latch initialized with the given count.
    #[cfg(latches_no_const_sync)]
    #[must_use]
    #[inline]
    pub fn new(count: usize) -> Self {
        Self {
            stat: AtomicUsize::new(count),
            lock: Mutex::new(Waiters::new()),
        }
    }

    /// Decrements the latch count, wake up all pending tasks if the counter
    /// reaches 0 after decrement.
    ///
    /// - If the counter has reached 0 then do nothing.
    /// - If the current count is greater than 0 then it is decremented.
    /// - If the new count is 0 then all pending tasks are waked up.
    ///
    /// # Examples
    ///
    /// ```
    /// use latches::task::Latch;
    ///
    /// let latch = Latch::new(1);
    /// latch.count_down();
    /// ```
    pub fn count_down(&self) {
        macros::decrement!(self, 1);
    }

    /// Decrements the latch count by `n`, wake up all pending tasks if the
    /// counter reaches 0 after decrement.
    ///
    /// It will not cause an overflow by decrement the counter.
    ///
    /// - If the `n` is 0 or the counter has reached 0 then do nothing.
    /// - If the current count is greater than `n` then decremented by `n`.
    /// - If the current count is greater than 0 and less than or equal to `n`,
    ///   then the new count will be 0, and all pending tasks are waked up.
    ///
    /// # Examples
    ///
    /// ```
    /// use latches::task::Latch;
    ///
    /// let latch = Latch::new(10);
    ///
    /// // Do a batch upsert SQL and return `updatedRows` = 10 in runtime.
    /// # let updatedRows = 10;
    /// latch.arrive(updatedRows);
    /// assert_eq!(latch.count(), 0);
    /// ```
    pub fn arrive(&self, n: usize) {
        if n == 0 {
            return;
        }

        macros::decrement!(self, n);
    }

    /// Acquires the current count.
    ///
    /// It is typically used for debugging and testing.
    ///
    /// # Examples
    ///
    /// ```
    /// use latches::task::Latch;
    ///
    /// let latch = Latch::new(3);
    /// assert_eq!(latch.count(), 3);
    /// ```
    #[must_use]
    #[inline]
    pub fn count(&self) -> usize {
        self.stat.load(Acquire)
    }

    /// Checks that the counter has reached 0 and return [`Ok(())`], otherwise
    /// return [`Err(count)`].
    ///
    /// # Examples
    ///
    /// ```
    /// use latches::task::Latch;
    ///
    /// let latch = Latch::new(1);
    /// assert_eq!(latch.try_wait(), Err(1));
    /// latch.count_down();
    /// assert_eq!(latch.try_wait(), Ok(()));
    /// ```
    #[inline]
    pub fn try_wait(&self) -> Result<(), usize> {
        macros::once_try_wait!(self)
    }

    /// Returns a future that suspends the current task to wait until the
    /// counter reaches 0.
    ///
    /// When the future is polled:
    ///
    /// - If the current count is 0 then ready immediately.
    /// - If the current count is greater than 0 then pending with a waker that
    ///   will be awakened by a [`count_down()`]/[`arrive()`] invocation which
    ///   causes the counter reaches 0.
    ///
    /// [`count_down()`]: Latch::count_down
    /// [`arrive()`]: Latch::arrive
    ///
    /// # Examples
    ///
    /// ```
    /// # use tokio::runtime::Builder;
    /// use std::{sync::Arc, thread};
    ///
    /// use latches::task::Latch;
    ///
    /// # Builder::new_multi_thread().build().unwrap().block_on(async move {
    /// let latch = Arc::new(Latch::new(1));
    /// let l1 = latch.clone();
    ///
    /// thread::spawn(move || l1.count_down());
    /// latch.wait().await;
    /// # });
    /// ```
    #[inline]
    pub fn wait(&self) -> LatchWait<'_> {
        LatchWait {
            id: None,
            latch: self,
        }
    }

    /// Returns a future that suspends the current task to wait until the
    /// counter reaches 0 or the timer done.
    ///
    /// It requires an asynchronous timer, which provides greater flexibility
    /// for optimization. For example, some implementations provide higher
    /// precision timers, while other implementations sacrifice timing accuracy
    /// for performance. Some async libraries provide a global timer pool, if
    /// your project is using these libraries you should consider using their
    /// built-in timers first.
    ///
    /// When the future is polled:
    ///
    /// - If the current count is 0 then [`Reached`] ready immediately.
    /// - If the timer is done then [`TimedOut(timer_res)`] ready immediately.
    /// - If the current count is greater than 0 then pending with a waker that
    ///   will be awakened by a [`count_down()`]/[`arrive()`] invocation which
    ///   causes the counter reaches 0, or awakened by the timer.
    ///
    /// [`Reached`]: WaitTimeoutResult::Reached
    /// [`TimedOut(timer_res)`]: WaitTimeoutResult::TimedOut
    /// [`count_down()`]: Latch::count_down
    /// [`arrive()`]: Latch::arrive
    ///
    /// # Examples
    ///
    /// This example shows how to extend your own `wait_timeout`.
    ///
    /// It is based on tokio, you can use other implementations that your
    /// prefers, like async-std, futures-timer, async-io, gloo-timers, etc.
    ///
    /// ```
    /// # use tokio::runtime::Builder;
    /// use std::ops::Deref;
    ///
    /// use tokio::time::{sleep, Duration};
    /// use latches::{task::Latch as Inner, WaitTimeoutResult as Res};
    ///
    /// #[repr(transparent)]
    /// struct Latch(Inner);
    ///
    /// impl Latch {
    ///     const fn new(count: usize) -> Latch {
    ///         Latch(Inner::new(count))
    ///     }
    /// }
    ///
    /// impl Latch {
    ///     async fn wait_timeout(&self, dur: Duration) -> Res<()> {
    ///         self.0.watch(sleep(dur)).await
    ///     }
    /// }
    ///
    /// impl Deref for Latch {
    ///     type Target = Inner;
    ///
    ///     fn deref(&self) -> &Self::Target {
    ///         &self.0
    ///     }
    /// }
    ///
    /// # Builder::new_multi_thread().enable_time().build().unwrap()
    /// # .block_on(async move {
    /// let latch = Latch::new(3);
    /// let dur = Duration::from_millis(10);
    ///
    /// latch.count_down();
    /// assert!(latch.wait_timeout(dur).await.is_timed_out());
    /// latch.arrive(2);
    /// assert!(latch.wait_timeout(dur).await.is_reached());
    /// # });
    /// ```
    #[inline]
    pub fn watch<T>(&self, timer: T) -> LatchWatch<'_, T> {
        LatchWatch {
            id: None,
            latch: self,
            timer,
        }
    }

    fn spin(&self) -> bool {
        macros::spin_try_wait!(self, s, true, s == 0);
    }

    #[cold]
    fn done(&self) {
        Waiters::wake_all(&self.lock);
    }
}

/// Future returned by [`Latch::wait`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct LatchWait<'a> {
    id: Option<usize>,
    latch: &'a Latch,
}

impl Future for LatchWait<'_> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let Self { latch, id } = self.get_mut();

        if latch.spin() {
            Poll::Ready(())
        } else {
            let mut lock = latch.lock.lock();

            if latch.stat.load(Acquire) == 0 {
                Poll::Ready(())
            } else {
                lock.upsert(id, cx.waker());

                Poll::Pending
            }
        }
    }
}

/// Future returned by [`Latch::watch`].
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct LatchWatch<'a, T> {
    id: Option<usize>,
    latch: &'a Latch,
    timer: T,
}

impl<T> LatchWatch<'_, T> {
    /// Gets the pinned timer.
    ///
    /// It is typically used to reset, cancel or pre-boot the timer, this
    /// depends on the timer implementation.
    ///
    /// # Examples
    ///
    /// This example shows how to reset a tokio timer, other libraries may or
    /// may not have other ways to resetting timers.
    ///
    /// ```
    /// # use tokio::runtime::Builder;
    /// use std::pin::Pin;
    ///
    /// use tokio::time::{sleep, Duration, Instant};
    /// use latches::task::Latch;
    ///
    /// # Builder::new_multi_thread().enable_time().build().unwrap()
    /// # .block_on(async move {
    /// let init_dur = Duration::from_millis(100);
    /// let reset_dur = Duration::from_millis(10);
    /// let latch = Latch::new(1);
    /// let start = Instant::now();
    /// let mut result = latch.watch(sleep(init_dur));
    /// let mut result = unsafe { Pin::new_unchecked(&mut result) };
    ///
    /// result.as_mut()
    ///     .timer() // Get `Pin<&mut tokio::time::Sleep>` here
    ///     .reset(start + reset_dur);
    /// result.await;
    /// assert!((reset_dur..init_dur).contains(&start.elapsed()));
    /// # });
    /// ```
    #[must_use]
    #[inline]
    pub fn timer(self: Pin<&mut Self>) -> Pin<&mut T> {
        // SAFETY: LatchWatch does not implement Drop, not repr(packed),
        // auto implement Unpin if T is Unpin cuz other fields are Unpin.
        unsafe {
            let Self { timer, .. } = self.get_unchecked_mut();
            Pin::new_unchecked(timer)
        }
    }
}

impl<T: Future> Future for LatchWatch<'_, T> {
    type Output = WaitTimeoutResult<T::Output>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // SAFETY: LatchWatch does not implement Drop, not repr(packed),
        // auto implement Unpin if T is Unpin cuz other fields are Unpin.
        let Self { id, latch, timer } = unsafe { self.get_unchecked_mut() };
        let timer = unsafe { Pin::new_unchecked(timer) };

        if latch.spin() {
            Poll::Ready(WaitTimeoutResult::Reached)
        } else {
            // Acquire lock after pulling timer, minimizing lock-in effects.
            let out = timer.poll(cx);
            let mut lock = latch.lock.lock();

            if latch.stat.load(Acquire) == 0 {
                Poll::Ready(WaitTimeoutResult::Reached)
            } else {
                match out {
                    Poll::Ready(t) => {
                        lock.remove(id);

                        Poll::Ready(WaitTimeoutResult::TimedOut(t))
                    }
                    Poll::Pending => {
                        lock.upsert(id, cx.waker());

                        Poll::Pending
                    }
                }
            }
        }
    }
}

impl fmt::Debug for Latch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Latch")
            .field("count", &self.stat)
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for LatchWait<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LatchWait").finish_non_exhaustive()
    }
}

impl<T> fmt::Debug for LatchWatch<'_, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LatchWatch").finish_non_exhaustive()
    }
}