ntex_util/time/
wheel.rs

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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//! Time wheel based timer service.
//!
//! Inspired by linux kernel timers system
#![allow(arithmetic_overflow, clippy::let_underscore_future)]
use std::cell::{Cell, RefCell};
use std::time::{Duration, Instant, SystemTime};
use std::{cmp::max, future::Future, mem, pin::Pin, rc::Rc, task, task::Poll};

use futures_timer::Delay;
use slab::Slab;

use crate::task::LocalWaker;

// Clock divisor for the next level
const LVL_CLK_SHIFT: u64 = 3;
const LVL_CLK_DIV: u64 = 1 << LVL_CLK_SHIFT;
const LVL_CLK_MASK: u64 = LVL_CLK_DIV - 1;

const fn lvl_shift(n: u64) -> u64 {
    n * LVL_CLK_SHIFT
}

const fn lvl_gran(n: u64) -> u64 {
    1 << lvl_shift(n)
}

// Resolution:
// 0: 1 millis
// 4: ~17 millis
const UNITS: u64 = 4;
// const UNITS: u64 = 0;

const fn to_units(n: u64) -> u64 {
    n >> UNITS
}

const fn to_millis(n: u64) -> u64 {
    n << UNITS
}

// The time start value for each level to select the bucket at enqueue time
const fn lvl_start(lvl: u64) -> u64 {
    (LVL_SIZE - 1) << ((lvl - 1) * LVL_CLK_SHIFT)
}

// Size of each clock level
const LVL_BITS: u64 = 6;
const LVL_SIZE: u64 = 1 << LVL_BITS;
const LVL_MASK: u64 = LVL_SIZE - 1;

// Level depth
const LVL_DEPTH: u64 = 8;

const fn lvl_offs(n: u64) -> u64 {
    n * LVL_SIZE
}

// The cutoff (max. capacity of the wheel)
const WHEEL_TIMEOUT_CUTOFF: u64 = lvl_start(LVL_DEPTH);
const WHEEL_TIMEOUT_MAX: u64 = WHEEL_TIMEOUT_CUTOFF - (lvl_gran(LVL_DEPTH - 1));
const WHEEL_SIZE: usize = (LVL_SIZE as usize) * (LVL_DEPTH as usize);

// Low res time resolution
const LOWRES_RESOLUTION: Duration = Duration::from_millis(5);

const fn as_millis(dur: Duration) -> u64 {
    dur.as_secs() * 1_000 + (dur.subsec_millis() as u64)
}

/// Returns an instant corresponding to “now”.
///
/// Resolution is 5ms
#[inline]
pub fn now() -> Instant {
    TIMER.with(Timer::now)
}

/// Returns the system time corresponding to “now”.
///
/// Resolution is 5ms
#[inline]
pub fn system_time() -> SystemTime {
    TIMER.with(Timer::system_time)
}

/// Returns the system time corresponding to “now”.
///
/// If low resolution system time is not set, use system time.
/// This method does not start timer driver.
#[inline]
#[doc(hidden)]
pub fn query_system_time() -> SystemTime {
    TIMER.with(Timer::system_time)
}

#[derive(Debug)]
pub struct TimerHandle(usize);

impl TimerHandle {
    /// Createt new timer and return handle
    pub fn new(millis: u64) -> Self {
        TIMER.with(|t| t.add_timer(millis))
    }

    /// Resets the `TimerHandle` instance to a new deadline.
    pub fn reset(&self, millis: u64) {
        TIMER.with(|t| t.update_timer(self.0, millis))
    }

    pub fn is_elapsed(&self) -> bool {
        TIMER.with(|t| t.0.inner.borrow().timers[self.0].bucket.is_none())
    }

    pub fn poll_elapsed(&self, cx: &mut task::Context<'_>) -> Poll<()> {
        TIMER.with(|t| {
            let entry = &t.0.inner.borrow().timers[self.0];
            if entry.bucket.is_none() {
                Poll::Ready(())
            } else {
                entry.task.register(cx.waker());
                Poll::Pending
            }
        })
    }
}

impl Drop for TimerHandle {
    fn drop(&mut self) {
        TIMER.with(|t| t.remove_timer(self.0));
    }
}

bitflags::bitflags! {
    #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
    pub struct Flags: u8 {
        const DRIVER_STARTED = 0b0000_0001;
        const DRIVER_RECALC  = 0b0000_0010;
        const LOWRES_TIMER   = 0b0000_1000;
        const LOWRES_DRIVER  = 0b0001_0000;
        const RUNNING        = 0b0010_0000;
    }
}

