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
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll, Waker},
    time::{Duration, Instant},
};

use parking_lot::{Condvar, Mutex};

use crate::Arc;

#[derive(Debug)]
struct OneShotState<T> {
    filled: bool,
    fused: bool,
    item: Option<T>,
    waker: Option<Waker>,
}

impl<T> Default for OneShotState<T> {
    fn default() -> OneShotState<T> {
        OneShotState { filled: false, fused: false, item: None, waker: None }
    }
}

/// A Future value which may or may not be filled
#[derive(Debug)]
pub struct OneShot<T> {
    mu: Arc<Mutex<OneShotState<T>>>,
    cv: Arc<Condvar>,
}

/// The completer side of the Future
pub struct OneShotFiller<T> {
    mu: Arc<Mutex<OneShotState<T>>>,
    cv: Arc<Condvar>,
}

impl<T> OneShot<T> {
    /// Create a new `OneShotFiller` and the `OneShot`
    /// that will be filled by its completion.
    pub fn pair() -> (OneShotFiller<T>, Self) {
        let mu = Arc::new(Mutex::new(OneShotState::default()));
        let cv = Arc::new(Condvar::new());
        let future = Self { mu: mu.clone(), cv: cv.clone() };
        let filler = OneShotFiller { mu, cv };

        (filler, future)
    }

    /// Block on the `OneShot`'s completion
    /// or dropping of the `OneShotFiller`
    pub fn wait(self) -> Option<T> {
        let mut inner = self.mu.lock();
        while !inner.filled {
            self.cv.wait(&mut inner);
        }
        inner.item.take()
    }

    /// Block on the `OneShot`'s completion
    /// or dropping of the `OneShotFiller`,
    /// returning an error if not filled
    /// before a given timeout or if the
    /// system shuts down before then.
    ///
    /// Upon a successful receive, the
    /// oneshot should be dropped, as it
    /// will never yield that value again.
    pub fn wait_timeout(
        &mut self,
        mut timeout: Duration,
    ) -> Result<T, std::sync::mpsc::RecvTimeoutError> {
        let mut inner = self.mu.lock();
        while !inner.filled {
            let start = Instant::now();
            let res = self.cv.wait_for(&mut inner, timeout);
            if res.timed_out() {
                return Err(std::sync::mpsc::RecvTimeoutError::Disconnected);
            }
            timeout =
                if let Some(timeout) = timeout.checked_sub(start.elapsed()) {
                    timeout
                } else {
                    Duration::from_nanos(0)
                };
        }
        if let Some(item) = inner.item.take() {
            Ok(item)
        } else {
            Err(std::sync::mpsc::RecvTimeoutError::Disconnected)
        }
    }
}

impl<T> Future for OneShot<T> {
    type Output = Option<T>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut state = self.mu.lock();
        if state.fused {
            return Poll::Pending;
        }
        if state.filled {
            state.fused = true;
            Poll::Ready(state.item.take())
        } else {
            state.waker = Some(cx.waker().clone());
            Poll::Pending
        }
    }
}

impl<T> OneShotFiller<T> {
    /// Complete the `OneShot`
    pub fn fill(self, inner: T) {
        let mut state = self.mu.lock();

        if let Some(waker) = state.waker.take() {
            waker.wake();
        }

        state.filled = true;
        state.item = Some(inner);

        // having held the mutex makes this linearized
        // with the notify below.
        drop(state);

        let _notified = self.cv.notify_all();
    }
}

impl<T> Drop for OneShotFiller<T> {
    fn drop(&mut self) {
        let mut state = self.mu.lock();

        if state.filled {
            return;
        }

        if let Some(waker) = state.waker.take() {
            waker.wake();
        }

        state.filled = true;

        // having held the mutex makes this linearized
        // with the notify below.
        drop(state);

        let _notified = self.cv.notify_all();
    }
}