atomic-waitgroup 0.1.4

A waitgroup implementation supports async with advanced features
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
//! A waitgroup support async with advanced features
//!
//! implemented with atomic operations to reduce locking.
//!
//! # Features & restrictions
//!
//! * wait_to() supports waiting for a value >= zero
//!
//! * wait() & wait_to() can be canceled wrapped by timeout or futures::select!.
//!
//! * Assumes only one thread calls wait(). If multiple concurrent wait() is detected,
//! will panic for this invalid usage.
//!
//! * done() & wait() is allowed to called concurrently.
//!
//! * add() & done() is allowed to called concurrently.
//!
//! * Assumes add() and wait() are in the same thread.
//!
//! # Example
//!
//! ```
//! extern crate atomic_waitgroup;
//! use atomic_waitgroup::WaitGroup;
//! use tokio::runtime::Runtime;
//!
//! let rt = Runtime::new().unwrap();
//! let wg = WaitGroup::new();
//! rt.block_on(async move {
//!     for i in 0..2 {
//!         let _guard = wg.add_guard();
//!         tokio::spawn(async move {
//!            // Do something
//!             drop(_guard);
//!         });
//!     }
//!     match tokio::time::timeout(
//!         tokio::time::Duration::from_secs(1),
//!         wg.wait_to(1)).await {
//!         Ok(_) => {
//!             assert!(wg.left() <= 1);
//!         }
//!         Err(_) => {
//!             println!("wg.wait_to(1) timeouted");
//!         }
//!     }
//! });
//!
//!

use log::error;
use std::{
    future::Future,
    pin::Pin,
    sync::{
        atomic::{AtomicI64, Ordering},
        Arc,
    },
    task::{Context, Poll, Waker},
};
use parking_lot::Mutex;

/*

NOTE: Multiple atomic operation must happen at the same order

WaitGroupFuture |   done()
----------------|
left.load()     |
                |   left -=1
                |   load_waiting
waiting = true  |
left.load ()    |
------------------------------

*/
pub struct WaitGroup(Arc<WaitGroupInner>);

// do not allow multiple wait
impl Clone for WaitGroup {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

macro_rules! log_and_panic {
    ($($arg:tt)+) => (
        error!($($arg)+);
        panic!($($arg)+);
    );
}

macro_rules! trace_log {
    ($($arg:tt)+) => (
        #[cfg(feature="trace_log")]
        {
            log::trace!($($arg)+);
        }
    );
}

impl WaitGroup {
    pub fn new() -> Self {
        Self(WaitGroupInner::new())
    }

    /// Return the count left inside this WaitGroup
    #[inline(always)]
    pub fn left(&self) -> usize {
        let count = self.0.left.load(Ordering::SeqCst);
        if count < 0 {
            log_and_panic!("WaitGroup.left {} < 0", count);
        }
        count as usize
    }

    /// Add specified count.
    ///
    /// NOTE: You should always add() before done()
    #[inline(always)]
    pub fn add(&self, i: usize) {
        // To prevent code below re-order above, use Acquire here.
        let _r = self.0.left.fetch_add(i as i64, Ordering::Acquire);
        trace_log!("add {}->{}", i, _r + i as i64);
    }

    /// Add one to the WaitGroup, return a guard to decrease the count on drop.
    ///
    /// # Example
    ///
    /// ```
    /// extern crate atomic_waitgroup;
    /// use atomic_waitgroup::WaitGroup;
    /// use tokio::runtime::Runtime;
    ///
    /// let wg = WaitGroup::new();
    /// let rt = Runtime::new().unwrap();

    /// rt.block_on(async move {
    ///     let _guard = wg.add_guard();
    ///     tokio::spawn(async move {
    ///         // Do something
    ///         drop(_guard);
    ///     });
    ///     wg.wait().await;
    /// });
    #[inline(always)]
    pub fn add_guard(&self) -> WaitGroupGuard {
        self.add(1);
        WaitGroupGuard {
            inner: self.0.clone(),
        }
    }

