notify-future 0.2.6

Support asynchronous notification completion future
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::future::Future;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::sync::atomic::Ordering;
use std::task::{Context, Poll};

use atomic_waker::AtomicWaker;

#[cfg(test)]
use loom::cell::UnsafeCell;
#[cfg(test)]
use loom::sync::{atomic::AtomicU8, Arc};
#[cfg(not(test))]
use std::cell::UnsafeCell;
#[cfg(not(test))]
use std::sync::{atomic::AtomicU8, Arc};

const PENDING: u8 = 0;
const WRITING: u8 = 1;
const READY: u8 = 2;
const TAKEN: u8 = 3;
const CANCELED: u8 = 4;

struct NotifyFutureState<RESULT> {
    state: AtomicU8,
    waker: AtomicWaker,
    result: UnsafeCell<MaybeUninit<RESULT>>,
}

// SAFETY: `state` serializes all writes, reads, and drops of `result`. Values
// cross thread boundaries only by being written by the notifier and read by the
// waiter, so `RESULT: Send` is sufficient.
unsafe impl<RESULT: Send> Send for NotifyFutureState<RESULT> {}
unsafe impl<RESULT: Send> Sync for NotifyFutureState<RESULT> {}

impl<RESULT> NotifyFutureState<RESULT> {
    pub fn new() -> Arc<NotifyFutureState<RESULT>> {
        Arc::new(NotifyFutureState {
            state: AtomicU8::new(PENDING),
            waker: AtomicWaker::new(),
            result: UnsafeCell::new(MaybeUninit::uninit()),
        })
    }

    pub fn set_complete(state: &Arc<NotifyFutureState<RESULT>>, result: RESULT) {
        state.set_complete_inner(result);
    }

    fn set_complete_inner(&self, result: RESULT) {
        if self
            .state
            .compare_exchange(PENDING, WRITING, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            return;
        }

        self.write_result(result);

        self.state.store(READY, Ordering::Release);
        self.waker.wake();
    }

    fn poll_result(&self, cx: &mut Context<'_>, already_taken_message: &str) -> Poll<RESULT> {
        match self.state.load(Ordering::Acquire) {
            READY => return Poll::Ready(self.take_result(already_taken_message)),
            TAKEN => panic!("{already_taken_message}"),
            _ => {}
        }

        self.waker.register(cx.waker());

        match self.state.load(Ordering::Acquire) {
            READY => Poll::Ready(self.take_result(already_taken_message)),
            TAKEN => panic!("{already_taken_message}"),
            _ => Poll::Pending,
        }
    }

    fn take_result(&self, already_taken_message: &str) -> RESULT {
        match self
            .state
            .compare_exchange(READY, TAKEN, Ordering::AcqRel, Ordering::Acquire)
        {
            Ok(_) => self.read_result(),
            Err(TAKEN) => panic!("{already_taken_message}"),
            Err(_) => panic!("Notify result was not ready"),
        }
    }

    fn cancel(&self) {
        let _ = self
            .state
            .compare_exchange(PENDING, CANCELED, Ordering::AcqRel, Ordering::Acquire);
        let _ = self.waker.take();
    }

    pub fn is_canceled(&self) -> bool {
        self.state.load(Ordering::Acquire) == CANCELED
    }
}

#[cfg(not(test))]
impl<RESULT> NotifyFutureState<RESULT> {
    fn write_result(&self, result: RESULT) {
        // SAFETY: the PENDING -> WRITING transition gives this notifier unique
        // write access. Readers only read after observing READY.
        unsafe {
            self.result.get().write(MaybeUninit::new(result));
        }
    }

    fn read_result(&self) -> RESULT {
        // SAFETY: READY means the result was initialized, and the successful
        // READY -> TAKEN transition gives this caller unique ownership.
        unsafe { (*self.result.get()).as_ptr().read() }
    }

    fn drop_result(&mut self) {
        // SAFETY: READY means the result is initialized. `&mut self` means
        // no other Arc handles remain, so no other thread can take it now.
        unsafe {
            self.result.get_mut().assume_init_drop();
        }
    }
}