thread_local! {
    static TIMER: Timer = Timer::new();
}

struct Timer(Rc<TimerInner>);

struct TimerInner {
    elapsed: Cell<u64>,
    elapsed_time: Cell<Option<Instant>>,
    next_expiry: Cell<u64>,
    flags: Cell<Flags>,
    driver: LocalWaker,
    lowres_time: Cell<Option<Instant>>,
    lowres_stime: Cell<Option<SystemTime>>,
    lowres_driver: LocalWaker,
    inner: RefCell<TimerMod>,
}

struct TimerMod {
    timers: Slab<TimerEntry>,
    driver_sleep: Delay,
    buckets: Vec<Bucket>,
    /// Bit field tracking which bucket currently contain entries.
    occupied: [u64; WHEEL_SIZE],
    lowres_driver_sleep: Delay,
}

impl Timer {
    fn new() -> Self {
        Timer(Rc::new(TimerInner {
            elapsed: Cell::new(0),
            elapsed_time: Cell::new(None),
            next_expiry: Cell::new(u64::MAX),
            flags: Cell::new(Flags::empty()),
            driver: LocalWaker::new(),
            lowres_time: Cell::new(None),
            lowres_stime: Cell::new(None),
            lowres_driver: LocalWaker::new(),
            inner: RefCell::new(TimerMod {
                buckets: Self::create_buckets(),
                timers: Slab::default(),
                driver_sleep: Delay::new(Duration::ZERO),
                occupied: [0; WHEEL_SIZE],
                lowres_driver_sleep: Delay::new(Duration::ZERO),
            }),
        }))
    }

    fn create_buckets() -> Vec<Bucket> {
        let mut buckets = Vec::with_capacity(WHEEL_SIZE);
        for idx in 0..WHEEL_SIZE {
            let lvl = idx / (LVL_SIZE as usize);
            let offs = idx % (LVL_SIZE as usize);
            buckets.push(Bucket::new(lvl, offs))
        }
        buckets
    }

    fn now(&self) -> Instant {
        if let Some(cur) = self.0.lowres_time.get() {
            cur
        } else {
            let now = Instant::now();

            let flags = self.0.flags.get();
            if flags.contains(Flags::RUNNING) {
                self.0.lowres_time.set(Some(now));

                if flags.contains(Flags::LOWRES_DRIVER) {
                    self.0.lowres_driver.wake();
                } else {
                    LowresTimerDriver::start(self.0.clone());
                }
            }
            now
        }
    }

    fn system_time(&self) -> SystemTime {
        if let Some(cur) = self.0.lowres_stime.get() {
            cur
        } else {
            let now = SystemTime::now();
            let flags = self.0.flags.get();

            if flags.contains(Flags::RUNNING) {
                self.0.lowres_stime.set(Some(now));

                if flags.contains(Flags::LOWRES_DRIVER) {
                    self.0.lowres_driver.wake();
                } else {
                    LowresTimerDriver::start(self.0.clone());
                }
            }
            now
        }
    }

    /// Add the timer into the hash bucket
    fn add_timer(&self, millis: u64) -> TimerHandle {
        if millis == 0 {
            let mut inner = self.0.inner.borrow_mut();

            let entry = inner.timers.vacant_entry();
            let no = entry.key();

            entry.insert(TimerEntry {
                bucket_entry: 0,
                bucket: None,
                task: LocalWaker::new(),
            });
            return TimerHandle(no);
        }

        let mut flags = self.0.flags.get();
        flags.insert(Flags::RUNNING);
        self.0.flags.set(flags);

        let now = self.now();
        let elapsed_time = self.0.elapsed_time();
        let delta = if now >= elapsed_time {
            to_units(as_millis(now - elapsed_time) + millis)
        } else {
            to_units(millis)
        };

        let (no, bucket_expiry) = {
            // crate timer entry
            let (idx, bucket_expiry) = self
                .0
                .calc_wheel_index(self.0.elapsed.get().wrapping_add(delta), delta);

            let no = self.0.inner.borrow_mut().add_entry(idx);
            (no, bucket_expiry)
        };

        // Check whether new bucket expire earlier
        if bucket_expiry < self.0.next_expiry.get() {
            self.0.next_expiry.set(bucket_expiry);
            if flags.contains(Flags::DRIVER_STARTED) {
                flags.insert(Flags::DRIVER_RECALC);
                self.0.flags.set(flags);
                self.0.driver.wake();
            } else {
                TimerDriver::start(self.0.clone());
            }
        }

        TimerHandle(no)
    }

