Skip to content
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

Replace Linux Mutex and Condvar with futex based ones. #95035

Merged
merged 14 commits into from
Apr 5, 2022
Merged
Prev Previous commit
Next Next commit
Make Timespec available in sys::unix.
  • Loading branch information
m-ou-se committed Mar 24, 2022
commit 23badeb4cb22afd09e0264348800e44cda80af38
28 changes: 13 additions & 15 deletions library/std/src/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use crate::convert::TryInto;
const NSEC_PER_SEC: u64 = 1_000_000_000;

#[derive(Copy, Clone)]
struct Timespec {
t: libc::timespec,
pub(in crate::sys::unix) struct Timespec {
pub t: libc::timespec,
}

impl Timespec {
const fn zero() -> Timespec {
Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }
}

fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
if self >= other {
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
// to optimize it into a branchless form (see also #75545):
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Timespec {
}
}

fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
let mut secs = other
.as_secs()
.try_into() // <- target type would be `libc::time_t`
Expand All @@ -68,7 +68,7 @@ impl Timespec {
Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } })
}

fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
let mut secs = other
.as_secs()
.try_into() // <- target type would be `libc::time_t`
Expand Down Expand Up @@ -285,7 +285,7 @@ mod inner {

impl Instant {
pub fn now() -> Instant {
Instant { t: now(libc::CLOCK_MONOTONIC) }
Instant { t: Timespec::now(libc::CLOCK_MONOTONIC) }
}

pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
Expand All @@ -299,10 +299,6 @@ mod inner {
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant { t: self.t.checked_sub_duration(other)? })
}

pub(in crate::sys::unix) fn as_timespec(&self) -> libc::timespec {
self.t.t
}
}

impl fmt::Debug for Instant {
Expand All @@ -316,7 +312,7 @@ mod inner {

impl SystemTime {
pub fn now() -> SystemTime {
SystemTime { t: now(libc::CLOCK_REALTIME) }
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
}

pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
Expand Down Expand Up @@ -352,9 +348,11 @@ mod inner {
#[cfg(any(target_os = "dragonfly", target_os = "espidf"))]
pub type clock_t = libc::c_ulong;

fn now(clock: clock_t) -> Timespec {
let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } };
cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap();
t
impl Timespec {
pub fn now(clock: clock_t) -> Timespec {
let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } };
m-ou-se marked this conversation as resolved.
Show resolved Hide resolved
cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap();
t
}
}
}