#[cfg(test)]
impl<RESULT> NotifyFutureState<RESULT> {
    fn write_result(&self, result: RESULT) {
        // SAFETY: the PENDING -> WRITING transition gives this notifier unique
        // write access. Readers only read after observing READY.
        self.result.with_mut(|ptr| unsafe {
            ptr.write(MaybeUninit::new(result));
        });
    }

    fn read_result(&self) -> RESULT {
        // SAFETY: READY means the result was initialized, and the successful
        // READY -> TAKEN transition gives this caller unique ownership.
        self.result.with(|ptr| unsafe { (*ptr).as_ptr().read() })
    }

    fn drop_result(&mut self) {
        // SAFETY: READY means the result is initialized. `&mut self` means
        // no other Arc handles remain, so no other thread can take it now.
        self.result.get_mut().with(|ptr| unsafe {
            (*ptr).assume_init_drop();
        });
    }
}

impl<RESULT> Drop for NotifyFutureState<RESULT> {
    fn drop(&mut self) {
        if self.state.load(Ordering::Acquire) == READY {
            self.drop_result();
        }
    }
}

#[deprecated(since = "0.2.1", note = "Please use Notify instead")]
pub struct NotifyFuture<RESULT> {
    state: Arc<NotifyFutureState<RESULT>>,
}

#[allow(deprecated)]
impl<RESULT> Clone for NotifyFuture<RESULT> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
        }
    }
}

#[allow(deprecated)]
impl<RESULT> NotifyFuture<RESULT> {
    pub fn new() -> Self {
        Self {
            state: NotifyFutureState::new(),
        }
    }

    pub fn set_complete(&self, result: RESULT) {
        NotifyFutureState::set_complete(&self.state, result);
    }
}

#[allow(deprecated)]
impl<RESULT> Default for NotifyFuture<RESULT> {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(deprecated)]
impl<RESULT> Future for NotifyFuture<RESULT> {
    type Output = RESULT;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.state.poll_result(
            cx,
            "NotifyFuture was awaited by more than one task. Use Notify::new() instead",
        )
    }
}

pub struct Notify<RESULT> {
    state: Arc<NotifyFutureState<RESULT>>,
}

impl<RESULT> Notify<RESULT> {
    pub fn new() -> (Self, NotifyWaiter<RESULT>) {
        let state = NotifyFutureState::new();
        (
            Self {
                state: state.clone(),
            },
            NotifyWaiter::new(state),
        )
    }

    pub fn notify(self, result: RESULT) {
        NotifyFutureState::set_complete(&self.state, result);
    }

    pub fn is_canceled(&self) -> bool {
        self.state.is_canceled()
    }
}

pub struct NotifyWaiter<RESULT> {
    state: Arc<NotifyFutureState<RESULT>>,
}

impl<RESULT> NotifyWaiter<RESULT> {
    pub(crate) fn new(state: Arc<NotifyFutureState<RESULT>>) -> Self {
        Self { state }
    }
}

impl<RESULT> Drop for NotifyWaiter<RESULT> {
    fn drop(&mut self) {
        self.state.cancel();
    }
}

impl<RESULT> Future for NotifyWaiter<RESULT> {
    type Output = RESULT;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.state
            .poll_result(cx, "NotifyWaiter was polled after completion")
    }
}