    /// Wait until specified count is left in the WaitGroup.
    ///
    /// Return false means there's no waiting happened.
    ///
    /// Return true means the blocking actually happened.
    ///
    /// # NOTE
    ///
    /// * Only assume one waiting future at the same time, otherwise will panic.
    ///
    /// * Canceling future is supported.
    pub async fn wait_to(&self, target: usize) -> bool {
        let _self = self.0.as_ref();
        // We will check again with SeqCst later to prevent deadlock
        let left = _self.left.load(Ordering::Acquire);
        if left <= target as i64 {
            trace_log!("wait_to skip {} <= target {}", left, target);
            return false;
        }
        WaitGroupFuture {
            wg: &_self,
            target,
            waker: None,
        }
        .await;
        return true;
    }

    /// Wait until zero count in the WaitGroup.
    ///
    /// # NOTE
    ///
    /// * Only assume one waiting future at the same time, otherwise will panic.
    ///
    /// * Canceling future is supported.
    #[inline(always)]
    pub async fn wait(&self) {
        self.wait_to(0).await;
    }

    /// Decrease count by one.
    #[inline]
    pub fn done(&self) {
        let inner = self.0.as_ref();
        inner.done(1);
    }

    /// Decrease count by specified value
    #[inline]
    pub fn done_many(&self, count: usize) {
        let inner = self.0.as_ref();
        inner.done(count as i64);
    }
}

pub struct WaitGroupGuard {
    inner: Arc<WaitGroupInner>,
}

impl Drop for WaitGroupGuard {
    fn drop(&mut self) {
        let inner = &self.inner;
        inner.done(1);
    }
}

struct WaitGroupInner {
    /// The current count
    left: AtomicI64,
    /// The target count (>=0) if someone waiting, if no one is waiting, should be -1
    waiting: AtomicI64,
    waker: Mutex<Option<Arc<Waker>>>,
}

impl WaitGroupInner {
    #[inline(always)]
    fn new() -> Arc<Self> {
        Arc::new(Self {
            left: AtomicI64::new(0),
            waiting: AtomicI64::new(-1),
            waker: Mutex::new(None),
        })
    }
    #[inline]
    fn done(&self, count: i64) {
        // There's SeqCst behind, it's ok to use Relaxed
        let left = self.left.fetch_sub(count, Ordering::SeqCst) - count;
        if left < 0 {
            log_and_panic!("WaitGroup.left {} < 0", left);
        }
        let waiting = self.waiting.load(Ordering::SeqCst);
        if waiting < 0 {
            trace_log!("done {}->{} not waiting", count, left);
            return;
        }
        if left <= waiting {
            if self.waiting.compare_exchange(waiting, -1, Ordering::SeqCst, Ordering::Relaxed).is_ok() {
                let mut guard = self.waker.lock();
                if let Some(waker) = guard.take() {
                    waker.wake_by_ref();
                    drop(guard);
                    trace_log!("done {}->{} wake {}", count, left, waiting);
                } else {
                    drop(guard);
                    trace_log!("done {}->{} wake {} but no waker", count, left, waiting);
                }
            }
            // some one already wake
        } else {
            trace_log!("done {}->{} waiting {}", count, left, waiting);
        }
    }

    /// Once waker set, waker might be false waken many times
    #[inline]
    fn set_waker(&self, waker: Arc<Waker>, target: usize, force: bool) {
        trace_log!("set_waker {} force={}", target, force);
        {
            let mut guard = self.waker.lock();
            if !force {
                if guard.is_some() {
                    drop(guard);
                    log_and_panic!("concurrent wait detected");
                }
            }
            guard.replace(waker);
            let old_target = self.waiting.swap(target as i64, Ordering::SeqCst);
            drop(guard);
            if ! force && old_target >= 0 {
                log_and_panic!("Concurrent wait() by multiple coroutines, enter unlikely code");
            }
        }
    }

