forked from compio-rs/compio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.rs
More file actions
153 lines (127 loc) · 3.61 KB
/
time.rs
File metadata and controls
153 lines (127 loc) · 3.61 KB
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
use std::{
collections::BTreeMap,
future::Future,
marker::PhantomData,
mem::replace,
pin::Pin,
task::{Context, Poll, Waker},
time::{Duration, Instant},
};
use crate::runtime::Runtime;
pub(crate) enum FutureState {
Active(Option<Waker>),
Completed,
}
impl Default for FutureState {
fn default() -> Self {
Self::Active(None)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct TimerKey {
deadline: Instant,
key: u64,
_local_marker: PhantomData<*const ()>,
}
pub struct TimerRuntime {
key: u64,
wheel: BTreeMap<TimerKey, FutureState>,
}
impl TimerRuntime {
pub fn new() -> Self {
Self {
key: 0,
wheel: BTreeMap::default(),
}
}
/// If the timer is completed, remove it and return `true`. Otherwise return
/// `false` and keep it.
pub fn remove_completed(&mut self, key: &TimerKey) -> bool {
let completed = self
.wheel
.get(key)
.map(|state| matches!(state, FutureState::Completed))
.unwrap_or_default();
if completed {
self.wheel.remove(key);
}
completed
}
/// Insert a new timer. If the deadline is in the past, return `None`.
pub fn insert(&mut self, deadline: Instant) -> Option<TimerKey> {
if deadline <= Instant::now() {
return None;
}
let key = TimerKey {
key: self.key,
deadline,
_local_marker: PhantomData,
};
self.wheel.insert(key, FutureState::default());
self.key += 1;
Some(key)
}
/// Update the waker for a timer.
pub fn update_waker(&mut self, key: &TimerKey, waker: Waker) {
if let Some(w) = self.wheel.get_mut(key) {
*w = FutureState::Active(Some(waker));
}
}
/// Cancel a timer.
pub fn cancel(&mut self, key: &TimerKey) {
self.wheel.remove(key);
}
/// Get the minimum timeout duration for the next poll.
pub fn min_timeout(&self) -> Option<Duration> {
self.wheel.first_key_value().map(|(key, _)| {
let now = Instant::now();
key.deadline.saturating_duration_since(now)
})
}
/// Wake all the timer futures that have reached their deadline.
pub fn wake(&mut self) {
if self.wheel.is_empty() {
return;
}
let now = Instant::now();
self.wheel
.iter_mut()
.take_while(|(k, _)| k.deadline <= now)
.for_each(|(_, v)| {
if let FutureState::Active(Some(waker)) = replace(v, FutureState::Completed) {
waker.wake();
}
});
}
pub fn clear(&mut self) {
self.wheel.clear();
}
}
pub struct TimerFuture(TimerKey);
impl TimerFuture {
pub fn new(key: TimerKey) -> Self {
Self(key)
}
}
impl Future for TimerFuture {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Runtime::with_current(|r| r.poll_timer(cx, &self.0))
}
}
impl Drop for TimerFuture {
fn drop(&mut self) {
Runtime::with_current(|r| r.cancel_timer(&self.0));
}
}
crate::assert_not_impl!(TimerFuture, Send, Sync);
#[test]
fn timer_min_timeout() {
let mut runtime = TimerRuntime::new();
assert_eq!(runtime.min_timeout(), None);
let now = Instant::now();
runtime.insert(now + Duration::from_secs(1));
runtime.insert(now + Duration::from_secs(10));
let min_timeout = runtime.min_timeout().unwrap().as_secs_f32();
assert!(min_timeout < 1.);
}