|
| 1 | +// Copyright 2024 FastLabs Developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::BinaryHeap; |
| 16 | +use std::ops::ControlFlow; |
| 17 | +use std::sync::atomic; |
| 18 | +use std::sync::atomic::AtomicBool; |
| 19 | +use std::sync::Arc; |
| 20 | +use std::time::Duration; |
| 21 | +use std::time::Instant; |
| 22 | + |
| 23 | +use crossbeam_queue::SegQueue; |
| 24 | +use parking::Parker; |
| 25 | +use parking::Unparker; |
| 26 | + |
| 27 | +use crate::TimeContext; |
| 28 | +use crate::TimeDriverShutdown; |
| 29 | +use crate::TimeEntry; |
| 30 | + |
| 31 | +/// Returns a new time driver, its time context and the shutdown handle. |
| 32 | +pub fn binary_heap_driver() -> (BinaryHeapTimeDriver, TimeContext, TimeDriverShutdown) { |
| 33 | + let (parker, unparker) = parking::pair(); |
| 34 | + let timers = BinaryHeap::new(); |
| 35 | + let inbounds = Arc::new(SegQueue::new()); |
| 36 | + let shutdown = Arc::new(AtomicBool::new(false)); |
| 37 | + |
| 38 | + let driver = BinaryHeapTimeDriver { |
| 39 | + parker, |
| 40 | + unparker, |
| 41 | + timers, |
| 42 | + inbounds, |
| 43 | + shutdown, |
| 44 | + }; |
| 45 | + |
| 46 | + let context = TimeContext { |
| 47 | + unparker: driver.unparker.clone(), |
| 48 | + inbounds: driver.inbounds.clone(), |
| 49 | + }; |
| 50 | + |
| 51 | + let shutdown = TimeDriverShutdown { |
| 52 | + unparker: driver.unparker.clone(), |
| 53 | + shutdown: driver.shutdown.clone(), |
| 54 | + }; |
| 55 | + |
| 56 | + (driver, context, shutdown) |
| 57 | +} |
| 58 | + |
| 59 | +/// A heap-based time driver that drives registered timers. |
| 60 | +#[derive(Debug)] |
| 61 | +pub struct BinaryHeapTimeDriver { |
| 62 | + parker: Parker, |
| 63 | + unparker: Unparker, |
| 64 | + timers: BinaryHeap<TimeEntry>, |
| 65 | + inbounds: Arc<SegQueue<TimeEntry>>, |
| 66 | + shutdown: Arc<AtomicBool>, |
| 67 | +} |
| 68 | + |
| 69 | +impl BinaryHeapTimeDriver { |
| 70 | + /// Drives the timers and returns `true` if the driver has been shut down. |
| 71 | + pub fn turn(&mut self) -> ControlFlow<()> { |
| 72 | + if self.shutdown.load(atomic::Ordering::Acquire) { |
| 73 | + return ControlFlow::Break(()); |
| 74 | + } |
| 75 | + |
| 76 | + match self.timers.peek() { |
| 77 | + None => self.parker.park(), |
| 78 | + Some(entry) => { |
| 79 | + let delta = entry.when.saturating_duration_since(Instant::now()); |
| 80 | + if delta > Duration::ZERO { |
| 81 | + self.parker.park_timeout(delta); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + while let Some(entry) = self.inbounds.pop() { |
| 87 | + self.timers.push(entry); |
| 88 | + } |
| 89 | + |
| 90 | + while let Some(entry) = self.timers.peek() { |
| 91 | + if entry.when <= Instant::now() { |
| 92 | + entry.waker.wake(); |
| 93 | + let _ = self.timers.pop(); |
| 94 | + } else { |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if self.shutdown.load(atomic::Ordering::Acquire) { |
| 100 | + ControlFlow::Break(()) |
| 101 | + } else { |
| 102 | + ControlFlow::Continue(()) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments