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
use alloc::sync::Arc;
use core::{
    fmt,
    future::Future,
    pin::Pin,
    sync::atomic::{AtomicU64, Ordering},
    task::{Context, Poll},
    time::Duration,
};
use flume::{Receiver, Sender};

mod bitset;
mod entry;
mod stack;
mod wheel;

use entry::{ArcEntry, Entry};

pub struct Scheduler {
    wheel: wheel::Wheel,
    handle: Handle,
    queue: Receiver<ArcEntry>,
}

impl fmt::Debug for Scheduler {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Scheduler")
            .field("now", &self.handle.now())
            .field("wheel", &self.wheel)
            .field("queue", &self.queue.len())
            .finish()
    }
}

impl Default for Scheduler {
    fn default() -> Self {
        Self::new(Some(1024), Duration::from_millis(1))
    }
}

impl Scheduler {
    /// Creates a new Scheduler
    pub fn new(capacity: Option<usize>, tick_duration: Duration) -> Self {
        let (sender, queue) = if let Some(capacity) = capacity {
            flume::bounded(capacity)
        } else {
            flume::unbounded()
        };
        let handle = Handle::new(sender, tick_duration);

        Self {
            wheel: Default::default(),
            handle,
            queue,
        }
    }

    /// Returns a handle that can be easily cloned
    pub fn handle(&self) -> Handle {
        self.handle.clone()
    }

    /// Returns the amount of time until the next task
    ///
    /// An implementation may sleep for the duration.
    pub fn advance(&mut self) -> Option<Duration> {
        self.update();

        let ticks = self.wheel.advance()?;
        let time = self.handle.advance(ticks);

        Some(time)
    }

    /// Wakes all of the expired tasks
    pub fn wake(&mut self) -> usize {
        self.wheel.wake()
    }

    /// Move the queued entries into the wheel
    fn update(&mut self) {
        for entry in self.queue.try_iter() {
            self.wheel.insert(entry);
        }
    }
}

#[derive(Debug, Clone)]
pub struct Handle(Arc<InnerHandle>);

impl Handle {
    fn new(sender: Sender<ArcEntry>, tick_duration: Duration) -> Self {
        let nanos_per_tick = tick_duration.max(Duration::from_nanos(1)).as_nanos() as u64;
        let inner = InnerHandle {
            ticks: AtomicU64::new(0),
            sender,
            nanos_per_tick: AtomicU64::new(nanos_per_tick),
        };
        Self(Arc::new(inner))
    }

    /// Returns a future that sleeps for the given duration
    pub fn delay(&self, duration: Duration) -> Timer {
        // Assuming a tick duratin of 1ns, this will truncate out at about 584
        // years. Hopefully no one needs that :)
        let ticks = (duration.as_nanos() / self.nanos_per_tick() as u128) as u64;

        let entry = Entry::new(ticks, false);
        let handle = self.clone();
        Timer { handle, entry }
    }

    /// Returns the current amount of time that has passed for this scheduler
    pub fn now(&self) -> Duration {
        let mut ticks = self.ticks();
        ticks *= self.nanos_per_tick();
        Duration::from_nanos(ticks)
    }

    fn advance(&self, mut ticks: u64) -> Duration {
        if cfg!(test) {
            self.0
                .ticks
                .load(Ordering::SeqCst)
                .checked_add(ticks)
                .expect("tick overflow");
        }
        self.0.ticks.fetch_add(ticks, Ordering::SeqCst);
        ticks *= self.nanos_per_tick();
        Duration::from_nanos(ticks)
    }

    fn ticks(&self) -> u64 {
        self.0.ticks.load(Ordering::SeqCst)
    }

    fn nanos_per_tick(&self) -> u64 {
        self.0.nanos_per_tick.load(Ordering::SeqCst)
    }
}

#[derive(Debug)]
struct InnerHandle {
    ticks: AtomicU64,
    sender: Sender<ArcEntry>,
    nanos_per_tick: AtomicU64,
}

impl Handle {
    fn register(&self, entry: &ArcEntry) {
        self.0.sender.send(entry.clone()).expect("send queue full")
    }
}

