1 use crate::{backend, io};
2 
3 pub use crate::timespec::Timespec;
4 
5 #[cfg(not(any(
6     apple,
7     target_os = "dragonfly",
8     target_os = "espidf",
9     target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
10     target_os = "openbsd",
11     target_os = "redox",
12     target_os = "vita",
13     target_os = "wasi",
14 )))]
15 pub use crate::clockid::ClockId;
16 
17 /// `clock_nanosleep(id, 0, request, remain)`—Sleeps for a duration on a
18 /// given clock.
19 ///
20 /// This is `clock_nanosleep` specialized for the case of a relative sleep
21 /// interval. See [`clock_nanosleep_absolute`] for absolute intervals.
22 ///
23 /// # References
24 ///  - [POSIX]
25 ///  - [Linux]
26 ///
27 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_nanosleep.html
28 /// [Linux]: https://man7.org/linux/man-pages/man2/clock_nanosleep.2.html
29 #[cfg(not(any(
30     apple,
31     target_os = "dragonfly",
32     target_os = "emscripten",
33     target_os = "espidf",
34     target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
35     target_os = "haiku",
36     target_os = "openbsd",
37     target_os = "redox",
38     target_os = "vita",
39     target_os = "wasi",
40 )))]
41 #[inline]
clock_nanosleep_relative(id: ClockId, request: &Timespec) -> NanosleepRelativeResult42 pub fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> NanosleepRelativeResult {
43     backend::thread::syscalls::clock_nanosleep_relative(id, request)
44 }
45 
46 /// `clock_nanosleep(id, TIMER_ABSTIME, request, NULL)`—Sleeps until an
47 /// absolute time on a given clock.
48 ///
49 /// This is `clock_nanosleep` specialized for the case of an absolute sleep
50 /// interval. See [`clock_nanosleep_relative`] for relative intervals.
51 ///
52 /// # References
53 ///  - [POSIX]
54 ///  - [Linux]
55 ///
56 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_nanosleep.html
57 /// [Linux]: https://man7.org/linux/man-pages/man2/clock_nanosleep.2.html
58 #[cfg(not(any(
59     apple,
60     target_os = "dragonfly",
61     target_os = "emscripten",
62     target_os = "espidf",
63     target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
64     target_os = "haiku",
65     target_os = "openbsd",
66     target_os = "redox",
67     target_os = "vita",
68     target_os = "wasi",
69 )))]
70 #[inline]
clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::Result<()>71 pub fn clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::Result<()> {
72     backend::thread::syscalls::clock_nanosleep_absolute(id, request)
73 }
74 
75 /// `nanosleep(request, remain)`—Sleeps for a duration.
76 ///
77 /// This effectively uses the system monotonic clock.
78 ///
79 /// # References
80 ///  - [POSIX]
81 ///  - [Linux]
82 ///
83 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html
84 /// [Linux]: https://man7.org/linux/man-pages/man2/nanosleep.2.html
85 #[inline]
nanosleep(request: &Timespec) -> NanosleepRelativeResult86 pub fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
87     backend::thread::syscalls::nanosleep(request)
88 }
89 
90 /// A return type for `nanosleep` and `clock_nanosleep_relative`.
91 #[derive(Debug, Clone)]
92 #[must_use]
93 pub enum NanosleepRelativeResult {
94     /// The sleep completed normally.
95     Ok,
96     /// The sleep was interrupted, the remaining time is returned.
97     Interrupted(Timespec),
98     /// An invalid time value was provided.
99     Err(io::Errno),
100 }
101