rustix/
clockid.rs

1use crate::backend::c;
2use crate::fd::BorrowedFd;
3
4/// `CLOCK_*` constants for use with [`clock_gettime`].
5///
6/// These constants are always supported at runtime, so `clock_gettime` never
7/// has to fail with `INVAL` due to an unsupported clock. See
8/// [`DynamicClockId`] for a greater set of clocks, with the caveat that not
9/// all of them are always supported.
10///
11/// [`clock_gettime`]: crate::time::clock_gettime
12#[cfg(not(any(apple, target_os = "wasi")))]
13#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
14#[cfg_attr(
15    not(any(target_os = "aix", target_os = "cygwin", target_os = "dragonfly")),
16    repr(i32)
17)]
18#[cfg_attr(any(target_os = "cygwin", target_os = "dragonfly"), repr(u64))]
19#[cfg_attr(target_os = "aix", repr(i64))]
20#[non_exhaustive]
21pub enum ClockId {
22    /// `CLOCK_REALTIME`
23    #[doc(alias = "CLOCK_REALTIME")]
24    Realtime = bitcast!(c::CLOCK_REALTIME),
25
26    /// `CLOCK_MONOTONIC`
27    #[doc(alias = "CLOCK_MONOTONIC")]
28    Monotonic = bitcast!(c::CLOCK_MONOTONIC),
29
30    /// `CLOCK_UPTIME`
31    ///
32    /// On FreeBSD, this is an alias for [`Self::Boottime`].
33    ///
34    /// On OpenBSD, this differs from `Self::Boottime`; it only advances when
35    /// the system is not suspended.
36    ///
37    /// [`Self::Uptime`]: https://docs.rs/rustix/*/x86_64-unknown-freebsd/rustix/time/enum.ClockId.html#variant.Uptime
38    #[cfg(any(freebsdlike, target_os = "openbsd"))]
39    #[doc(alias = "CLOCK_UPTIME")]
40    Uptime = c::CLOCK_UPTIME,
41
42    /// `CLOCK_PROCESS_CPUTIME_ID`
43    #[cfg(not(any(
44        solarish,
45        target_os = "horizon",
46        target_os = "netbsd",
47        target_os = "redox",
48        target_os = "vita"
49    )))]
50    #[doc(alias = "CLOCK_PROCESS_CPUTIME_ID")]
51    ProcessCPUTime = c::CLOCK_PROCESS_CPUTIME_ID,
52
53    /// `CLOCK_THREAD_CPUTIME_ID`
54    #[cfg(not(any(
55        solarish,
56        target_os = "horizon",
57        target_os = "netbsd",
58        target_os = "redox",
59        target_os = "vita"
60    )))]
61    #[doc(alias = "CLOCK_THREAD_CPUTIME_ID")]
62    ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
63
64    /// `CLOCK_REALTIME_COARSE`
65    #[cfg(any(linux_kernel, target_os = "freebsd"))]
66    #[doc(alias = "CLOCK_REALTIME_COARSE")]
67    RealtimeCoarse = c::CLOCK_REALTIME_COARSE,
68
69    /// `CLOCK_MONOTONIC_COARSE`
70    #[cfg(any(linux_kernel, target_os = "freebsd"))]
71    #[doc(alias = "CLOCK_MONOTONIC_COARSE")]
72    MonotonicCoarse = c::CLOCK_MONOTONIC_COARSE,
73
74    /// `CLOCK_MONOTONIC_RAW`
75    #[cfg(linux_kernel)]
76    #[doc(alias = "CLOCK_MONOTONIC_RAW")]
77    MonotonicRaw = c::CLOCK_MONOTONIC_RAW,
78
79    /// `CLOCK_REALTIME_ALARM`
80    #[cfg(linux_kernel)]
81    #[doc(alias = "CLOCK_REALTIME_ALARM")]
82    RealtimeAlarm = bitcast!(c::CLOCK_REALTIME_ALARM),
83
84    /// `CLOCK_TAI`, available on Linux ≥ 3.10
85    #[cfg(all(linux_kernel, feature = "linux_4_11"))]
86    #[doc(alias = "CLOCK_TAI")]
87    Tai = bitcast!(c::CLOCK_TAI),
88
89    /// `CLOCK_BOOTTIME`
90    #[cfg(any(linux_kernel, target_os = "fuchsia", target_os = "openbsd"))]
91    #[doc(alias = "CLOCK_BOOTTIME")]
92    Boottime = bitcast!(c::CLOCK_BOOTTIME),
93
94    /// `CLOCK_BOOTTIME_ALARM`
95    #[cfg(any(linux_kernel, target_os = "fuchsia"))]
96    #[doc(alias = "CLOCK_BOOTTIME_ALARM")]
97    BoottimeAlarm = bitcast!(c::CLOCK_BOOTTIME_ALARM),
98}
99
100/// `CLOCK_*` constants for use with [`clock_gettime`].
101///
102/// These constants are always supported at runtime, so `clock_gettime` never
103/// has to fail with `INVAL` due to an unsupported clock. See
104/// [`DynamicClockId`] for a greater set of clocks, with the caveat that not
105/// all of them are always supported.
106///
107/// [`clock_gettime`]: crate::time::clock_gettime
108#[cfg(apple)]
109#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
110#[repr(u32)]
111#[non_exhaustive]
112pub enum ClockId {
113    /// `CLOCK_REALTIME`
114    #[doc(alias = "CLOCK_REALTIME")]
115    Realtime = c::CLOCK_REALTIME,
116
117    /// `CLOCK_MONOTONIC`
118    #[doc(alias = "CLOCK_MONOTONIC")]
119    Monotonic = c::CLOCK_MONOTONIC,
120
121    /// `CLOCK_PROCESS_CPUTIME_ID`
122    #[doc(alias = "CLOCK_PROCESS_CPUTIME_ID")]
123    ProcessCPUTime = c::CLOCK_PROCESS_CPUTIME_ID,
124
125    /// `CLOCK_THREAD_CPUTIME_ID`
126    #[doc(alias = "CLOCK_THREAD_CPUTIME_ID")]
127    ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
128}
129
130/// `CLOCK_*` constants for use with [`clock_gettime_dynamic`].
131///
132/// These constants may be unsupported at runtime, depending on the OS version,
133/// and `clock_gettime_dynamic` may fail with `INVAL`. See [`ClockId`] for
134/// clocks which are always supported at runtime.
135///
136/// [`clock_gettime_dynamic`]: crate::time::clock_gettime_dynamic
137#[cfg(not(target_os = "wasi"))]
138#[derive(Debug, Copy, Clone)]
139#[non_exhaustive]
140pub enum DynamicClockId<'a> {
141    /// `ClockId` values that are always supported at runtime.
142    Known(ClockId),
143
144    /// Linux dynamic clocks.
145    Dynamic(BorrowedFd<'a>),
146
147    /// `CLOCK_REALTIME_ALARM`
148    #[cfg(linux_kernel)]
149    #[doc(alias = "CLOCK_REALTIME_ALARM")]
150    RealtimeAlarm,
151
152    /// `CLOCK_TAI`, available on Linux ≥ 3.10
153    #[cfg(linux_kernel)]
154    #[doc(alias = "CLOCK_TAI")]
155    Tai,
156
157    /// `CLOCK_BOOTTIME`
158    #[cfg(any(
159        linux_kernel,
160        target_os = "freebsd",
161        target_os = "fuchsia",
162        target_os = "openbsd"
163    ))]
164    #[doc(alias = "CLOCK_BOOTTIME")]
165    Boottime,
166
167    /// `CLOCK_BOOTTIME_ALARM`
168    #[cfg(any(linux_kernel, target_os = "fuchsia"))]
169    #[doc(alias = "CLOCK_BOOTTIME_ALARM")]
170    BoottimeAlarm,
171}