/// A future that sleeps a task for a duration
pub struct Timer {
    handle: Handle,
    entry: ArcEntry,
}

impl Timer {
    /// Cancels the timer
    pub fn cancel(&mut self) {
        self.entry.cancel();
    }
}

impl Drop for Timer {
    fn drop(&mut self) {
        self.cancel();
    }
}

impl Future for Timer {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
        // check condition before to avoid needless registration
        if self.entry.take_expired() {
            return Poll::Ready(());
        }

        // register the waker with the entry
        self.entry.register(cx.waker());

        // check condition after registration to avoid loss of notification
        if self.entry.take_expired() {
            return Poll::Ready(());
        }

        // register the timer with the handle
        if !self.entry.is_registered() {
            self.handle.register(&self.entry);
        }

        Poll::Pending
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::executor;
    use alloc::{rc::Rc, vec::Vec};
    use bolero::{check, generator::*};
    use core::cell::Cell;

    fn executor() -> executor::Executor<Env> {
        executor::Executor::new(Env::default(), None)
    }

    #[derive(Default)]
    struct Env;

    impl crate::executor::Environment for Env {
        type Runner = Runner;

        fn with<F: Fn(Self::Runner)>(&mut self, _handle: &executor::Handle, f: F) -> Poll<()> {
            let count = Rc::new(Cell::new(false));
            let runner = Runner(count.clone());
            f(runner);
            if count.get() {
                Poll::Pending
            } else {
                Poll::Ready(())
            }
        }
    }

    #[derive(Default)]
    struct Runner(Rc<Cell<bool>>);

    impl crate::executor::Runner for Runner {
        fn run<F: FnOnce() -> bool + Send + 'static>(&mut self, run: F) {
            if run() {
                self.0.set(true);
            }
        }
    }

    async fn delay(handle: Handle, count: usize, delay: Duration) {
        let nanos_per_tick = handle.nanos_per_tick() as u128;
        for _ in 0..count {
            // get the time before the delay
            let now = handle.now();

            // await the delay
            handle.delay(delay).await;

            // get the time that has passed on the clock and make sure it matches the amount that
            // was delayed
            let actual = handle.now();
            let expected = now + delay;
            assert_eq!(
                actual.as_nanos() / nanos_per_tick,
                expected.as_nanos() / nanos_per_tick,
                "actual: {:?}, expected: {:?}",
                actual,
                expected
            );
        }
    }

    fn test_helper(delays: &[(u8, Duration)], min_time: Duration) {
        std::eprintln!();
        let mut scheduler = Scheduler::new(None, min_time);
        let mut executor = executor();

        let handle = scheduler.handle();

        for (count, duration) in delays.as_ref().iter() {
            executor.spawn(delay(handle.clone(), *count as usize, *duration));
        }

        let mut total = Duration::from_secs(0);

        loop {
            while executor.tick() == Poll::Pending {}

            if let Some(expiration) = scheduler.advance() {
                total += expiration;
                scheduler.wake();
            } else if scheduler.wake() == 0 {
                // there are no remaining tasks to execute
                break;
            }
        }

        assert!(executor.tick().is_ready(), "the task list should be empty");
        assert!(
            scheduler.advance().is_none(),
            "the scheduler should be empty"
        );
        assert_eq!(
            total,
            handle.now(),
            "the current time should reflect the total number of sleep durations"
        );

        drop(scheduler);
        drop(executor);

        assert_eq!(
            Arc::strong_count(&handle.0),
            1,
            "the scheduler should cleanly shut down"
        );
    }

    #[test]
    fn timer_test() {
        let min_time = Duration::from_nanos(1).as_nanos() as u64;
        let max_time = Duration::from_secs(3600).as_nanos() as u64;

        let delay = (min_time..max_time).map_gen(Duration::from_nanos);
        let count = 0u8..3;
        let delays = gen::<Vec<_>>().with().values((count, delay));

        let min_duration = (Duration::from_nanos(1).as_nanos()
            ..Duration::from_millis(10).as_nanos())
            .map_gen(|v| Duration::from_nanos(v as _));

        check!()
            .with_generator((delays, min_duration))
            .for_each(|(delays, min_duration)| test_helper(delays, *min_duration));
    }
}