    #[inline]
    fn cancel_wait(&self) {
        trace_log!("cancel_wait");
        {
            let mut guard = self.waker.lock();
            self.waiting.store(-1, Ordering::SeqCst);
            let _ = guard.take();
        }
    }
}

struct WaitGroupFuture<'a> {
    wg: &'a WaitGroupInner,
    target: usize,
    waker: Option<Arc<Waker>>,
}

impl<'a> WaitGroupFuture<'a> {
    #[inline(always)]
    fn _poll(&mut self) -> bool {
        // Use SeqCst to avoid reading old value
        let cur = self.wg.left.load(Ordering::SeqCst);
        if cur <= self.target as i64 {
            trace_log!("poll ready {}<={}", cur, self.target);
            self._clear();
            true
        } else {
            trace_log!("poll not ready {}>{}", cur, self.target);
            false
        }
    }

    #[inline(always)]
    fn _clear(&mut self) {
        if self.waker.take().is_some() {
            self.wg.cancel_wait();
        }
    }
}

/// When wait() is canceled with timeout(),  make sure it clear the waker.
impl<'a> Drop for WaitGroupFuture<'a> {
    fn drop(&mut self) {
        self._clear();
    }
}

impl<'a> Future for WaitGroupFuture<'a> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
        let _self = self.get_mut();
        if _self._poll() {
            return Poll::Ready(());
        }
        let force = {
            if let Some(waker) = _self.waker.as_ref() {
                // First check if someone take the waker
                if _self.wg.waiting.load(Ordering::SeqCst) >= 0 &&
                    // Sometimes tokio will make waker ineffect,
                    // we should always check before reuse the same waker.
                    waker.will_wake(ctx.waker()) {
                    return Poll::Pending;
                }
                // The waker is not usable, reg another
                true
            } else {
                false
            }
        };
        // The Arc is for checking waker without lock
        let waker = Arc::new(ctx.waker().clone());
        _self.wg.set_waker(waker.clone(), _self.target, force);
        _self.waker.replace(waker);
        if _self._poll() {
            return Poll::Ready(());
        }
        Poll::Pending
    }
}

#[cfg(test)]
mod tests {
    extern crate rand;

    use std::time::Duration;
    use tokio::time::{sleep, timeout};

    use super::*;

    fn make_runtime(threads: usize) -> tokio::runtime::Runtime {
        return tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .worker_threads(threads)
            .build()
            .unwrap();
    }

    #[test]
    fn test_inner() {
        make_runtime(1).block_on(async move {
            let wg = WaitGroup::new();
            wg.add(2);
            let _wg = wg.clone();
            let th = tokio::spawn(async move {
                assert!(_wg.wait_to(1).await);
            });
            sleep(Duration::from_secs(1)).await;
            {
                let guard = wg.0.waker.lock();
                assert!(guard.is_some());
                assert_eq!(wg.0.waiting.load(Ordering::Acquire), 1);
            }
            wg.done();
            let _ = th.await;
            assert_eq!(wg.0.waiting.load(Ordering::Acquire), -1);
            assert_eq!(wg.left(), 1);
            wg.done();
            assert_eq!(wg.left(), 0);
            assert_eq!(wg.wait_to(0).await, false);
        });
    }

    #[test]
    fn test_cancel() {
        let wg = WaitGroup::new();
        make_runtime(1).block_on(async move {
            wg.add(1);
            println!("test timeout");
            assert!(timeout(Duration::from_secs(1), wg.wait()).await.is_err());
            println!("timeout happened");
            assert_eq!(wg.0.waiting.load(Ordering::Acquire), -1);
            wg.done();
            wg.add(2);
            wg.done_many(2);
            wg.add(2);
            let _wg = wg.clone();
            let th = tokio::spawn(async move {
                _wg.wait().await;
            });
            sleep(Duration::from_millis(200)).await;
            wg.done();
            wg.done();
            let _ = th.await;
        });
    }
}