Skip to content

Commit 6b4166b

Browse files
committed
more default sleep_until implementations
1 parent 291e460 commit 6b4166b

File tree

10 files changed

+49
-33
lines changed

10 files changed

+49
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Thread {
9090
let now = Instant::now();
9191

9292
if let Some(delay) = deadline.checked_duration_since(now) {
93-
sleep(delay);
93+
Self::sleep(delay);
9494
}
9595
}
9696

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::mem::ManuallyDrop;
1010
use crate::num::NonZero;
1111
use crate::ptr::NonNull;
1212
use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
13-
use crate::time::Duration;
13+
use crate::time::{Duration, Instant};
1414
use crate::{hint, io};
1515

1616
pub struct Thread {
@@ -209,7 +209,7 @@ impl Thread {
209209
let now = Instant::now();
210210

211211
if let Some(delay) = deadline.checked_duration_since(now) {
212-
sleep(delay);
212+
Self::sleep(delay);
213213
}
214214
}
215215

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Thread {
136136
let now = Instant::now();
137137

138138
if let Some(delay) = deadline.checked_duration_since(now) {
139-
sleep(delay);
139+
Self::sleep(delay);
140140
}
141141
}
142142

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Thread {
4343
let now = Instant::now();
4444

4545
if let Some(delay) = deadline.checked_duration_since(now) {
46-
sleep(delay);
46+
Self::sleep(delay);
4747
}
4848
}
4949

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::sys::weak::dlsym;
66
#[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "nto",))]
77
use crate::sys::weak::weak;
88
use crate::sys::{os, stack_overflow};
9-
use crate::time::Duration;
9+
use crate::time::{Duration, Instant};
1010
use crate::{cmp, io, ptr};
1111
#[cfg(not(any(
1212
target_os = "l4re",
@@ -296,27 +296,7 @@ impl Thread {
296296
}
297297
}
298298

299-
#[cfg(not(any(
300-
target_os = "freebsd",
301-
target_os = "netbsd",
302-
target_os = "linux",
303-
target_os = "android",
304-
target_os = "solaris",
305-
target_os = "illumos",
306-
target_os = "dragonfly",
307-
target_os = "hurd",
308-
target_os = "fuchsia",
309-
target_os = "vxworks",
310-
)))]
311-
pub fn sleep_until(deadline: Instant) {
312-
let now = Instant::now();
313-
314-
if let Some(delay) = deadline.checked_duration_since(now) {
315-
sleep(delay);
316-
}
317-
}
318-
319-
// Note depends on clock_nanosleep (not supported on os's by apple)
299+
// Any unix that has clock_nanosleep
320300
#[cfg(any(
321301
target_os = "freebsd",
322302
target_os = "netbsd",
@@ -329,7 +309,7 @@ impl Thread {
329309
target_os = "fuchsia",
330310
target_os = "vxworks",
331311
))]
332-
pub fn sleep_until(deadline: crate::time::Instant) {
312+
pub fn sleep_until(deadline: Instant) {
333313
let mut ts = deadline
334314
.into_inner()
335315
.into_timespec()
@@ -352,6 +332,27 @@ impl Thread {
352332
}
353333
}
354334

335+
// Any unix that does not have clock_nanosleep
336+
#[cfg(not(any(
337+
target_os = "freebsd",
338+
target_os = "netbsd",
339+
target_os = "linux",
340+
target_os = "android",
341+
target_os = "solaris",
342+
target_os = "illumos",
343+
target_os = "dragonfly",
344+
target_os = "hurd",
345+
target_os = "fuchsia",
346+
target_os = "vxworks",
347+
)))]
348+
pub fn sleep_until(deadline: Instant) {
349+
let now = Instant::now();
350+
351+
if let Some(delay) = deadline.checked_duration_since(now) {
352+
Self::sleep(delay);
353+
}
354+
}
355+
355356
pub fn join(self) {
356357
let id = self.into_id();
357358
let ret = unsafe { libc::pthread_join(id, ptr::null_mut()) };

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ impl Instant {
292292
Some(Instant { t: self.t.checked_sub_duration(other)? })
293293
}
294294

295+
// reason for allow(unused): this is needed by the `sleep_until` implementation
296+
// for some unix platforms not all.
297+
#[allow(unused)]
295298
pub(crate) fn into_timespec(self) -> Timespec {
296299
self.t
297300
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ impl Thread {
2626
panic!("can't sleep");
2727
}
2828

29+
pub fn sleep_until(_deadline: Instant) {
30+
panic!("can't sleep");
31+
}
32+
2933
pub fn join(self) {
3034
self.0
3135
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::ffi::CStr;
44
use crate::num::NonZero;
5-
use crate::time::Duration;
5+
use crate::time::{Duration, Instant};
66
use crate::{io, mem};
77

88
cfg_if::cfg_if! {
@@ -175,7 +175,7 @@ impl Thread {
175175
let now = Instant::now();
176176

177177
if let Some(delay) = deadline.checked_duration_since(now) {
178-
sleep(delay);
178+
Self::sleep(delay);
179179
}
180180
}
181181

library/std/src/sys/pal/wasm/atomics/thread.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ffi::CStr;
22
use crate::io;
33
use crate::num::NonZero;
44
use crate::sys::unsupported;
5-
use crate::time::Duration;
5+
use crate::time::{Duration, Instant};
66

77
pub struct Thread(!);
88

@@ -41,6 +41,14 @@ impl Thread {
4141
}
4242
}
4343

44+
pub fn sleep_until(deadline: Instant) {
45+
let now = Instant::now();
46+
47+
if let Some(delay) = deadline.checked_duration_since(now) {
48+
Self::sleep(delay);
49+
}
50+
}
51+
4452
pub fn join(self) {}
4553
}
4654

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::os::xous::ffi::{
88
map_memory, update_memory_flags,
99
};
1010
use crate::os::xous::services::{TicktimerScalar, ticktimer_server};
11-
use crate::time::Duration;
11+
use crate::time::{Duration, Instant};
1212

1313
pub struct Thread {
1414
tid: ThreadId,
@@ -132,7 +132,7 @@ impl Thread {
132132
let now = Instant::now();
133133

134134
if let Some(delay) = deadline.checked_duration_since(now) {
135-
sleep(delay);
135+
Self::sleep(delay);
136136
}
137137
}
138138

0 commit comments

Comments
 (0)