    /// Update existing timer
    fn update_timer(&self, hnd: usize, millis: u64) {
        if millis == 0 {
            self.remove_timer_bucket(hnd);
            self.0.inner.borrow_mut().timers[hnd].bucket = None;
            return;
        }

        let now = self.now();
        let elapsed_time = self.0.elapsed_time();
        let delta = if now >= elapsed_time {
            max(to_units(as_millis(now - elapsed_time) + millis), 1)
        } else {
            max(to_units(millis), 1)
        };

        let bucket_expiry = {
            // calc bucket
            let (idx, bucket_expiry) = self
                .0
                .calc_wheel_index(self.0.elapsed.get().wrapping_add(delta), delta);

            self.0.inner.borrow_mut().update_entry(hnd, idx);

            bucket_expiry
        };

        // Check whether new bucket expire earlier
        if bucket_expiry < self.0.next_expiry.get() {
            self.0.next_expiry.set(bucket_expiry);
            let mut flags = self.0.flags.get();
            if flags.contains(Flags::DRIVER_STARTED) {
                flags.insert(Flags::DRIVER_RECALC);
                self.0.flags.set(flags);
                self.0.driver.wake();
            } else {
                TimerDriver::start(self.0.clone());
            }
        }
    }

    fn remove_timer(&self, handle: usize) {
        self.0.inner.borrow_mut().remove_timer_bucket(handle, true)
    }

    fn remove_timer_bucket(&self, handle: usize) {
        self.0.inner.borrow_mut().remove_timer_bucket(handle, false)
    }
}

impl TimerMod {
    fn execute_expired_timers(&mut self, mut clk: u64) {
        for lvl in 0..LVL_DEPTH {
            let idx = (clk & LVL_MASK) + lvl * LVL_SIZE;
            let b = &mut self.buckets[idx as usize];
            if !b.entries.is_empty() {
                self.occupied[b.lvl as usize] &= b.bit_n;
                for no in b.entries.drain() {
                    if let Some(timer) = self.timers.get_mut(no) {
                        timer.complete();
                    }
                }
            }

            // Is it time to look at the next level?
            if (clk & LVL_CLK_MASK) != 0 {
                break;
            }
            // Shift clock for the next level granularity
            clk >>= LVL_CLK_SHIFT;
        }
    }

    fn remove_timer_bucket(&mut self, handle: usize, remove_handle: bool) {
        let entry = &mut self.timers[handle];
        if let Some(bucket) = entry.bucket {
            let b = &mut self.buckets[bucket as usize];
            b.entries.remove(entry.bucket_entry);
            if b.entries.is_empty() {
                self.occupied[b.lvl as usize] &= b.bit_n;
            }
        }

        if remove_handle {
            self.timers.remove(handle);
        }
    }

    fn add_entry(&mut self, idx: usize) -> usize {
        let entry = self.timers.vacant_entry();
        let no = entry.key();
        let bucket = &mut self.buckets[idx];
        let bucket_entry = bucket.add_entry(no);

        entry.insert(TimerEntry {
            bucket_entry,
            bucket: Some(idx as u16),
            task: LocalWaker::new(),
        });
        self.occupied[bucket.lvl as usize] |= bucket.bit;

        no
    }

    fn update_entry(&mut self, hnd: usize, idx: usize) {
        let entry = &mut self.timers[hnd];

        // cleanup active timer
        if let Some(bucket) = entry.bucket {
            // do not do anything if wheel bucket is the same
            if idx == bucket as usize {
                return;
            }

            // remove timer entry from current bucket
            let b = &mut self.buckets[bucket as usize];
            b.entries.remove(entry.bucket_entry);
            if b.entries.is_empty() {
                self.occupied[b.lvl as usize] &= b.bit_n;
            }
        }

        // put timer to new bucket
        let bucket = &mut self.buckets[idx];
        entry.bucket = Some(idx as u16);
        entry.bucket_entry = bucket.add_entry(hnd);

        self.occupied[bucket.lvl as usize] |= bucket.bit;
    }
}

impl TimerInner {
    fn calc_wheel_index(&self, expires: u64, delta: u64) -> (usize, u64) {
        if delta < lvl_start(1) {
            Self::calc_index(expires, 0)
        } else if delta < lvl_start(2) {
            Self::calc_index(expires, 1)
        } else if delta < lvl_start(3) {
            Self::calc_index(expires, 2)
        } else if delta < lvl_start(4) {
            Self::calc_index(expires, 3)
        } else if delta < lvl_start(5) {
            Self::calc_index(expires, 4)
        } else if delta < lvl_start(6) {
            Self::calc_index(expires, 5)
        } else if delta < lvl_start(7) {
            Self::calc_index(expires, 6)
        } else if delta < lvl_start(8) {
            Self::calc_index(expires, 7)
        } else {
            // Force expire obscene large timeouts to expire at the
            // capacity limit of the wheel.
            if delta >= WHEEL_TIMEOUT_CUTOFF {
                Self::calc_index(
                    self.elapsed.get().wrapping_add(WHEEL_TIMEOUT_MAX),
                    LVL_DEPTH - 1,
                )
            } else {
                Self::calc_index(expires, LVL_DEPTH - 1)
            }
        }
    }

