From 28d9d1fe8784b23dae13b6104721a35abe126f49 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Fri, 29 Mar 2019 12:39:48 -0700 Subject: [PATCH] move linux impl to unix (it works on osx too!) --- lib/wasi/src/syscalls/unix/linux.rs | 61 ----------------------------- lib/wasi/src/syscalls/unix/macos.rs | 16 -------- lib/wasi/src/syscalls/unix/mod.rs | 60 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 77 deletions(-) diff --git a/lib/wasi/src/syscalls/unix/linux.rs b/lib/wasi/src/syscalls/unix/linux.rs index 8cc6320c6..8b1378917 100644 --- a/lib/wasi/src/syscalls/unix/linux.rs +++ b/lib/wasi/src/syscalls/unix/linux.rs @@ -1,62 +1 @@ -use crate::syscalls::types::*; -use libc::{ - clock_getres, clock_gettime, timespec, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, - CLOCK_REALTIME, CLOCK_THREAD_CPUTIME_ID, -}; -use std::cell::Cell; -use std::mem; -pub fn platform_clock_res_get( - clock_id: __wasi_clockid_t, - resolution: &Cell<__wasi_timestamp_t>, -) -> __wasi_errno_t { - let linux_clock_id = match clock_id { - __WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC, - __WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID, - __WASI_CLOCK_REALTIME => CLOCK_REALTIME, - __WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID, - _ => return __WASI_EINVAL, - }; - - let (output, timespec_out) = unsafe { - let mut timespec_out: timespec = mem::uninitialized(); - ( - clock_getres(linux_clock_id, &mut timespec_out), - timespec_out, - ) - }; - - resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t); - - // TODO: map output of clock_getres to __wasi_errno_t - __WASI_ESUCCESS -} - -pub fn platform_clock_time_get( - clock_id: __wasi_clockid_t, - precision: __wasi_timestamp_t, - time: &Cell<__wasi_timestamp_t>, -) -> __wasi_errno_t { - let linux_clock_id = match clock_id { - __WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC, - __WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID, - __WASI_CLOCK_REALTIME => CLOCK_REALTIME, - __WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID, - _ => return __WASI_EINVAL, - }; - - let (output, timespec_out) = unsafe { - let mut timespec_out: timespec = mem::uninitialized(); - ( - clock_gettime(linux_clock_id, &mut timespec_out), - timespec_out, - ) - }; - - // TODO: adjust output by precision... - - time.set(timespec_out.tv_nsec as __wasi_timestamp_t); - - // TODO: map output of clock_gettime to __wasi_errno_t - __WASI_ESUCCESS -} diff --git a/lib/wasi/src/syscalls/unix/macos.rs b/lib/wasi/src/syscalls/unix/macos.rs index 6273695d3..8b1378917 100644 --- a/lib/wasi/src/syscalls/unix/macos.rs +++ b/lib/wasi/src/syscalls/unix/macos.rs @@ -1,17 +1 @@ -use crate::syscalls::types::*; -use std::cell::Cell; -pub fn platform_clock_res_get( - clock_id: __wasi_clockid_t, - resolution: &Cell<__wasi_timestamp_t>, -) -> __wasi_errno_t { - __WASI_EINVAL -} - -pub fn platform_clock_time_get( - clock_id: __wasi_clockid_t, - precision: __wasi_timestamp_t, - time: &Cell<__wasi_timestamp_t>, -) -> __wasi_errno_t { - unimplemented!() -} diff --git a/lib/wasi/src/syscalls/unix/mod.rs b/lib/wasi/src/syscalls/unix/mod.rs index 8fab04668..915699c24 100644 --- a/lib/wasi/src/syscalls/unix/mod.rs +++ b/lib/wasi/src/syscalls/unix/mod.rs @@ -9,3 +9,63 @@ pub use linux::*; #[cfg(target_os = "macos")] pub use macos::*; + +use crate::syscalls::types::*; +use libc::{ + clock_getres, clock_gettime, timespec, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, + CLOCK_REALTIME, CLOCK_THREAD_CPUTIME_ID, +}; +use std::cell::Cell; +use std::mem; + +pub fn platform_clock_res_get( + clock_id: __wasi_clockid_t, + resolution: &Cell<__wasi_timestamp_t>, +) -> __wasi_errno_t { + let unix_clock_id = match clock_id { + __WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC, + __WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID, + __WASI_CLOCK_REALTIME => CLOCK_REALTIME, + __WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID, + _ => return __WASI_EINVAL, + }; + + let (output, timespec_out) = unsafe { + let mut timespec_out: timespec = mem::uninitialized(); + (clock_getres(unix_clock_id, &mut timespec_out), timespec_out) + }; + + resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t); + + // TODO: map output of clock_getres to __wasi_errno_t + __WASI_ESUCCESS +} + +pub fn platform_clock_time_get( + clock_id: __wasi_clockid_t, + precision: __wasi_timestamp_t, + time: &Cell<__wasi_timestamp_t>, +) -> __wasi_errno_t { + let unix_clock_id = match clock_id { + __WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC, + __WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID, + __WASI_CLOCK_REALTIME => CLOCK_REALTIME, + __WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID, + _ => return __WASI_EINVAL, + }; + + let (output, timespec_out) = unsafe { + let mut timespec_out: timespec = mem::uninitialized(); + ( + clock_gettime(unix_clock_id, &mut timespec_out), + timespec_out, + ) + }; + + // TODO: adjust output by precision... + + time.set(timespec_out.tv_nsec as __wasi_timestamp_t); + + // TODO: map output of clock_gettime to __wasi_errno_t + __WASI_ESUCCESS +}