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
// Copyright (c) 2018 Nuclear Furnace
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//! Turnstyles are a way to provide futures-aware gated access in a sequential fashion.
//!
//! Callers will "join" the queue, and receive a future that will complete once they make it
//! through the turnstyle.  The turnstyle is controlled externally by some coordinator, and can
//! "turn" it to let the next waiter in line through.  Thus, you can have multiple waiters, which
//! can join the line at any time, and allow the coordinator to continually admit them through.
//!
//! This can be used to synchronize access to a resource, or to provide a synchronization point
//! to repeatedly calculation.
//!
//! One example is a network daemon that reloads its configuration and reestablishes all of its
//! listening sockets.  Normally, you might join (using `select`) both the listening socket
//! future and a close future so that you can shutdown the socket when its no longer required.
//!
//! With a turnstyle, you can join the queue every time you reload the configuration, and then
//! share that future to all of the newly created listeners.  Once the new listeners are ready, you
//! also do one turn on the turnstyle, which signals the last waiter in line -- a future shared
//! with all of the old listeners -- that they can now shutdown.  That same turnstyle can perform
//! this over and over without issue.
//!
//! Turnstyles internally protect themselves via a `Mutex` but are fast enough in normal cases that
//! you can `join` or `turn` from within a future without fear of stalling the executor.  If you're
//! joining at an extremely high frequency, you could potentially cause performance degradation.
extern crate futures;

use futures::{prelude::*, sync::oneshot};
use std::{
    collections::VecDeque,
    sync::{Arc, Mutex, atomic::AtomicUsize, atomic::Ordering::SeqCst},
};

/// A future that waits to be notified, based on its place in line.
pub struct Waiter {
    inner: oneshot::Receiver<usize>,
}

impl Future for Waiter {
    type Error = ();
    type Item = usize;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> { self.inner.poll().map_err(|_| ()) }
}

/// An ordered queue of waiting participants.
///
/// Every turn of the turnstyle, the next participant in queue is notified and removed from the
/// queue.  If the queue is empty, `turn` is a noop.  Waiters receive their all-time position
/// through the turnstyle as their item i.e. the first waiter receives 0, the second receives 1,
/// etc.
///
/// `Turnstyle`s can be cloned and are safe to share across threads.  When a `Turnstyle` is
/// dropped, all of its waiters will be notified.
#[derive(Clone)]
pub struct Turnstyle {
    waiters: Arc<Mutex<VecDeque<(usize, oneshot::Sender<usize>)>>>,
    version: Arc<AtomicUsize>,
}

impl Turnstyle {
    /// Creates a new, empty turnstyle.
    pub fn new() -> Turnstyle {
        Turnstyle {
            waiters: Arc::new(Mutex::new(VecDeque::new())),
            version: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// Joins the waiting queue.
    ///
    /// Returns a tuple of (`usize`, `Waiter`) to the caller.  The `usize` represents the waiter's
    /// ticket in the queue since creation of the turnstyle, and the `Waiter` is a future that will
    /// complete when the turnstyle turns and reaches the caller's position in the queue.
    pub fn join(&self) -> (usize, Waiter) {
        let version = self.version.fetch_add(1, SeqCst);
        let (tx, rx) = oneshot::channel();
        {
            let mut waiters = self.waiters.lock().expect("turnstyle unable to join line");
            waiters.push_back((version, tx));
        }

        (version, Waiter { inner: rx })
    }

    /// Turns once, letting a single waiter through.
    ///
    /// The `Waiter` is notified by the future completing.  The function returns `true` if a waiter
    /// was found/notified, `false` otherwise.
    pub fn turn(&self) -> bool {
        let waiter = {
            let mut waiters = self.waiters.lock().unwrap();
            waiters.pop_front()
        };

        if let Some((v, w)) = waiter {
            // We drop the result of the send here because our waiter may have disappeared.  This
            // is fine, because we don't care about notifying _at least_ one waiter, only about
            // making sure we've removed one waiter from the queue, whether we truly notified
            // someone or not.
            let _ = w.send(v);
            true
        } else {
            false
        }
    }
}

impl Drop for Turnstyle {
    fn drop(&mut self) {
        while self.turn() {}
    }
}

#[cfg(test)]
mod tests {
    use super::Turnstyle;
    use futures::{future, Future, Async};

    #[test]
    fn single_waiter() {
        future::lazy(|| {
            let ts = Turnstyle::new();

            let (_, mut w) = ts.join();
            assert!(!w.poll().unwrap().is_ready());

            ts.turn();
            assert!(w.poll().unwrap().is_ready());

            future::ok::<_, ()>(())
        }).wait()
            .unwrap();
    }

    #[test]
    fn multiple_waiters() {
        future::lazy(|| {
            let ts = Turnstyle::new();
            let (_, mut w1) = ts.join();
            let (_, mut w2) = ts.join();
            let (_, mut w3) = ts.join();

            assert!(!w1.poll().unwrap().is_ready());
            assert!(!w2.poll().unwrap().is_ready());
            assert!(!w2.poll().unwrap().is_ready());

            ts.turn();
            assert!(w1.poll().unwrap().is_ready());
            assert!(!w2.poll().unwrap().is_ready());
            assert!(!w3.poll().unwrap().is_ready());

            ts.turn();
            assert!(w2.poll().unwrap().is_ready());
            assert!(!w3.poll().unwrap().is_ready());

            ts.turn();
            assert!(w3.poll().unwrap().is_ready());

            future::ok::<_, ()>(())
        }).wait()
            .unwrap();
    }

    #[test]
    fn versions() {
        future::lazy(|| {
            let ts = Turnstyle::new();
            let (v1, mut w1) = ts.join();
            let (v2, mut w2) = ts.join();
            let (v3, mut w3) = ts.join();

            assert_eq!(v1, 0);
            assert_eq!(v2, 1);
            assert_eq!(v3, 2);

            ts.turn();
            ts.turn();
            ts.turn();

            if let Async::Ready(w1v) = w1.poll().unwrap() {
                assert_eq!(w1v, 0);
            } else {
                panic!("waiter 1 was not ready");
            }

            if let Async::Ready(w2v) = w2.poll().unwrap() {
                assert_eq!(w2v, 1);
            } else {
                panic!("waiter 2 was not ready");
            }

            if let Async::Ready(w3v) = w3.poll().unwrap() {
                assert_eq!(w3v, 2);
            } else {
                panic!("waiter 3 was not ready");
            }

            future::ok::<_, ()>(())
        }).wait()
            .unwrap();
    }

    #[test]
    fn on_drop() {
        future::lazy(|| {
            let ts = Turnstyle::new();
            let (_, mut w1) = ts.join();
            let (_, mut w2) = ts.join();
            let (_, mut w3) = ts.join();

            assert!(!w1.poll().unwrap().is_ready());
            assert!(!w2.poll().unwrap().is_ready());
            assert!(!w2.poll().unwrap().is_ready());

            drop(ts);

            assert!(w1.poll().unwrap().is_ready());
            assert!(w2.poll().unwrap().is_ready());
            assert!(w3.poll().unwrap().is_ready());

            future::ok::<_, ()>(())
        }).wait()
            .unwrap();
    }
}