Allow selecting a clock source for timers by passing a `ClockSource`
variant to `Timer::new`.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/hrtimer.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 50 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/hrtimer.rs b/rust/kernel/hrtimer.rs
index 1674d1dcba39cc7ab82e1f189002afa365ee9341..f3d57c6cb1a95075c28192b5cd2f97431989a0b1 100644
--- a/rust/kernel/hrtimer.rs
+++ b/rust/kernel/hrtimer.rs
@@ -57,7 +57,7 @@ unsafe impl<U> Sync for Timer<U> {}
impl<T> Timer<T> {
/// Return an initializer for a new timer instance.
- pub fn new(mode: TimerMode) -> impl PinInit<Self>
+ pub fn new(mode: TimerMode, clock: ClockSource) -> impl PinInit<Self>
where
T: TimerCallback,
{
@@ -70,7 +70,7 @@ pub fn new(mode: TimerMode) -> impl PinInit<Self>
unsafe {
bindings::hrtimer_init(
place,
- bindings::CLOCK_MONOTONIC as i32,
+ clock.into(),
mode.into(),
);
}
@@ -442,6 +442,54 @@ fn from(value: TimerMode) -> Self {
}
}
+/// The clock source to use for a [`Timer`].
+pub enum ClockSource {
+ /// A settable system-wide clock that measures real (i.e., wall-clock) time.
+ /// Setting this clock requires appropriate privileges. This clock is
+ /// affected by discontinuous jumps in the system time (e.g., if the system
+ /// administrator manually changes the clock), and by frequency adjustments
+ /// performed by NTP and similar applications via adjtime(3), adjtimex(2),
+ /// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts the
+ /// number of seconds since 1970-01-01 00:00:00 Coordinated Universal Time
+ /// (UTC) except that it ignores leap seconds; near a leap second it is
+ /// typically adjusted by NTP to stay roughly in sync with UTC.
+ RealTime,
+ /// A nonsettable system-wide clock that represents monotonic time since—as
+ /// described by POSIX—"some unspecified point in the past". On Linux, that
+ /// point corresponds to the number of seconds that the system has been
+ /// running since it was booted.
+ ///
+ /// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps in the
+ /// system time (e.g., if the system administrator manually changes the
+ /// clock), but is affected by frequency adjustments. This clock does not
+ /// count time that the system is suspended.
+ Monotonic,
+ /// A nonsettable system-wide clock that is identical to CLOCK_MONOTONIC,
+ /// except that it also includes any time that the system is suspended. This
+ /// allows applications to get a suspend-aware monotonic clock without
+ /// having to deal with the complications of CLOCK_REALTIME, which may have
+ /// discontinuities if the time is changed using settimeofday(2) or similar.
+ BootTime,
+ /// A nonsettable system-wide clock derived from wall-clock time but
+ /// counting leap seconds. This clock does not experience discontinuities or
+ /// frequency adjustments caused by inserting leap seconds as CLOCK_REALTIME
+ /// does.
+ ///
+ /// The acronym TAI refers to International Atomic Time.
+ TAI,
+}
+
+impl From<ClockSource> for bindings::clockid_t {
+ fn from(value: ClockSource) -> Self {
+ match value {
+ ClockSource::RealTime => bindings::CLOCK_REALTIME as i32,
+ ClockSource::Monotonic => bindings::CLOCK_MONOTONIC as i32,
+ ClockSource::BootTime => bindings::CLOCK_BOOTTIME as i32,
+ ClockSource::TAI => bindings::CLOCK_TAI as i32,
+ }
+ }
+}
+
/// Use to implement the [`HasTimer<T>`] trait.
///
/// See [`module`] documentation for an example.
--
2.46.0