add realtime and monotonic clock support for wasi on windows

This commit is contained in:
Mark McCaskey 2019-05-21 10:24:06 -07:00
parent f3220a0435
commit 08b4b639f4
2 changed files with 36 additions and 2 deletions

View File

@ -19,6 +19,7 @@ log = "0.4.6"
byteorder = "1.3.1" byteorder = "1.3.1"
# hack to get tests to work # hack to get tests to work
wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true } wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true }
winapi = { version = "0.3.7", optional = true }
[build-dependencies] [build-dependencies]
glob = "0.2.11" glob = "0.2.11"

View File

@ -5,7 +5,21 @@ pub fn platform_clock_res_get(
clock_id: __wasi_clockid_t, clock_id: __wasi_clockid_t,
resolution: &Cell<__wasi_timestamp_t>, resolution: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t { ) -> __wasi_errno_t {
__WASI_EINVAL let resolution = match clock_id {
// resolution of monotonic clock at 10ms, from:
// https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-gettickcount64
__WASI_CLOCK_MONOTONIC => 10_000_000,
// TODO: verify or compute this
__WASI_CLOCK_REALTIME => 1,
__WASI_CLOCK_PROCESS_CPUTIME_ID => {
return __WASI_EINVAL;
}
__WASI_CLOCK_THREAD_CPUTIME_ID => {
return __WASI_EINVAL;
}
};
resolution.set(resolution);
__WASI_ESUCCESS
} }
pub fn platform_clock_time_get( pub fn platform_clock_time_get(
@ -13,5 +27,24 @@ pub fn platform_clock_time_get(
precision: __wasi_timestamp_t, precision: __wasi_timestamp_t,
time: &Cell<__wasi_timestamp_t>, time: &Cell<__wasi_timestamp_t>,
) -> __wasi_errno_t { ) -> __wasi_errno_t {
unimplemented!() let nanos = match clock_id {
__WASI_CLOCK_MONOTONIC => {
let tick_ms = winapi::um::sysinfoapi::GetTickCount64();
tick_ms * 1_000_000
}
__WASI_CLOCK_REALTIME => {
let duration = wasi_try!(std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok_or(__WASI_EIO));
duration.as_nanos() as u64
}
__WASI_CLOCK_PROCESS_CPUTIME_ID => {
unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_PROCESS_CPUTIME_ID, ..)")
}
__WASI_CLOCK_THREAD_CPUTIME_ID => {
unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_THREAD_CPUTIME_ID, ..)")
}
};
time.set(nanos);
__WASI_ESUCCESS
} }