Skip to contentSkip to conversation

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

PATCH Superseded Andreas Hindborg 5 participants 8 messages Feb 15 – Mar 1

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andreas Hindborg

author maintainer
#0 lore raw

PATCH 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.

---
 rust/kernel/time/hrtimer.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/time/hrtimer.rs +10 -0
index 856d2d929a008..b2272b059e504 100644
@@ -66,6 +66,8 @@
 //!
 //! A `restart` operation on a timer in the **stopped** state is equivalent to a
 //! `start` operation.
+//!
+//! A timer is **active** if it is either in the **started** or **running** states.
 
 use super::{ClockSource, Delta, Instant};
 use crate::{prelude::*, types::Opaque};
@@ -246,6 +248,14 @@ pub fn expires(&self) -> HrTimerInstant<T>
             )
         }
     }
+
+    /// Query the state of the timer.
+    ///
+    /// Returns `true` if the timer is in the started or running states.
+    pub fn active(&self) -> bool {
+        // SAFETY: By type invariant, `self.timer` is valid.
+        unsafe { bindings::hrtimer_active(self.timer.get()) }
+    }
 }
 
 /// Implemented by pointer types that point to structs that contain a [`HrTimer`].

---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-hrtimer-active-f183411fe56b

Best regards,

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

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 254

Quoted 4 lines · Andreas Hindborg
rust/kernel/time/hrtimer.rs +3 -0
+    /// Query the state of the timer.
+    ///
+    /// Returns `true` if the timer is in the started or running states.

[true]

Since we try to add examples when we add APIs, could we perhaps add one that inits a timers and asserts it is not active?

Thanks!

Cheers,
Miguel

#2 lore raw
Quoted 7 lines · On Sun, 15 Feb 2026 21:59:57 +0100 Miguel Ojeda…

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 254

Quoted 4 lines · Andreas Hindborg
rust/kernel/time/hrtimer.rs +3 -0
+    /// Query the state of the timer.
+    ///
+    /// Returns `true` if the timer is in the started or running states.

[true]

Should we use intra-doc links for true/flase? Currently, rust/kernel doesn't seem to use them. Seems that Rust std library isn't consistent on this either.

#3 lore raw
Quoted 4 lines · FUJITA Tomonori

Should we use intra-doc links for true/flase? Currently, rust/kernel doesn't seem to use them. Seems that Rust std library isn't consistent on this either.

Maybe... It doesn't matter much either way, obviously.

For simplicity/consistency with other "trivial" items, it may be better. On the other hand, it is more painful to type and takes 2 characters.

This is the entry I have so far for this in my to-be-sent guidelines "rules" list:

- Use intra-doc links wherever the link would work (i.e. if ``rustdoc`` can resolve it), unless doing so would be unreasonably complex or verbose. In such a case, use a simple code span instead.

Do so even for trivial types and constants, such as ``i32`` and ``true``, as well as for ``Self``.

.. code-block:: rust

    /// Returns a new [`Foo`].

Avoid:

.. code-block:: rust

    /// Returns a new `Foo`.

Cheers,
Miguel

#4 lore raw
Quoted 24 lines · On Sat, 28 Feb 2026 04:04:27 +0100 Miguel Ojeda…
Quoted 4 lines · FUJITA Tomonori

Should we use intra-doc links for true/flase? Currently, rust/kernel doesn't seem to use them. Seems that Rust std library isn't consistent on this either.

Maybe... It doesn't matter much either way, obviously.

For simplicity/consistency with other "trivial" items, it may be better. On the other hand, it is more painful to type and takes 2 characters.

This is the entry I have so far for this in my to-be-sent guidelines "rules" list:

- Use intra-doc links wherever the link would work (i.e. if ``rustdoc`` can resolve it), unless doing so would be unreasonably complex or verbose. In such a case, use a simple code span instead.

Do so even for trivial types and constants, such as ``i32`` and ``true``, as well as for ``Self``.

.. code-block:: rust

Quoted 7 lines
    /// Returns a new [`Foo`].

Avoid:

.. code-block:: rust

    /// Returns a new `Foo`.

Agreed, consistency alone is a good enough reason for this.

reviewer
#5 lore raw
In reply to Miguel Ojeda
Quoted 24 lines · Miguel Ojeda
Quoted 4 lines · FUJITA Tomonori

Should we use intra-doc links for true/flase? Currently, rust/kernel doesn't seem to use them. Seems that Rust std library isn't consistent on this either.

Maybe... It doesn't matter much either way, obviously.

For simplicity/consistency with other "trivial" items, it may be better. On the other hand, it is more painful to type and takes 2 characters.

This is the entry I have so far for this in my to-be-sent guidelines "rules" list:

- Use intra-doc links wherever the link would work (i.e. if ``rustdoc`` can resolve it), unless doing so would be unreasonably complex or verbose. In such a case, use a simple code span instead.

Do so even for trivial types and constants, such as ``i32`` and ``true``, as well as for ``Self``.

.. code-block:: rust

Quoted 7 lines
    /// Returns a new [`Foo`].

Avoid:

.. code-block:: rust

    /// Returns a new `Foo`.

I'm not convinced by that. I don't think it's useful for true or Self to be links in most cases.

Alice

reviewer
#6 lore raw
In reply to Andreas Hindborg
Quoted 25 lines · Andreas Hindborg

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.

---
 rust/kernel/time/hrtimer.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/time/hrtimer.rs +2 -0
index 856d2d929a008..b2272b059e504 100644
@@ -66,6 +66,8 @@
 //!
 //! A `restart` operation on a timer in the **stopped** state is equivalent to a
 //! `start` operation.
+//!
+//! A timer is **active** if it is either in the **started** or **running** states.
 
 use super::{ClockSource, Delta, Instant};
 use crate::{prelude::*, types::Opaque};

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 254

Quoted 7 lines
rust/kernel/time/hrtimer.rs +4 -0
@@ -246,6 +248,14 @@ pub fn expires(&self) -> HrTimerInstant<T>
             )
         }
     }
+
+    /// Query the state of the timer.
+    ///
+    /// Returns `true` if the timer is in the started or running states.

This could be #[inline].

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 256

Quoted 2 lines
rust/kernel/time/hrtimer.rs +2 -0
+    pub fn active(&self) -> bool {
+        // SAFETY: By type invariant, `self.timer` is valid.

Perhaps also mention that this function is safe to call without exclusive requirement, as this is a common requiremnt for many other hrtimer functions.

Best,
Gary

Quoted 11 lines
rust/kernel/time/hrtimer.rs +2 -1
+        unsafe { bindings::hrtimer_active(self.timer.get()) }
+    }
 }
 
 /// Implemented by pointer types that point to structs that contain a [`HrTimer`].
 
---

base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-hrtimer-active-f183411fe56b

Best regards,

Reviewed-by on v1 1/1
#7 lore raw
In reply to Andreas Hindborg
Quoted 10 lines · On Sun, 15 Feb 2026 21:29:53 +0100 Andreas Hindborg <[email protected]>…

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.

---
 rust/kernel/time/hrtimer.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)