-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.vek
More file actions
245 lines (214 loc) · 6.84 KB
/
Copy pathtime.vek
File metadata and controls
245 lines (214 loc) · 6.84 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
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
// std:time — clocks, durations, and sleep.
//
// The clocks are read by the runtime as a single total-nanoseconds value (one
// `clock_gettime`) and split here into seconds + sub-second nanoseconds, so the
// (secs, nanos) pair is always consistent. `Duration`/`Instant`/`SystemTime` are
// plain value structs (no heap fields — trivially copyable and droppable).
native fn __rt_time_mono_nanos() -> u64;
native fn __rt_time_real_nanos() -> i64;
native fn __rt_time_sleep(secs: u64, nanos: u32) -> void;
const NANOS_PER_SEC: u64 = 1000000000;
const U64_MAX: u64 = 18446744073709551615;
// A non-negative span of time. The constructors keep `nanos < 1_000_000_000`.
pub struct Duration {
pub secs: u64;
pub nanos: u32;
// Whole seconds, truncated.
pub inline fn as_secs(self) -> u64 {
return self.secs;
}
// Total milliseconds, saturating at `u64::MAX` on overflow.
pub inline fn as_millis(self) -> u64 {
let sub: u64 = (self.nanos as u64) / 1000000;
if self.secs > (U64_MAX - sub) / 1000 {
return U64_MAX;
}
return self.secs * 1000 + sub;
}
// Total microseconds, saturating.
pub inline fn as_micros(self) -> u64 {
let sub: u64 = (self.nanos as u64) / 1000;
if self.secs > (U64_MAX - sub) / 1000000 {
return U64_MAX;
}
return self.secs * 1000000 + sub;
}
// Total nanoseconds, saturating.
pub inline fn as_nanos(self) -> u64 {
let sub: u64 = self.nanos as u64;
if self.secs > (U64_MAX - sub) / NANOS_PER_SEC {
return U64_MAX;
}
return self.secs * NANOS_PER_SEC + sub;
}
satisfies Equal<Duration> {
inline fn equals(self, other: Duration) -> bool {
return self.secs == other.secs && self.nanos == other.nanos;
}
}
satisfies Order<Duration> {
fn compare(self, other: Duration) -> Ordering {
if self.secs < other.secs {
return Less;
}
if self.secs > other.secs {
return Greater;
}
if self.nanos < other.nanos {
return Less;
}
if self.nanos > other.nanos {
return Greater;
}
return Equal;
}
}
satisfies Clone {
inline fn clone(self) -> Self {
return self;
}
}
// Rendered in seconds with millisecond precision, e.g. `"3.500s"`.
satisfies Format {
fn format(self) -> string {
let millis: u64 = (self.nanos as u64) / 1000000;
let ms: string = if millis < 10 {
f"00{millis}"
} else if millis < 100 {
f"0{millis}"
} else {
f"{millis}"
};
return f"{self.secs}.{ms}s";
}
}
}
// A point on the monotonic clock. Only differences between `Instant`s are
// meaningful; the absolute value has no defined meaning.
pub struct Instant {
pub secs: u64;
pub nanos: u32;
satisfies Equal<Instant> {
inline fn equals(self, other: Instant) -> bool {
return self.secs == other.secs && self.nanos == other.nanos;
}
}
satisfies Order<Instant> {
fn compare(self, other: Instant) -> Ordering {
if self.secs < other.secs {
return Less;
}
if self.secs > other.secs {
return Greater;
}
if self.nanos < other.nanos {
return Less;
}
if self.nanos > other.nanos {
return Greater;
}
return Equal;
}
}
satisfies Clone {
inline fn clone(self) -> Self {
return self;
}
}
}
// A point on the wall clock, as an offset from the Unix epoch. May move backward
// (NTP); do not use for elapsed-time measurement — use `Instant` for that.
pub struct SystemTime {
pub unix_secs: i64;
pub nanos: u32;
satisfies Equal<SystemTime> {
inline fn equals(self, other: SystemTime) -> bool {
return self.unix_secs == other.unix_secs && self.nanos == other.nanos;
}
}
satisfies Order<SystemTime> {
fn compare(self, other: SystemTime) -> Ordering {
if self.unix_secs < other.unix_secs {
return Less;
}
if self.unix_secs > other.unix_secs {
return Greater;
}
if self.nanos < other.nanos {
return Less;
}
if self.nanos > other.nanos {
return Greater;
}
return Equal;
}
}
satisfies Clone {
inline fn clone(self) -> Self {
return self;
}
}
}
// ---- Duration constructors -----------------------------------------------
pub inline fn from_secs(secs: u64) -> Duration {
return Duration { secs: secs, nanos: 0 };
}
pub inline fn from_millis(ms: u64) -> Duration {
return Duration { secs: ms / 1000, nanos: ((ms % 1000) * 1000000) as u32 };
}
pub inline fn from_micros(us: u64) -> Duration {
return Duration { secs: us / 1000000, nanos: ((us % 1000000) * 1000) as u32 };
}
pub inline fn from_nanos(ns: u64) -> Duration {
return Duration { secs: ns / NANOS_PER_SEC, nanos: (ns % NANOS_PER_SEC) as u32 };
}
// ---- clocks --------------------------------------------------------------
// Reads the host monotonic clock. Never moves backward across two calls.
pub inline fn now() -> Instant {
let total: u64 = __rt_time_mono_nanos();
return Instant { secs: total / NANOS_PER_SEC, nanos: (total % NANOS_PER_SEC) as u32 };
}
// Reads the host wall clock.
pub inline fn system_now() -> SystemTime {
let total: i64 = __rt_time_real_nanos();
// `nanos` is the offset *within* the second, so it is always in [0, 1e9) —
// which means the split must floor, not truncate toward zero. Truncating gave a
// negative remainder for a pre-epoch clock, and casting that to u32 wrapped it
// (-1.5e9 -> unix_secs = -1, nanos = 3794967296 instead of -2, 5e8).
let mut secs: i64 = total / 1000000000;
let mut rem: i64 = total % 1000000000;
if rem < 0 {
rem += 1000000000;
secs -= 1;
}
return SystemTime { unix_secs: secs, nanos: rem as u32 };
}
// `later - earlier`. Panics if `earlier` is after `later` (a monotonicity violation).
pub fn duration_since(later: Instant, earlier: Instant) -> Duration {
if earlier.secs > later.secs
|| (earlier.secs == later.secs && earlier.nanos > later.nanos) {
panic("time.duration_since: the earlier instant is after the later one");
}
let mut secs: u64 = later.secs - earlier.secs;
let mut nanos: u32 = 0;
if later.nanos >= earlier.nanos {
nanos = later.nanos - earlier.nanos;
} else {
secs = secs - 1;
nanos = (later.nanos + 1000000000) - earlier.nanos;
}
return Duration { secs: secs, nanos: nanos };
}
// `duration_since(now(), start)`. Convenience for measuring elapsed time.
pub inline fn elapsed(start: Instant) -> Duration {
return duration_since(now(), start);
}
// ---- sleep ---------------------------------------------------------------
// Blocks the calling thread for at least `duration`. May sleep longer due to OS
// scheduling. A zero duration is a no-op (does not yield).
pub inline fn sleep(duration: Duration) -> void {
if duration.secs == 0 && duration.nanos == 0 {
return;
}
__rt_time_sleep(duration.secs, duration.nanos);
}