Skip to contentSkip to conversation

rust: hrtimer: add active() method to query timer state

PATCH Superseded Andreas Hindborg 1 participant 1 message Jun 5

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andreas Hindborg

author maintainer
#0 lore raw

PATCH v2 1/1 rust: hrtimer: add active() method to query timer state

Add an active() method to HrTimer that returns true if the timer is in the started or running states. This wraps the kernel's hrtimer_active() function.

Also add documentation clarifying the definition of an active timer.

---
Changes in v2:
- Mark active() #[inline] (Gary).
- Use the [true] intra-doc link in the rustdoc (Miguel).
- Document that active() does not require exclusive access to the timer, and reflect this in the SAFETY comment (Gary).
- Add a doctest example that initializes a timer and asserts it is not active (Miguel).
- Link to v1: https://msgid.link/[email protected]

To: Andreas Hindborg <[email protected]>
To: Boqun Feng <[email protected]>
To: FUJITA Tomonori <[email protected]>
To: Frederic Weisbecker <[email protected]>
To: Lyude Paul <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Anna-Maria Behnsen <[email protected]>
To: John Stultz <[email protected]>
To: Stephen Boyd <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 rust/kernel/time/hrtimer.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
rust/kernel/time/hrtimer.rs +60 -0
index 2d7f1131a813..d57276496ed6 100644
@@ -71,6 +71,8 @@
 //! issue the `start` operation while the timer is in the **started** state. In
 //! this case the `start` operation is equivalent to the `restart` operation.
 //!
+//! A timer is **active** if it is either in the **started** or **running** states.
+//!
 //! # Examples
 //!
 //! ## Using an intrusive timer living in a [`Box`]
@@ -582,6 +584,64 @@ pub fn expires(&self) -> HrTimerInstant<T>
             )
         }
     }
+
+    /// Query the state of the timer.
+    ///
+    /// Returns [`true`] if the timer is in the started or running states.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use kernel::{
+    /// #     impl_has_hr_timer,
+    /// #     prelude::*,
+    /// #     time::{
+    /// #         hrtimer::{
+    /// #             HasHrTimer, HrTimer, HrTimerCallback, HrTimerCallbackContext,
+    /// #             HrTimerRestart, RelativeMode,
+    /// #         },
+    /// #         Monotonic,
+    /// #     },
+    /// # };
+    /// # use pin_init::stack_pin_init;
+    /// #[pin_data]
+    /// struct ExampleTimer {
+    ///     #[pin]
+    ///     timer: HrTimer<Self>,
+    /// }
+    ///
+    /// impl ExampleTimer {
+    ///     fn new() -> impl PinInit<Self> {
+    ///         pin_init!(Self { timer <- HrTimer::new() })
+    ///     }
+    /// }
+    ///
+    /// impl HrTimerCallback for ExampleTimer {
+    ///     type Pointer<'a> = Pin<&'a Self>;
+    ///     fn run(_this: Pin<&Self>, _ctx: HrTimerCallbackContext<'_, Self>) -> HrTimerRestart {
+    ///         HrTimerRestart::NoRestart
+    ///     }
+    /// }
+    ///
+    /// impl_has_hr_timer! {
+    ///     impl HasHrTimer<Self> for ExampleTimer {
+    ///         mode: RelativeMode<Monotonic>, field: self.timer
+    ///     }
+    /// }
+    ///
+    /// stack_pin_init!(let has_timer = ExampleTimer::new());
+    /// assert!(!has_timer.timer.active());
+    /// # Ok::<(), kernel::error::Error>(())
+    /// ```
+    ///
+    /// This method synchronizes internally and does not require exclusive access to the timer.
+    #[inline]
+    pub fn active(&self) -> bool {
+        // SAFETY: By type invariant, `self.timer` is valid. `hrtimer_active`
+        // synchronizes internally and is safe to call without exclusive
+        // access to the timer.
+        unsafe { bindings::hrtimer_active(self.timer.get()) }
+    }
 }
 
 /// Implemented by pointer types that point to structs that contain a [`HrTimer`].

---
base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32
change-id: 20260215-hrtimer-active-f183411fe56b

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>