[go: up one dir, main page]

wgroup 0.1.0

Part of the Ora scheduler framework.
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
//! A wait-group implementation for named tasks in async contexts.

use std::{
    collections::BTreeMap,
    pin::Pin,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc, Mutex,
    },
    task::{Context, Poll},
};

use futures::{future::pending, task::AtomicWaker, Future, Stream};
use tokio_util::sync::CancellationToken;

/// A wait group that keeps track a set of tasks.
#[derive(Debug)]
pub struct WaitGroup {
    cancel_on_drop: bool,
    inner: Arc<WgInner>,
}

impl Default for WaitGroup {
    fn default() -> Self {
        Self {
            cancel_on_drop: true,
            inner: Default::default(),
        }
    }
}

impl WaitGroup {
    /// Create a new wait group.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new wait guard.
    #[allow(clippy::missing_panics_doc)]
    pub fn add(&self) -> WaitGuard {
        self.inner.clone().add("")
    }

    /// Create a new wait guard with a specific name.
    #[allow(clippy::missing_panics_doc)]
    pub fn add_with(&self, name: &str) -> WaitGuard {
        self.inner.clone().add(name)
    }

    /// The number of currently active wait guards.
    #[must_use]
    #[allow(clippy::missing_panics_doc)]
    pub fn count(&self) -> usize {
        self.inner.guards.lock().unwrap().len()
    }

    /// Whether all wait guards have been dropped.
    #[must_use]
    #[allow(clippy::missing_panics_doc)]
    pub fn is_done(&self) -> bool {
        self.inner.guards.lock().unwrap().len() == 0
    }

    /// A future that resolves when the wait
    /// group is currently waiting.
    ///
    /// Tasks can interpret it as a form of
    /// cancellation.
    pub async fn waiting(&self) {
        self.inner.cancellation.cancelled().await;
    }

    /// Return whether the wait group is currently waiting.
    #[must_use]
    pub fn is_waiting(&self) -> bool {
        self.inner.cancellation.is_cancelled()
    }

    /// Return a handle to the wait group that allows
    /// adding new wait guards.
    #[must_use]
    pub fn handle(&self) -> WaitGroupHandle {
        WaitGroupHandle {
            inner: Some(self.inner.clone()),
        }
    }

    /// Set whether wait guards should be signaled
    /// when the wait group is dropped.
    ///
    /// Note that regardless of this setting
    /// the group never blocks on drop.
    ///
    /// The default is `true`.
    pub fn waiting_on_drop(&mut self, signal: bool) {
        self.cancel_on_drop = signal;
    }

    /// Wait for all guards to be dropped.
    pub fn all_done(self) -> impl Future<Output = ()> + Unpin {
        AllDone {
            last_count: 0,
            inner: self.inner.clone(),
        }
    }

    /// A stream returning the count of currently active wait guards,
    /// while also returning a list of names where available.
    pub fn all_done_stream(self) -> impl Stream<Item = (usize, Vec<Arc<str>>)> + Unpin {
        AllDone {
            last_count: 0,
            inner: self.inner.clone(),
        }
    }
}

impl Drop for WaitGroup {
    fn drop(&mut self) {
        if self.cancel_on_drop {
            self.inner.cancellation.cancel();
        }
    }
}

/// A handle to a wait group that allows
/// adding new wait guards.
#[derive(Debug, Clone)]
pub struct WaitGroupHandle {
    inner: Option<Arc<WgInner>>,
}

impl WaitGroupHandle {
    /// Create a new wait group handle
    /// that is not associated with any wait group.
    #[must_use]
    pub fn never() -> Self {
        Self { inner: None }
    }

    /// Create a new wait guard.
    #[allow(clippy::missing_panics_doc)]
    pub fn add(&self) -> WaitGuard {
        if let Some(inner) = &self.inner {
            inner.clone().add("")
        } else {
            WaitGuard::never()
        }
    }

    /// Create a new wait guard with a specific name.
    #[allow(clippy::missing_panics_doc)]
    pub fn add_with(&self, name: &str) -> WaitGuard {
        if let Some(inner) = &self.inner {
            inner.clone().add(name)
        } else {
            WaitGuard::never()
        }
    }

    /// Return whether the wait group is currently waiting.
    #[must_use]
    pub fn is_waiting(&self) -> bool {
        if let Some(inner) = &self.inner {
            inner.cancellation.is_cancelled()
        } else {
            false
        }
    }
}

/// A guard that keeps track of a task in a wait group.
///
/// It is considered done when dropped.
#[derive(Debug)]
#[must_use = "A wait guard must be kept until the given task is done"]
pub struct WaitGuard {
    id: usize,
    inner: Arc<WgInner>,
    _waker: DropWaker,
}

impl WaitGuard {
    /// Create a new wait guard
    /// that is not associated with any wait group
    /// and thus will never be considered done.
    pub fn never() -> Self {
        Self {
            id: usize::MAX,
            inner: Arc::new(WgInner::default()),
            _waker: DropWaker(Arc::new(AtomicWaker::new())),
        }
    }

