Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 56178af

Browse files
authored
Implement clock_{time,res}_get (#12)
Currently traps for the `*_CPUTIME_*` clocks and doesn't check for overflow on nanoseconds.
1 parent d0cc37a commit 56178af

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/lib.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,43 @@ pub unsafe extern "C" fn environ_sizes_get(
171171
/// return `errno::inval`.
172172
/// Note: This is similar to `clock_getres` in POSIX.
173173
#[no_mangle]
174-
pub unsafe extern "C" fn clock_res_get(id: Clockid, resolution: *mut Timestamp) -> Errno {
175-
unreachable()
174+
pub extern "C" fn clock_res_get(id: Clockid, resolution: &mut Timestamp) -> Errno {
175+
match id {
176+
CLOCKID_MONOTONIC => {
177+
let res = wasi_clocks::monotonic_clock_resolution(
178+
wasi_default_clocks::default_monotonic_clock(),
179+
);
180+
*resolution = res;
181+
}
182+
CLOCKID_REALTIME => {
183+
let res = wasi_clocks::wall_clock_resolution(wasi_default_clocks::default_wall_clock());
184+
*resolution = u64::from(res.nanoseconds) + res.seconds * 1_000_000_000;
185+
}
186+
_ => unreachable(),
187+
}
188+
ERRNO_SUCCESS
176189
}
177190

178191
/// Return the time value of a clock.
179192
/// Note: This is similar to `clock_gettime` in POSIX.
180193
#[no_mangle]
181194
pub unsafe extern "C" fn clock_time_get(
182195
id: Clockid,
183-
precision: Timestamp,
184-
time: *mut Timestamp,
196+
_precision: Timestamp,
197+
time: &mut Timestamp,
185198
) -> Errno {
186-
unreachable()
199+
match id {
200+
CLOCKID_MONOTONIC => {
201+
*time =
202+
wasi_clocks::monotonic_clock_now(wasi_default_clocks::default_monotonic_clock());
203+
}
204+
CLOCKID_REALTIME => {
205+
let res = wasi_clocks::wall_clock_now(wasi_default_clocks::default_wall_clock());
206+
*time = u64::from(res.nanoseconds) + res.seconds * 1_000_000_000;
207+
}
208+
_ => unreachable(),
209+
}
210+
ERRNO_SUCCESS
187211
}
188212

189213
/// Provide file advisory information on a file descriptor.

0 commit comments

Comments
 (0)