Skip to content

Enable clock_gettime on wasi #2499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ fn main() {
println!("cargo:rustc-cfg=libc_cfg_target_vendor");
}

if rustc_minor_ver >= 51 || rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_ptr_addr_of");
}

// #[thread_local] is currently unstable
if rustc_dep_of_std {
println!("cargo:rustc-cfg=libc_thread_local");
Expand Down
16 changes: 16 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,19 @@ macro_rules! deprecated_mach {
)*
}
}

#[allow(unused_macros)]
#[cfg(not(libc_ptr_addr_of))]
macro_rules! ptr_addr_of {
($place:expr) => {
&$place
};
}

#[allow(unused_macros)]
#[cfg(libc_ptr_addr_of)]
macro_rules! ptr_addr_of {
($place:expr) => {
::core::ptr::addr_of!($place)
};
}
32 changes: 24 additions & 8 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ pub enum __locale_struct {}

pub type locale_t = *mut __locale_struct;

s_paren! {
// in wasi-libc clockid_t is const struct __clockid* (where __clockid is an opaque struct),
// but that's an implementation detail that we don't want to have to deal with
#[repr(transparent)]
pub struct clockid_t(*const u8);
}

unsafe impl Send for clockid_t {}
unsafe impl Sync for clockid_t {}

s! {
#[repr(align(8))]
pub struct fpos_t {
Expand Down Expand Up @@ -342,6 +352,13 @@ pub const _SC_PAGE_SIZE: ::c_int = _SC_PAGESIZE;
pub const _SC_IOV_MAX: c_int = 60;
pub const _SC_SYMLOOP_MAX: c_int = 173;

pub static CLOCK_MONOTONIC: clockid_t = unsafe { clockid_t(ptr_addr_of!(_CLOCK_MONOTONIC)) };
pub static CLOCK_PROCESS_CPUTIME_ID: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_PROCESS_CPUTIME_ID)) };
pub static CLOCK_REALTIME: clockid_t = unsafe { clockid_t(ptr_addr_of!(_CLOCK_REALTIME)) };
pub static CLOCK_THREAD_CPUTIME_ID: clockid_t =
unsafe { clockid_t(ptr_addr_of!(_CLOCK_THREAD_CPUTIME_ID)) };

#[cfg_attr(
feature = "rustc-dep-of-std",
link(name = "c", kind = "static", cfg(target_feature = "crt-static"))
Expand Down Expand Up @@ -417,15 +434,14 @@ extern "C" {
pub fn asctime_r(a: *const tm, b: *mut c_char) -> *mut c_char;
pub fn ctime_r(a: *const time_t, b: *mut c_char) -> *mut c_char;

static _CLOCK_MONOTONIC: u8;
static _CLOCK_PROCESS_CPUTIME_ID: u8;
static _CLOCK_REALTIME: u8;
static _CLOCK_THREAD_CPUTIME_ID: u8;
pub fn nanosleep(a: *const timespec, b: *mut timespec) -> c_int;
// pub fn clock_getres(a: clockid_t, b: *mut timespec) -> c_int;
// pub fn clock_gettime(a: clockid_t, b: *mut timespec) -> c_int;
// pub fn clock_nanosleep(
// a: clockid_t,
// a2: c_int,
// b: *const timespec,
// c: *mut timespec,
// ) -> c_int;
pub fn clock_getres(a: clockid_t, b: *mut timespec) -> c_int;
pub fn clock_gettime(a: clockid_t, b: *mut timespec) -> c_int;
pub fn clock_nanosleep(a: clockid_t, a2: c_int, b: *const timespec, c: *mut timespec) -> c_int;

pub fn isalnum(c: c_int) -> c_int;
pub fn isalpha(c: c_int) -> c_int;
Expand Down