Repository navigation
Expand file tree
/
Copy pathtime.wave
More file actions
103 lines (92 loc) · 3.48 KB
/
Copy pathtime.wave
File metadata and controls
103 lines (92 loc) · 3.48 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
// This file is part of the Wave language project.
// Copyright (c) 2024-2026 Wave Foundation
// Copyright (c) 2024-2026 LunaStev and contributors
// SPDX-License-Identifier: Apache-2.0
import("std::sys::errors")::{SYS_ERR_INVAL, SYS_ERR_OVERFLOW};
// WASI Preview 1 clocks and monotonic deadline sleep through poll_oneoff.
extern(c, "clock_time_get") fun wasi_clock_time_get(
clock_id: u32, precision: u64, timestamp: ptr<u64>
) -> u16;
extern(c, "poll_oneoff") fun wasi_poll_oneoff(
subscriptions: ptr<WasiSubscription>, events: ptr<WasiEvent>,
count: u32, event_count: ptr<u32>
) -> u16;
struct WasiSubscriptionClock {
clock_id: u32;
timeout: u64;
precision: u64;
flags: u16;
}
struct WasiSubscription {
userdata: u64;
event_type: u8;
clock: WasiSubscriptionClock;
}
struct WasiEventFd {
byte_count: u64;
flags: u16;
}
struct WasiEvent {
userdata: u64;
error: u16;
event_type: u8;
fd: WasiEventFd;
}
pub struct TimeSpec {
sec: i64;
nsec: i64;
}
pub fun clock_gettime(clock_id: i32, tp: ptr<TimeSpec>) -> i64 {
if (tp == null || clock_id < 0 || clock_id > 3) { return SYS_ERR_INVAL; }
var timestamp: u64 = 0;
var error: u16 = wasi_clock_time_get(clock_id as u32, 1, ×tamp);
if (error != 0) { return -(error as i64); }
// Split unsigned nanoseconds first: both fields fit i64 even at u64::MAX.
deref tp.sec = (timestamp / 1000000000) as i64;
deref tp.nsec = (timestamp % 1000000000) as i64;
return 0;
}
const WASI_TIMESTAMP_MAX: u64 = 18446744073709551615;
pub fun nanosleep(req: ptr<TimeSpec>, rem: ptr<TimeSpec>) -> i64 {
if (req == null) { return SYS_ERR_INVAL; }
// Snapshot the request before initializing rem: callers may alias the two.
var seconds: i64 = deref req.sec;
var nanoseconds: i64 = deref req.nsec;
if (rem != null) { deref rem = TimeSpec { sec: 0, nsec: 0 }; }
if (seconds < 0 || nanoseconds < 0 || nanoseconds >= 1000000000) { return SYS_ERR_INVAL; }
if (seconds > 18446744073) { return SYS_ERR_OVERFLOW; }
if (seconds == 18446744073 && nanoseconds > 709551615) { return SYS_ERR_OVERFLOW; }
var timeout: u64 = seconds as u64 * 1000000000 + nanoseconds as u64;
if (timeout == 0) { return 0; }
var start: u64 = 0;
var error: u16 = wasi_clock_time_get(1, 1, &start);
if (error != 0) { return -(error as i64); }
if (timeout > WASI_TIMESTAMP_MAX - start) { return SYS_ERR_OVERFLOW; }
var deadline: u64 = start + timeout;
// Keep the deadline for interruption accounting; request a relative wait
// so hosts need not implement absolute monotonic subscriptions.
var clock: WasiSubscriptionClock = WasiSubscriptionClock {
clock_id: 1, timeout: timeout, precision: 1, flags: 0
};
var subscription: WasiSubscription = WasiSubscription {
userdata: 1, event_type: 0, clock: clock
};
var event: WasiEvent;
var count: u32 = 0;
error = wasi_poll_oneoff(&subscription, &event, 1, &count);
if (error == 0) {
if (count != 1) { return -29; }
error = event.error;
}
if (error == 27 && rem != null) {
var now: u64 = 0;
var clock_error: u16 = wasi_clock_time_get(1, 1, &now);
if (clock_error != 0) { return -(clock_error as i64); }
if (now < deadline) {
var remaining: u64 = deadline - now;
deref rem.sec = (remaining / 1000000000) as i64;
deref rem.nsec = (remaining % 1000000000) as i64;
}
}
return -(error as i64);
}