Skip to content

Commit 69d2721

Browse files
Andreas Hindborgfbq
authored andcommitted
rust: hrtimer: add clocksource selection through ClockSource
Allow selecting a clock source for timers by passing a `ClockSource` variant to `HrTimer::new`. Acked-by: Frederic Weisbecker <[email protected]> Signed-off-by: Andreas Hindborg <[email protected]> Reviewed-by: Lyude Paul <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4f8f32a commit 69d2721

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

rust/kernel/time/hrtimer.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ unsafe impl<T> Sync for HrTimer<T> {}
9393

9494
impl<T> HrTimer<T> {
9595
/// Return an initializer for a new timer instance.
96-
pub fn new(mode: HrTimerMode) -> impl PinInit<Self>
96+
pub fn new(mode: HrTimerMode, clock: ClockSource) -> impl PinInit<Self>
9797
where
9898
T: HrTimerCallback,
9999
{
@@ -107,7 +107,7 @@ impl<T> HrTimer<T> {
107107
bindings::hrtimer_setup(
108108
place,
109109
Some(T::Pointer::run),
110-
bindings::CLOCK_MONOTONIC as i32,
110+
clock.into_c(),
111111
mode.into_c(),
112112
);
113113
}
@@ -468,6 +468,61 @@ impl HrTimerMode {
468468
}
469469
}
470470

471+
/// The clock source to use for a [`HrTimer`].
472+
pub enum ClockSource {
473+
/// A settable system-wide clock that measures real (i.e., wall-clock) time.
474+
///
475+
/// Setting this clock requires appropriate privileges. This clock is
476+
/// affected by discontinuous jumps in the system time (e.g., if the system
477+
/// administrator manually changes the clock), and by frequency adjustments
478+
/// performed by NTP and similar applications via adjtime(3), adjtimex(2),
479+
/// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts the
480+
/// number of seconds since 1970-01-01 00:00:00 Coordinated Universal Time
481+
/// (UTC) except that it ignores leap seconds; near a leap second it is
482+
/// typically adjusted by NTP to stay roughly in sync with UTC.
483+
RealTime,
484+
/// A monotonically increasing clock.
485+
///
486+
/// A nonsettable system-wide clock that represents monotonic time since—as
487+
/// described by POSIX—"some unspecified point in the past". On Linux, that
488+
/// point corresponds to the number of seconds that the system has been
489+
/// running since it was booted.
490+
///
491+
/// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps in the
492+
/// system time (e.g., if the system administrator manually changes the
493+
/// clock), but is affected by frequency adjustments. This clock does not
494+
/// count time that the system is suspended.
495+
Monotonic,
496+
/// A monotonic that ticks while system is suspended.
497+
///
498+
/// A nonsettable system-wide clock that is identical to CLOCK_MONOTONIC,
499+
/// except that it also includes any time that the system is suspended. This
500+
/// allows applications to get a suspend-aware monotonic clock without
501+
/// having to deal with the complications of CLOCK_REALTIME, which may have
502+
/// discontinuities if the time is changed using settimeofday(2) or similar.
503+
BootTime,
504+
/// International Atomic Time.
505+
///
506+
/// A nonsettable system-wide clock derived from wall-clock time but
507+
/// counting leap seconds. This clock does not experience discontinuities or
508+
/// frequency adjustments caused by inserting leap seconds as CLOCK_REALTIME
509+
/// does.
510+
///
511+
/// The acronym TAI refers to International Atomic Time.
512+
TAI,
513+
}
514+
515+
impl ClockSource {
516+
fn into_c(self) -> bindings::clockid_t {
517+
match self {
518+
ClockSource::RealTime => bindings::CLOCK_REALTIME as i32,
519+
ClockSource::Monotonic => bindings::CLOCK_MONOTONIC as i32,
520+
ClockSource::BootTime => bindings::CLOCK_BOOTTIME as i32,
521+
ClockSource::TAI => bindings::CLOCK_TAI as i32,
522+
}
523+
}
524+
}
525+
471526
/// Use to implement the [`HasHrTimer<T>`] trait.
472527
///
473528
/// See [`module`] documentation for an example.

0 commit comments

Comments
 (0)