This patch allows timer handlers to report that they want a timer to be
restarted after the timer handler has finished executing.
Also update the `hrtimer` documentation to showcase the new feature.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/hrtimer.rs | 50 ++++++++++++++++++++++++++++++++------
rust/kernel/hrtimer/arc.rs | 4 +--
2 files changed, 43 insertions(+), 11 deletions(-)
diff --git a/rust/kernel/hrtimer.rs b/rust/kernel/hrtimer.rs
index fd1520ba9fba..d6c3fa89f77e 100644
--- a/rust/kernel/hrtimer.rs
+++ b/rust/kernel/hrtimer.rs
@@ -9,7 +9,7 @@
//!
//! ```
//! use kernel::{
-//! hrtimer::{Timer, TimerCallback, TimerPointer},
+//! hrtimer::{Timer, TimerCallback, TimerPointer, TimerRestart},
//! impl_has_timer, new_condvar, new_mutex,
//! prelude::*,
//! sync::{Arc, CondVar, Mutex},
@@ -21,7 +21,7 @@
//! #[pin]
//! timer: Timer<Self>,
//! #[pin]
-//! flag: Mutex<bool>,
+//! flag: Mutex<u64>,
//! #[pin]
//! cond: CondVar,
//! }
@@ -30,7 +30,7 @@
//! fn new() -> impl PinInit<Self, kernel::error::Error> {
//! try_pin_init!(Self {
//! timer <- Timer::new(),
-//! flag <- new_mutex!(false),
+//! flag <- new_mutex!(0),
//! cond <- new_condvar!(),
//! })
//! }
@@ -39,10 +39,18 @@
//! impl TimerCallback for ArcIntrusiveTimer {
//! type CallbackTarget<'a> = Arc<Self>;
//!
-//! fn run(this: Self::CallbackTarget<'_>) {
+//! fn run(this: Self::CallbackTarget<'_>) -> TimerRestart {
//! pr_info!("Timer called\n");
-//! *this.flag.lock() = true;
+//! let mut guard = this.flag.lock();
+//! *guard += 1;
//! this.cond.notify_all();
+//! if *guard == 5 {
+//! TimerRestart::NoRestart
+//! }
+//! else {
+//! TimerRestart::Restart
+//!
+//! }
//! }
//! }
//!
@@ -55,11 +63,11 @@
//! let _handle = has_timer.clone().schedule(Ktime::from_ns(200_000_000));
//! let mut guard = has_timer.flag.lock();
//!
-//! while !*guard {
+//! while *guard != 5 {
//! has_timer.cond.wait(&mut guard);
//! }
//!
-//! pr_info!("Flag raised\n");
+//! pr_info!("Counted to 5\n");
//! # Ok::<(), kernel::error::Error>(())
//! ```
@@ -202,7 +210,7 @@ pub trait TimerCallback {
type CallbackTarget<'a>: RawTimerCallback;
/// Called by the timer logic when the timer fires.
- fn run(this: Self::CallbackTarget<'_>)
+ fn run(this: Self::CallbackTarget<'_>) -> TimerRestart
where
Self: Sized;
}
@@ -296,6 +304,32 @@ unsafe fn schedule(self_ptr: *const Self, expires: Ktime) {
}
}
+/// Restart policy for timers.
+pub enum TimerRestart {
+ /// Timer should not be restarted.
+ NoRestart,
+ /// Timer should be restarted.
+ Restart,
+}
+
+impl From<u32> for TimerRestart {
+ fn from(value: bindings::hrtimer_restart) -> Self {
+ match value {
+ 0 => Self::NoRestart,
+ _ => Self::Restart,
+ }
+ }
+}
+
+impl From<TimerRestart> for bindings::hrtimer_restart {
+ fn from(value: TimerRestart) -> Self {
+ match value {
+ TimerRestart::NoRestart => bindings::hrtimer_restart_HRTIMER_NORESTART,
+ TimerRestart::Restart => bindings::hrtimer_restart_HRTIMER_RESTART,
+ }
+ }
+}
+
/// Use to implement the [`HasTimer<T>`] trait.
///
/// See [`module`] documentation for an example.
diff --git a/rust/kernel/hrtimer/arc.rs b/rust/kernel/hrtimer/arc.rs
index 80f6c20f95a9..fb8a40484add 100644
--- a/rust/kernel/hrtimer/arc.rs
+++ b/rust/kernel/hrtimer/arc.rs
@@ -80,8 +80,6 @@ impl<U> RawTimerCallback for Arc<U>
// timer. This `U` is contained in an `Arc`.
let receiver = unsafe { Arc::clone_from_raw(data_ptr) };
- U::run(receiver);
-
- bindings::hrtimer_restart_HRTIMER_NORESTART
+ U::run(receiver).into()
}
}
--
2.46.0