    /// Helper function to calculate the bucket index and bucket expiration
    fn calc_index(expires: u64, lvl: u64) -> (usize, u64) {
        // The timer wheel has to guarantee that a timer does not fire
        // early. Early expiry can happen due to:
        // - Timer is armed at the edge of a tick
        // - Truncation of the expiry time in the outer wheel levels
        //
        // Round up with level granularity to prevent this.

        let expires = (expires + lvl_gran(lvl)) >> lvl_shift(lvl);
        (
            (lvl_offs(lvl) + (expires & LVL_MASK)) as usize,
            expires << lvl_shift(lvl),
        )
    }

    fn elapsed_time(&self) -> Instant {
        if let Some(elapsed_time) = self.elapsed_time.get() {
            elapsed_time
        } else {
            let elapsed_time = Instant::now();
            self.elapsed_time.set(Some(elapsed_time));
            elapsed_time
        }
    }

    fn execute_expired_timers(&self) {
        self.inner
            .borrow_mut()
            .execute_expired_timers(self.next_expiry.get());
    }

    /// Find next expiration bucket
    fn next_pending_bucket(&self) -> Option<u64> {
        let inner = self.inner.borrow_mut();

        let mut clk = self.elapsed.get();
        let mut next = u64::MAX;

        for lvl in 0..LVL_DEPTH {
            let lvl_clk = clk & LVL_CLK_MASK;
            let occupied = inner.occupied[lvl as usize];
            let pos = if occupied == 0 {
                -1
            } else {
                let zeros = occupied
                    .rotate_right((clk & LVL_MASK) as u32)
                    .trailing_zeros() as usize;
                zeros as isize
            };

            if pos >= 0 {
                let tmp = (clk + pos as u64) << lvl_shift(lvl);
                if tmp < next {
                    next = tmp
                }

                // If the next expiration happens before we reach
                // the next level, no need to check further.
                if (pos as u64) <= ((LVL_CLK_DIV - lvl_clk) & LVL_CLK_MASK) {
                    break;
                }
            }

            clk >>= LVL_CLK_SHIFT;
            clk += u64::from(lvl_clk != 0);
        }

        if next < u64::MAX {
            Some(next)
        } else {
            None
        }
    }

    /// Get next expiry time in millis
    fn next_expiry_ms(&self) -> u64 {
        to_millis(self.next_expiry.get().saturating_sub(self.elapsed.get()))
    }

    fn stop_wheel(&self) {
        // mark all timers as elapsed
        if let Ok(mut inner) = self.inner.try_borrow_mut() {
            let mut buckets = mem::take(&mut inner.buckets);
            for b in &mut buckets {
                for no in b.entries.drain() {
                    inner.timers[no].bucket = None;
                }
            }

            // cleanup info
            self.flags.set(Flags::empty());
            self.next_expiry.set(u64::MAX);
            self.elapsed.set(0);
            self.elapsed_time.set(None);
            self.lowres_time.set(None);
            self.lowres_stime.set(None);

            inner.buckets = buckets;
            inner.occupied = [0; WHEEL_SIZE];
        }
    }
}

#[derive(Debug)]
struct Bucket {
    lvl: u32,
    bit: u64,
    bit_n: u64,
    entries: Slab<usize>,
}

impl Bucket {
    fn add_entry(&mut self, no: usize) -> usize {
        self.entries.insert(no)
    }
}

impl Bucket {
    fn new(lvl: usize, offs: usize) -> Self {
        let bit = 1 << (offs as u64);
        Bucket {
            bit,
            lvl: lvl as u32,
            bit_n: !bit,
            entries: Slab::default(),
        }
    }
}

#[derive(Debug)]
struct TimerEntry {
    bucket: Option<u16>,
    bucket_entry: usize,
    task: LocalWaker,
}

impl TimerEntry {
    fn complete(&mut self) {
        if self.bucket.is_some() {
            self.bucket.take();
            self.task.wake();
        }
    }
}

struct TimerDriver(Rc<TimerInner>);

