Skip to content

Commit cfe7c6b

Browse files
committed
sleep_until: improve maintainability
Co-authored-by: Jonas Böttiger <jonasboettiger@icloud.com> - use the internal platform specific Instant::CLOCK_ID - skip allow(unused) on on platform that uses it such that it can not become dead code - sleep_until: remove leftover links
1 parent 5065a35 commit cfe7c6b

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

library/std/src/sys/pal/unix/thread.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,12 @@ impl Thread {
326326
// If we're awoken with a signal and the return value is -1
327327
// clock_nanosleep needs to be called again.
328328
unsafe {
329-
while libc::clock_nanosleep(libc::CLOCK_MONOTONIC, libc::TIMER_ABSTIME, ts_ptr, ts_ptr)
330-
== -1
329+
while libc::clock_nanosleep(
330+
super::time::Instant::CLOCK_ID,
331+
libc::TIMER_ABSTIME,
332+
ts_ptr,
333+
ts_ptr,
334+
) == -1
331335
{
332336
assert_eq!(
333337
os::errno(),

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ pub struct Instant {
261261
}
262262

263263
impl Instant {
264+
#[cfg(target_vendor = "apple")]
265+
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
266+
#[cfg(not(target_vendor = "apple"))]
267+
pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_MONOTONIC;
264268
pub fn now() -> Instant {
265269
// https://www.manpagez.com/man/3/clock_gettime/
266270
//
@@ -273,11 +277,7 @@ impl Instant {
273277
//
274278
// Instant on macos was historically implemented using mach_absolute_time;
275279
// we preserve this value domain out of an abundance of caution.
276-
#[cfg(target_vendor = "apple")]
277-
const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
278-
#[cfg(not(target_vendor = "apple"))]
279-
const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;
280-
Instant { t: Timespec::now(clock_id) }
280+
Instant { t: Timespec::now(Self::CLOCK_ID) }
281281
}
282282

283283
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {

library/std/src/thread/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,6 @@ pub fn sleep(dur: Duration) {
920920
///
921921
/// [currently]: crate::io#platform-specific-behavior
922922
/// [clock_nanosleep]: https://linux.die.net/man/3/clock_nanosleep
923-
/// [subscription_clock]: https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-subscription_clock-record
924-
/// [mach_wait_until]: https://developer.apple.com/library/archive/technotes/tn2169/_index.html
925923
///
926924
/// **Disclaimer:** These system calls might change over time.
927925
///

library/std/src/time.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,11 @@ impl Instant {
408408
self.0.checked_sub_duration(&duration).map(Instant)
409409
}
410410

411-
// used by platform specific `sleep_until` implementations.
412-
#[allow(unused, reason = "not every platform has a specific `sleep_until`")]
411+
// Used by platform specific `sleep_until` implementations such as the one used on Linux.
412+
#[cfg_attr(
413+
not(target_os = "linux"),
414+
allow(unused, reason = "not every platform has a specific `sleep_until`")
415+
)]
413416
pub(crate) fn into_inner(self) -> time::Instant {
414417
self.0
415418
}

0 commit comments

Comments
 (0)