Skip to content

Commit 8c9fb2c

Browse files
committed
Fix unsatisfied times import on wasi
1 parent 5aef92c commit 8c9fb2c

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ hexf-parse = "0.2.1"
4848
indexmap = "1.0.2"
4949
ahash = "0.7.2"
5050
bitflags = "1.3"
51-
libc = "0.2.99"
51+
libc = "0.2.107"
5252
nix = "0.23"
5353
paste = "1.0.5"
5454
is-macro = "0.1"

vm/src/stdlib/time.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,15 @@ mod time {
248248
Ok(get_thread_time(vm)?.as_nanos() as u64)
249249
}
250250

251-
#[cfg(any(windows, all(target_arch = "wasm32", not(target_os = "unknown"))))]
251+
#[cfg(any(windows, all(target_arch = "wasm32", target_arch = "emscripten")))]
252252
pub(super) fn time_muldiv(ticks: i64, mul: i64, div: i64) -> u64 {
253253
let intpart = ticks / div;
254254
let ticks = ticks % div;
255255
let remaining = (ticks * mul) / div;
256256
(intpart * mul + remaining) as u64
257257
}
258258

259-
#[cfg(all(target_arch = "wasm32", not(target_os = "unknown")))]
259+
#[cfg(all(target_arch = "wasm32", target_os = "emscripten"))]
260260
fn get_process_time(vm: &VirtualMachine) -> PyResult<Duration> {
261261
let t: libc::tms = unsafe {
262262
let mut t = std::mem::MaybeUninit::uninit();
@@ -265,17 +265,26 @@ mod time {
265265
}
266266
t.assume_init()
267267
};
268-
269-
#[cfg(target_os = "wasi")]
270-
let freq = 60;
271-
#[cfg(not(target_os = "wasi"))]
272268
let freq = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
273269

274270
Ok(Duration::from_nanos(
275271
time_muldiv(t.tms_utime, SEC_TO_NS, freq) + time_muldiv(t.tms_stime, SEC_TO_NS, freq),
276272
))
277273
}
278274

275+
// same as the get_process_time impl for most unixes
276+
#[cfg(all(target_arch = "wasm32", target_os = "wasi"))]
277+
pub(super) fn get_process_time(vm: &VirtualMachine) -> PyResult<Duration> {
278+
let time: libc::timespec = unsafe {
279+
let mut time = std::mem::MaybeUninit::uninit();
280+
if libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID, time.as_mut_ptr()) == -1 {
281+
return Err(vm.new_os_error("Failed to get clock time".to_owned()));
282+
}
283+
time.assume_init()
284+
};
285+
Ok(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))
286+
}
287+
279288
#[cfg(not(any(
280289
windows,
281290
target_os = "macos",

0 commit comments

Comments
 (0)