Skip to content

Commit 072d6de

Browse files
committed
Auto merge of #2499 - coolreader18:wasi-clock_gettime, r=Amanieu
Enable clock_gettime on wasi I think this mostly addresses the issues that were brought up in #1307 regarding clock ids; I made clockid_t a wrapper struct because it needs to be Send and Sync to be used in a constant, and I figured it was opaque in wasi-libc anyway. Instead of static ZSTs, I just made them `u8`s, since I figured it's very unlikely that wasi-libc would change them from `struct __clockid { __wasi_clockid_t id; }` to `struct __clockid {}`, and it'll always be valid to treat a static as a `/(u8)+/`. One thing I was wondering about, should I add a cfg check to `build.rs` checking for `core::ptr::addr_of`? Since I *think*(?) using that would fix any issue of __clockid becoming a "zst" struct in the future.
2 parents d5401c9 + cd57a93 commit 072d6de

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ fn main() {
7272
println!("cargo:rustc-cfg=libc_cfg_target_vendor");
7373
}
7474

75+
if rustc_minor_ver >= 51 || rustc_dep_of_std {
76+
println!("cargo:rustc-cfg=libc_ptr_addr_of");
77+
}
78+
7579
// #[thread_local] is currently unstable
7680
if rustc_dep_of_std {
7781
println!("cargo:rustc-cfg=libc_thread_local");

src/macros.rs

+16
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,19 @@ macro_rules! deprecated_mach {
341341
)*
342342
}
343343
}
344+
345+
#[allow(unused_macros)]
346+
#[cfg(not(libc_ptr_addr_of))]
347+
macro_rules! ptr_addr_of {
348+
($place:expr) => {
349+
&$place
350+
};
351+
}
352+
353+
#[allow(unused_macros)]
354+
#[cfg(libc_ptr_addr_of)]
355+
macro_rules! ptr_addr_of {
356+
($place:expr) => {
357+
::core::ptr::addr_of!($place)
358+
};
359+
}

src/wasi.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ pub enum __locale_struct {}
5151

5252
pub type locale_t = *mut __locale_struct;
5353

54+
s_paren! {
55+
// in wasi-libc clockid_t is const struct __clockid* (where __clockid is an opaque struct),
56+
// but that's an implementation detail that we don't want to have to deal with
57+
#[repr(transparent)]
58+
pub struct clockid_t(*const u8);
59+
}
60+
61+
unsafe impl Send for clockid_t {}
62+
unsafe impl Sync for clockid_t {}
63+
5464
s! {
5565
#[repr(align(8))]
5666
pub struct fpos_t {
@@ -342,6 +352,13 @@ pub const _SC_PAGE_SIZE: ::c_int = _SC_PAGESIZE;
342352
pub const _SC_IOV_MAX: c_int = 60;
343353
pub const _SC_SYMLOOP_MAX: c_int = 173;
344354

355+
pub static CLOCK_MONOTONIC: clockid_t = unsafe { clockid_t(ptr_addr_of!(_CLOCK_MONOTONIC)) };
356+
pub static CLOCK_PROCESS_CPUTIME_ID: clockid_t =
357+
unsafe { clockid_t(ptr_addr_of!(_CLOCK_PROCESS_CPUTIME_ID)) };
358+
pub static CLOCK_REALTIME: clockid_t = unsafe { clockid_t(ptr_addr_of!(_CLOCK_REALTIME)) };
359+
pub static CLOCK_THREAD_CPUTIME_ID: clockid_t =
360+
unsafe { clockid_t(ptr_addr_of!(_CLOCK_THREAD_CPUTIME_ID)) };
361+
345362
#[cfg_attr(
346363
feature = "rustc-dep-of-std",
347364
link(name = "c", kind = "static", cfg(target_feature = "crt-static"))
@@ -417,15 +434,14 @@ extern "C" {
417434
pub fn asctime_r(a: *const tm, b: *mut c_char) -> *mut c_char;
418435
pub fn ctime_r(a: *const time_t, b: *mut c_char) -> *mut c_char;
419436

437+
static _CLOCK_MONOTONIC: u8;
438+
static _CLOCK_PROCESS_CPUTIME_ID: u8;
439+
static _CLOCK_REALTIME: u8;
440+
static _CLOCK_THREAD_CPUTIME_ID: u8;
420441
pub fn nanosleep(a: *const timespec, b: *mut timespec) -> c_int;
421-
// pub fn clock_getres(a: clockid_t, b: *mut timespec) -> c_int;
422-
// pub fn clock_gettime(a: clockid_t, b: *mut timespec) -> c_int;
423-
// pub fn clock_nanosleep(
424-
// a: clockid_t,
425-
// a2: c_int,
426-
// b: *const timespec,
427-
// c: *mut timespec,
428-
// ) -> c_int;
442+
pub fn clock_getres(a: clockid_t, b: *mut timespec) -> c_int;
443+
pub fn clock_gettime(a: clockid_t, b: *mut timespec) -> c_int;
444+
pub fn clock_nanosleep(a: clockid_t, a2: c_int, b: *const timespec, c: *mut timespec) -> c_int;
429445

430446
pub fn isalnum(c: c_int) -> c_int;
431447
pub fn isalpha(c: c_int) -> c_int;

0 commit comments

Comments
 (0)