Implement clock_time_get on Windows (#119)
* Fix some Windows warnings. * Implement clock_time_get on Windows. Also update misc_testsuite to include latest clock_time_get test changes. * improve comments * Remove a leftover import. Co-Authored-By: Jakub Konka <kubkon@jakubkonka.com>
This commit is contained in:
committed by
Jakub Konka
parent
bb5c879718
commit
2659641132
@@ -1,6 +1,7 @@
|
||||
use crate::{Error, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
|
||||
st.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| Error::EINVAL)? // date earlier than UNIX_EPOCH
|
||||
|
||||
@@ -226,9 +226,14 @@ pub(crate) const RIGHTS_ALL: __wasi_rights_t = __WASI_RIGHT_FD_DATASYNC
|
||||
|
||||
// Block and character device interaction is outside the scope of
|
||||
// WASI. Simply allow everything.
|
||||
// Those constants are unused on Windows
|
||||
#[allow(unused)]
|
||||
pub(crate) const RIGHTS_BLOCK_DEVICE_BASE: __wasi_rights_t = RIGHTS_ALL;
|
||||
#[allow(unused)]
|
||||
pub(crate) const RIGHTS_BLOCK_DEVICE_INHERITING: __wasi_rights_t = RIGHTS_ALL;
|
||||
#[allow(unused)]
|
||||
pub(crate) const RIGHTS_CHARACTER_DEVICE_BASE: __wasi_rights_t = RIGHTS_ALL;
|
||||
#[allow(unused)]
|
||||
pub(crate) const RIGHTS_CHARACTER_DEVICE_INHERITING: __wasi_rights_t = RIGHTS_ALL;
|
||||
|
||||
// Only allow directory operations on directories. Directories can only
|
||||
|
||||
@@ -1,18 +1,33 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
#![allow(unused)]
|
||||
use crate::helpers::systemtime_to_timestamp;
|
||||
use crate::memory::*;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, wasm32, Result};
|
||||
use crate::{host, wasm32, Error, Result};
|
||||
use cpu_time::{ProcessTime, ThreadTime};
|
||||
use lazy_static::lazy_static;
|
||||
use std::convert::TryInto;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
lazy_static! {
|
||||
static ref START_MONOTONIC: Instant = Instant::now();
|
||||
}
|
||||
|
||||
use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
unimplemented!("clock_res_get")
|
||||
}
|
||||
|
||||
pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
unimplemented!("clock_time_get")
|
||||
let duration = match clock_id {
|
||||
host::__WASI_CLOCK_REALTIME => get_monotonic_time(),
|
||||
host::__WASI_CLOCK_MONOTONIC => get_realtime_time()?,
|
||||
host::__WASI_CLOCK_PROCESS_CPUTIME_ID => get_proc_cputime()?,
|
||||
host::__WASI_CLOCK_THREAD_CPUTIME_ID => get_thread_cputime()?,
|
||||
_ => return Err(Error::EINVAL),
|
||||
};
|
||||
duration.as_nanos().try_into().map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn poll_oneoff(
|
||||
@@ -21,3 +36,27 @@ pub(crate) fn poll_oneoff(
|
||||
) -> Result<wasm32::size_t> {
|
||||
unimplemented!("poll_oneoff")
|
||||
}
|
||||
|
||||
fn get_monotonic_time() -> Duration {
|
||||
// We're circumventing the fact that we can't get a Duration from an Instant
|
||||
// The epoch of __WASI_CLOCK_MONOTONIC is undefined, so we fix a time point once
|
||||
// and count relative to this time point.
|
||||
//
|
||||
// The alternative would be to copy over the implementation of std::time::Instant
|
||||
// to our source tree and add a conversion to std::time::Duration
|
||||
START_MONOTONIC.elapsed()
|
||||
}
|
||||
|
||||
fn get_realtime_time() -> Result<Duration> {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| Error::EFAULT)
|
||||
}
|
||||
|
||||
fn get_proc_cputime() -> Result<Duration> {
|
||||
Ok(ProcessTime::try_now()?.as_duration())
|
||||
}
|
||||
|
||||
fn get_thread_cputime() -> Result<Duration> {
|
||||
Ok(ThreadTime::try_now()?.as_duration())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user