    /// A future that resolves when the wait
    /// group is currently waiting.
    ///
    /// Tasks can interpret it as a form of
    /// cancellation.
    pub async fn waiting(&self) {
        if self.id == usize::MAX {
            return pending().await;
        }

        self.inner.cancellation.cancelled().await;
    }

    /// Return whether the wait group is currently waiting.
    #[must_use]
    pub fn is_waiting(&self) -> bool {
        if self.id == usize::MAX {
            return false;
        }

        self.inner.cancellation.is_cancelled()
    }

    /// Create a new wait guard with a specific name
    /// and add it to the wait group that owns this guard.
    #[allow(clippy::missing_panics_doc)]
    pub fn add(&self) -> Self {
        if self.id == usize::MAX {
            return Self::never();
        }

        self.inner.clone().add("")
    }

    /// Create a new wait guard with a specific name
    /// and add it to the wait group that owns this guard.
    #[allow(clippy::missing_panics_doc)]
    pub fn add_with(&self, name: &str) -> Self {
        if self.id == usize::MAX {
            return Self::never();
        }

        self.inner.clone().add(name)
    }

    /// Complete the wait guard explicitly.
    ///
    /// It is the same as dropping the guard.
    pub fn done(self) {}
}

impl Drop for WaitGuard {
    #[inline]
    fn drop(&mut self) {
        self.inner.guards.lock().unwrap().remove(&self.id);
    }
}

#[derive(Debug, Clone)]
struct DropWaker(Arc<AtomicWaker>);

impl Drop for DropWaker {
    fn drop(&mut self) {
        self.0.wake();
    }
}

struct AllDone {
    last_count: usize,
    inner: Arc<WgInner>,
}

impl Future for AllDone {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.inner.guards.lock().unwrap().is_empty() {
            Poll::Ready(())
        } else {
            self.inner.waker.register(cx.waker());
            Poll::Pending
        }
    }
}

impl Stream for AllDone {
    type Item = (usize, Vec<Arc<str>>);

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let guards = self.inner.guards.lock().unwrap();

        if guards.is_empty() {
            Poll::Ready(None)
        } else {
            let count = guards.len();
            self.inner.waker.register(cx.waker());

            // FIXME: This will miss the case where a guard is added
            //        and removed in the same poll cycle.
            let ret = if self.last_count == count {
                Poll::Pending
            } else {
                Poll::Ready(Some((
                    count,
                    guards.values().map(|g| g.name.clone()).collect(),
                )))
            };

            drop(guards);

            self.last_count = count;

            ret
        }
    }
}

#[derive(Debug, Default)]
struct WgInner {
    next_id: AtomicUsize,
    guards: Mutex<BTreeMap<usize, GuardInfo>>,
    cancellation: CancellationToken,
    waker: Arc<AtomicWaker>,
}

impl WgInner {
    fn new_id(&self) -> usize {
        self.next_id.fetch_add(1, Ordering::Relaxed)
    }

    /// Add a new wait guard with a specific name.
    ///
    /// If a guard with the same name already exists,
    /// a unique name will be generated on a best-effort basis.
    #[allow(clippy::missing_panics_doc)]
    pub fn add(self: Arc<Self>, name: &str) -> WaitGuard {
        let mut guards = self.guards.lock().unwrap();

        let id = self.new_id();
        let name = Arc::<str>::from(name);

        guards.insert(id, GuardInfo { name: name.clone() });

        WaitGuard {
            id,
            inner: self.clone(),
            _waker: DropWaker(self.waker.clone()),
        }
    }
}

#[derive(Debug)]
struct GuardInfo {
    name: Arc<str>,
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::*;

    #[tokio::test]
    async fn test_wait_group() {
        let task_count = 1_000;

        let group = WaitGroup::new();

        let done_counter = Arc::new(AtomicUsize::new(0));
        for _ in 0..task_count {
            let guard = group.add();
            let c = done_counter.clone();
            std::thread::spawn(move || {
                let _g = guard;
                std::thread::sleep(std::time::Duration::from_millis(30));
                c.fetch_add(1, Ordering::AcqRel);
            });
        }

        group.all_done().await;

        assert_eq!(done_counter.load(Ordering::Acquire), task_count);
    }

    #[tokio::test]
    async fn test_wait_guard_drop() {
        let task_count = 300;

        let group = WaitGroup::new();

        let done_counter = Arc::new(AtomicUsize::new(0));
        for _ in 0..task_count {
            let guard = group.add();
            let c = done_counter.clone();
            tokio::spawn(async move {
                let g = guard;
                g.waiting().await;
                c.fetch_add(1, Ordering::AcqRel);
            });
        }

        drop(group);

        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        assert_eq!(done_counter.load(Ordering::Acquire), task_count);
    }

    #[tokio::test]
    async fn test_wait_guard_drop2() {
        let task_count = 300;

        let group = WaitGroup::new();

        let done_counter = Arc::new(AtomicUsize::new(0));
        for _ in 0..task_count {
            let guard = group.add();
            let c = done_counter.clone();
            tokio::spawn(async move {
                let g = guard;
                g.waiting().await;
                c.fetch_add(1, Ordering::AcqRel);
            });
        }

        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        drop(group);

        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        assert_eq!(done_counter.load(Ordering::Acquire), task_count);
    }
}