#[cfg(test)]
#[allow(deprecated)]
mod test {
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::{
        atomic::{AtomicUsize as StdAtomicUsize, Ordering},
        Arc as StdArc,
    };
    use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};

    use loom::sync::{atomic::AtomicUsize, Arc};
    use loom::thread;

    use crate::{Notify, NotifyFuture};

    struct DropCounter {
        drops: Arc<AtomicUsize>,
    }

    impl Drop for DropCounter {
        fn drop(&mut self) {
            self.drops.fetch_add(1, Ordering::SeqCst);
        }
    }

    fn noop_waker() -> &'static Waker {
        Waker::noop()
    }

    fn counting_waker(wakes: StdArc<StdAtomicUsize>) -> Waker {
        unsafe fn clone(data: *const ()) -> RawWaker {
            let wakes = StdArc::<StdAtomicUsize>::from_raw(data.cast::<StdAtomicUsize>());
            let cloned = wakes.clone();
            let _ = StdArc::into_raw(wakes);
            RawWaker::new(StdArc::into_raw(cloned).cast::<()>(), &VTABLE)
        }

        unsafe fn wake(data: *const ()) {
            let wakes = StdArc::<StdAtomicUsize>::from_raw(data.cast::<StdAtomicUsize>());
            wakes.fetch_add(1, Ordering::SeqCst);
        }

        unsafe fn wake_by_ref(data: *const ()) {
            let wakes = StdArc::<StdAtomicUsize>::from_raw(data.cast::<StdAtomicUsize>());
            wakes.fetch_add(1, Ordering::SeqCst);
            let _ = StdArc::into_raw(wakes);
        }

        unsafe fn drop(data: *const ()) {
            let _ = StdArc::<StdAtomicUsize>::from_raw(data.cast::<StdAtomicUsize>());
        }

        static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop);

        let raw = RawWaker::new(StdArc::into_raw(wakes).cast::<()>(), &VTABLE);
        unsafe { Waker::from_raw(raw) }
    }

    #[test]
    fn notify_future_ready_after_set_complete() {
        loom::model(|| {
            let mut notify_future = NotifyFuture::<u32>::new();
            let notifier = notify_future.clone();

            notifier.set_complete(1);

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut notify_future).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("future remained pending after set_complete"),
            };

            assert_eq!(value, 1);
        });
    }

    #[test]
    fn notify_waiter_ready_after_notify() {
        loom::model(|| {
            let (notify, mut waiter) = Notify::<u32>::new();

            notify.notify(1);

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut waiter).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("waiter remained pending after notify"),
            };

            assert_eq!(value, 1);
        });
    }

    #[test]
    fn notify_waiter_drop_before_ready_is_canceled() {
        loom::model(|| {
            let (notify, waiter) = Notify::<u32>::new();
            drop(waiter);
            assert!(notify.is_canceled());
        });
    }

    #[test]
    fn notify_waiter_wakes_registered_waker() {
        loom::model(|| {
            let (notify, mut waiter) = Notify::<u32>::new();
            let wakes = StdArc::new(StdAtomicUsize::new(0));
            let waker = counting_waker(wakes.clone());
            let mut cx = Context::from_waker(&waker);

            assert!(matches!(Pin::new(&mut waiter).poll(&mut cx), Poll::Pending));

            notify.notify(1);

            assert_eq!(wakes.load(Ordering::SeqCst), 1);
            let value = match Pin::new(&mut waiter).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("waiter remained pending after notify"),
            };
            assert_eq!(value, 1);
        });
    }

    #[test]
    fn notify_after_waiter_drop_discards_result() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let (notify, waiter) = Notify::<DropCounter>::new();

            drop(waiter);
            assert!(notify.is_canceled());

            notify.notify(DropCounter {
                drops: drops.clone(),
            });

            assert_eq!(drops.load(Ordering::SeqCst), 1);
        });
    }

    #[test]
    fn waiter_drop_after_ready_does_not_cancel() {
        loom::model(|| {
            let state = super::NotifyFutureState::new();
            let notify = Notify {
                state: state.clone(),
            };
            let waiter = super::NotifyWaiter::new(state.clone());

            notify.notify(1);
            drop(waiter);

            assert!(!state.is_canceled());
        });
    }

    #[test]
    fn repeated_set_complete_keeps_first_value() {
        loom::model(|| {
            let mut notify_future = NotifyFuture::<u32>::new();
            let notifier = notify_future.clone();

            notifier.set_complete(1);
            notifier.set_complete(2);

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut notify_future).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("future remained pending after set_complete"),
            };

            assert_eq!(value, 1);
        });
    }

    #[test]
    fn ready_value_is_dropped_if_waiter_is_dropped() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let (notify, waiter) = Notify::<DropCounter>::new();

            notify.notify(DropCounter {
                drops: drops.clone(),
            });
            drop(waiter);

            assert_eq!(drops.load(Ordering::SeqCst), 1);
        });
    }

    #[test]
    fn taken_value_is_not_dropped_twice() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let (notify, mut waiter) = Notify::<DropCounter>::new();

            notify.notify(DropCounter {
                drops: drops.clone(),
            });

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut waiter).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("waiter remained pending after notify"),
            };

            assert_eq!(drops.load(Ordering::SeqCst), 0);
            drop(value);
            assert_eq!(drops.load(Ordering::SeqCst), 1);
        });
    }

    #[test]
    fn notify_racing_waiter_drop_drops_value_once() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let (notify, waiter) = Notify::<DropCounter>::new();

            let notify_thread = {
                let drops = drops.clone();
                thread::spawn(move || {
                    notify.notify(DropCounter { drops });
                })
            };

            let drop_thread = thread::spawn(move || {
                drop(waiter);
            });

            notify_thread.join().unwrap();
            drop_thread.join().unwrap();

            assert_eq!(drops.load(Ordering::SeqCst), 1);
        });
    }

    #[test]
    fn poll_racing_notify_observes_one_ready_value() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let (notify, mut waiter) = Notify::<DropCounter>::new();

            let notify_thread = {
                let drops = drops.clone();
                thread::spawn(move || {
                    notify.notify(DropCounter { drops });
                })
            };

            let mut cx = Context::from_waker(noop_waker());

            match Pin::new(&mut waiter).poll(&mut cx) {
                Poll::Ready(value) => drop(value),
                Poll::Pending => {
                    notify_thread.join().unwrap();
                    let value = match Pin::new(&mut waiter).poll(&mut cx) {
                        Poll::Ready(value) => value,
                        Poll::Pending => panic!("waiter remained pending after notify"),
                    };
                    drop(value);
                    assert_eq!(drops.load(Ordering::SeqCst), 1);
                    return;
                }
            }

            notify_thread.join().unwrap();
            assert_eq!(drops.load(Ordering::SeqCst), 1);
        });
    }

    #[test]
    fn repeated_notify_future_set_complete_drops_both_inputs_once() {
        loom::model(|| {
            let drops = Arc::new(AtomicUsize::new(0));
            let mut notify_future = NotifyFuture::<DropCounter>::new();
            let first_notifier = notify_future.clone();
            let second_notifier = notify_future.clone();

            let first_thread = {
                let drops = drops.clone();
                thread::spawn(move || {
                    first_notifier.set_complete(DropCounter { drops });
                })
            };

            let second_thread = {
                let drops = drops.clone();
                thread::spawn(move || {
                    second_notifier.set_complete(DropCounter { drops });
                })
            };

            first_thread.join().unwrap();
            second_thread.join().unwrap();

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut notify_future).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("future remained pending after set_complete"),
            };

            assert_eq!(drops.load(Ordering::SeqCst), 1);
            drop(value);
            assert_eq!(drops.load(Ordering::SeqCst), 2);
        });
    }

    #[test]
    #[should_panic(expected = "NotifyWaiter was polled after completion")]
    fn notify_waiter_panics_when_polled_after_completion() {
        loom::model(|| {
            let (notify, mut waiter) = Notify::<u32>::new();

            notify.notify(1);

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut waiter).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("waiter remained pending after notify"),
            };
            assert_eq!(value, 1);

            let _ = Pin::new(&mut waiter).poll(&mut cx);
        });
    }

    #[test]
    #[should_panic(expected = "NotifyFuture was awaited by more than one task")]
    fn notify_future_panics_when_polled_after_result_taken() {
        loom::model(|| {
            let mut first_waiter = NotifyFuture::<u32>::new();
            let notifier = first_waiter.clone();
            let mut second_waiter = first_waiter.clone();

            notifier.set_complete(1);

            let mut cx = Context::from_waker(noop_waker());
            let value = match Pin::new(&mut first_waiter).poll(&mut cx) {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("future remained pending after set_complete"),
            };
            assert_eq!(value, 1);

            let _ = Pin::new(&mut second_waiter).poll(&mut cx);
        });
    }
}