impl TimerDriver {
    fn start(timer: Rc<TimerInner>) {
        let mut flags = timer.flags.get();
        flags.insert(Flags::DRIVER_STARTED);
        timer.flags.set(flags);
        timer.inner.borrow_mut().driver_sleep =
            Delay::new(Duration::from_millis(timer.next_expiry_ms()));

        let _ = crate::spawn(TimerDriver(timer));
    }
}

impl Drop for TimerDriver {
    fn drop(&mut self) {
        self.0.stop_wheel();
    }
}

impl Future for TimerDriver {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
        self.0.driver.register(cx.waker());

        let mut flags = self.0.flags.get();
        if flags.contains(Flags::DRIVER_RECALC) {
            flags.remove(Flags::DRIVER_RECALC);
            self.0.flags.set(flags);

            let now = Instant::now();
            let deadline =
                if let Some(diff) = now.checked_duration_since(self.0.elapsed_time()) {
                    Duration::from_millis(self.0.next_expiry_ms()).saturating_sub(diff)
                } else {
                    Duration::from_millis(self.0.next_expiry_ms())
                };
            self.0.inner.borrow_mut().driver_sleep.reset(deadline);
        }

        loop {
            if Pin::new(&mut self.0.inner.borrow_mut().driver_sleep)
                .poll(cx)
                .is_ready()
            {
                let now = Instant::now();
                self.0.elapsed.set(self.0.next_expiry.get());
                self.0.elapsed_time.set(Some(now));
                self.0.execute_expired_timers();

                if let Some(next_expiry) = self.0.next_pending_bucket() {
                    self.0.next_expiry.set(next_expiry);
                    let dur = Duration::from_millis(self.0.next_expiry_ms());
                    self.0.inner.borrow_mut().driver_sleep.reset(dur);
                    continue;
                } else {
                    self.0.next_expiry.set(u64::MAX);
                    self.0.elapsed_time.set(None);
                }
            }
            return Poll::Pending;
        }
    }
}

struct LowresTimerDriver(Rc<TimerInner>);

impl LowresTimerDriver {
    fn start(timer: Rc<TimerInner>) {
        let mut flags = timer.flags.get();
        flags.insert(Flags::LOWRES_DRIVER);
        timer.flags.set(flags);
        timer.inner.borrow_mut().lowres_driver_sleep = Delay::new(LOWRES_RESOLUTION);

        let _ = crate::spawn(LowresTimerDriver(timer));
    }
}

impl Drop for LowresTimerDriver {
    fn drop(&mut self) {
        self.0.stop_wheel();
    }
}

impl Future for LowresTimerDriver {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
        self.0.lowres_driver.register(cx.waker());

        let mut flags = self.0.flags.get();
        if !flags.contains(Flags::LOWRES_TIMER) {
            flags.insert(Flags::LOWRES_TIMER);
            self.0.flags.set(flags);
            self.0
                .inner
                .borrow_mut()
                .lowres_driver_sleep
                .reset(LOWRES_RESOLUTION);
        }

        if Pin::new(&mut self.0.inner.borrow_mut().lowres_driver_sleep)
            .poll(cx)
            .is_ready()
        {
            self.0.lowres_time.set(None);
            self.0.lowres_stime.set(None);
            flags.remove(Flags::LOWRES_TIMER);
            self.0.flags.set(flags);
        }
        Poll::Pending
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::time::{interval, sleep, Millis};

    #[ntex_macros::rt_test2]
    async fn test_timer() {
        crate::spawn(async {
            let s = interval(Millis(25));
            loop {
                s.tick().await;
            }
        });
        let time = Instant::now();
        let fut1 = sleep(Millis(1000));
        let fut2 = sleep(Millis(200));

        fut2.await;
        let _elapsed = Instant::now() - time;
        #[cfg(not(target_os = "macos"))]
        assert!(
            _elapsed > Duration::from_millis(200) && _elapsed < Duration::from_millis(300),
            "elapsed: {:?}",
            _elapsed
        );

        fut1.await;
        let _elapsed = Instant::now() - time;
        #[cfg(not(target_os = "macos"))]
        assert!(
            _elapsed > Duration::from_millis(1000)
                && _elapsed < Duration::from_millis(1200), // osx
            "elapsed: {:?}",
            _elapsed
        );

        let time = Instant::now();
        sleep(Millis(25)).await;
        let _elapsed = Instant::now() - time;
        #[cfg(not(target_os = "macos"))]
        assert!(
            _elapsed > Duration::from_millis(20) && _elapsed < Duration::from_millis(50),
            "elapsed: {:?}",
            _elapsed
        );
    }
}