Skip to content

Commit 5502733

Browse files
committed
std: simplify UNIX parker timeouts
1 parent 54daf49 commit 5502733

File tree

2 files changed

+21
-59
lines changed

2 files changed

+21
-59
lines changed

library/std/src/sys/unix/thread_parker.rs

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,52 +41,18 @@ unsafe fn wait(cond: *mut libc::pthread_cond_t, lock: *mut libc::pthread_mutex_t
4141
const TIMESPEC_MAX: libc::timespec =
4242
libc::timespec { tv_sec: <libc::time_t>::MAX, tv_nsec: 1_000_000_000 - 1 };
4343

44-
fn saturating_cast_to_time_t(value: u64) -> libc::time_t {
45-
if value > <libc::time_t>::MAX as u64 { <libc::time_t>::MAX } else { value as libc::time_t }
46-
}
47-
48-
// This implementation is used on systems that support pthread_condattr_setclock
49-
// where we configure the condition variable to use the monotonic clock (instead of
50-
// the default system clock). This approach avoids all problems that result
51-
// from changes made to the system time.
52-
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "espidf")))]
5344
unsafe fn wait_timeout(
5445
cond: *mut libc::pthread_cond_t,
5546
lock: *mut libc::pthread_mutex_t,
5647
dur: Duration,
5748
) {
58-
use crate::mem;
59-
60-
let mut now: libc::timespec = mem::zeroed();
61-
let r = libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
62-
assert_eq!(r, 0);
63-
// Nanosecond calculations can't overflow because both values are below 1e9.
64-
let nsec = dur.subsec_nanos() + now.tv_nsec as u32;
65-
let sec = saturating_cast_to_time_t(dur.as_secs())
66-
.checked_add((nsec / 1_000_000_000) as libc::time_t)
67-
.and_then(|s| s.checked_add(now.tv_sec));
68-
let nsec = nsec % 1_000_000_000;
69-
let timeout =
70-
sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX);
71-
let r = libc::pthread_cond_timedwait(cond, lock, &timeout);
72-
assert!(r == libc::ETIMEDOUT || r == 0);
73-
}
49+
// Use the system clock on systems that do not support pthread_condattr_setclock.
50+
// This unfortunately results in problems when the system time changes.
51+
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "espidf"))]
52+
let (now, dur) = {
53+
use super::time::SystemTime;
54+
use crate::cmp::min;
7455

75-
// This implementation is modeled after libcxx's condition_variable
76-
// https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46
77-
// https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367
78-
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "espidf"))]
79-
unsafe fn wait_timeout(
80-
cond: *mut libc::pthread_cond_t,
81-
lock: *mut libc::pthread_mutex_t,
82-
mut dur: Duration,
83-
) {
84-
use crate::ptr;
85-
86-
// 1000 years
87-
let max_dur = Duration::from_secs(1000 * 365 * 86400);
88-
89-
if dur > max_dur {
9056
// OSX implementation of `pthread_cond_timedwait` is buggy
9157
// with super long durations. When duration is greater than
9258
// 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
@@ -98,23 +64,19 @@ unsafe fn wait_timeout(
9864
// To work around this issue, and possible bugs of other OSes, timeout
9965
// is clamped to 1000 years, which is allowable per the API of `park_timeout`
10066
// because of spurious wakeups.
101-
dur = max_dur;
102-
}
103-
104-
let mut sys_now = libc::timeval { tv_sec: 0, tv_usec: 0 };
105-
let r = libc::gettimeofday(&mut sys_now, ptr::null_mut());
106-
debug_assert_eq!(r, 0);
107-
let nsec = dur.subsec_nanos() as libc::c_long + (sys_now.tv_usec * 1000) as libc::c_long;
108-
let extra = (nsec / 1_000_000_000) as libc::time_t;
109-
let nsec = nsec % 1_000_000_000;
110-
let seconds = saturating_cast_to_time_t(dur.as_secs());
111-
let timeout = sys_now
112-
.tv_sec
113-
.checked_add(extra)
114-
.and_then(|s| s.checked_add(seconds))
115-
.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec })
116-
.unwrap_or(TIMESPEC_MAX);
117-
// And wait!
67+
let dur = min(dur, Duration::from_secs(1000 * 365 * 86400));
68+
let now = SystemTime::now().t;
69+
(now, dur)
70+
};
71+
// Use the monotonic clock on other systems.
72+
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "espidf")))]
73+
let (now, dur) = {
74+
use super::time::Timespec;
75+
76+
(Timespec::now(libc::CLOCK_MONOTONIC), dur)
77+
};
78+
79+
let timeout = now.checked_add_duration(&dur).map(|t| t.t).unwrap_or(TIMESPEC_MAX);
11880
let r = libc::pthread_cond_timedwait(cond, lock, &timeout);
11981
debug_assert!(r == libc::ETIMEDOUT || r == 0);
12082
}

library/std/src/sys/unix/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod inner {
132132

133133
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
134134
pub struct SystemTime {
135-
t: Timespec,
135+
pub(in crate::sys::unix) t: Timespec,
136136
}
137137

138138
pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
@@ -279,7 +279,7 @@ mod inner {
279279

280280
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
281281
pub struct SystemTime {
282-
t: Timespec,
282+
pub(in crate::sys::unix) t: Timespec,
283283
}
284284

285285
pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };

0 commit comments

Comments
 (0)