From: Andreas Hindborg <a.hindborg@kernel.org>
To: "FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Lyude Paul" <lyude@redhat.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
"John Stultz" <jstultz@google.com>,
"Stephen Boyd" <sboyd@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Boqun Feng" <boqun@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Andreas Hindborg <a.hindborg@kernel.org>,
Boqun Feng <boqun@kernel.org>
Subject: [PATCH v2] rust: hrtimer: add active() method to query timer state
Date: Fri, 05 Jun 2026 15:11:36 +0200 [thread overview]
Message-ID: <20260605-hrtimer-active-v2-1-77d755c81fd7@kernel.org> (raw)
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.
Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
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/20260215-hrtimer-active-v1-1-a754d10492c2@kernel.org
To: Andreas Hindborg <a.hindborg@kernel.org>
To: Boqun Feng <boqun@kernel.org>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
To: Frederic Weisbecker <frederic@kernel.org>
To: Lyude Paul <lyude@redhat.com>
To: Thomas Gleixner <tglx@kernel.org>
To: Anna-Maria Behnsen <anna-maria@linutronix.de>
To: John Stultz <jstultz@google.com>
To: Stephen Boyd <sboyd@kernel.org>
To: Miguel Ojeda <ojeda@kernel.org>
To: Gary Guo <gary@garyguo.net>
To: Björn Roy Baron <bjorn3_gh@protonmail.com>
To: Benno Lossin <lossin@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>
To: Trevor Gross <tmgross@umich.edu>
To: Danilo Krummrich <dakr@kernel.org>
Cc: rust-for-linux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
rust/kernel/time/hrtimer.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
index 2d7f1131a813..d57276496ed6 100644
--- a/rust/kernel/time/hrtimer.rs
+++ b/rust/kernel/time/hrtimer.rs
@@ -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,
--
Andreas Hindborg <a.hindborg@kernel.org>
reply other threads:[~2026-06-05 13:11 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260605-hrtimer-active-v2-1-77d755c81fd7@kernel.org \
--to=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=jstultz@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox