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