From: Andreas Hindborg <a.hindborg@kernel.org>
To: Miguel Ojeda <ojeda@kernel.org>,
Alex Gaynor <alex.gaynor@gmail.com>,
Anna-Maria Behnsen <anna-maria@linutronix.de>,
Frederic Weisbecker <frederic@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>
Cc: "Andreas Hindborg" <a.hindborg@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Alice Ryhl" <aliceryhl@google.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 13/14] rust: hrtimer: add `schedule_function` to schedule closures
Date: Wed, 18 Sep 2024 00:27:37 +0200 [thread overview]
Message-ID: <20240917222739.1298275-14-a.hindborg@kernel.org> (raw)
In-Reply-To: <20240917222739.1298275-1-a.hindborg@kernel.org>
Add a way to quickly schedule functions for execution after a certain
duration has elapsed.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/hrtimer.rs | 49 +++++++++++++++++++++++
rust/kernel/hrtimer/closure.rs | 72 ++++++++++++++++++++++++++++++++++
2 files changed, 121 insertions(+)
create mode 100644 rust/kernel/hrtimer/closure.rs
diff --git a/rust/kernel/hrtimer.rs b/rust/kernel/hrtimer.rs
index 1750016b2b22..2fd3e68368da 100644
--- a/rust/kernel/hrtimer.rs
+++ b/rust/kernel/hrtimer.rs
@@ -132,6 +132,52 @@
//! pr_info!("Flag raised\n");
//! # Ok::<(), kernel::error::Error>(())
//! ```
+//!
+//! Using a helper:
+//! ```
+//! use kernel::{
+//! hrtimer::schedule_function,
+//! impl_has_timer, new_condvar, new_mutex,
+//! prelude::*,
+//! stack_try_pin_init,
+//! sync::{Arc, CondVar, Mutex},
+//! time::Ktime,
+//! };
+//!
+//! #[pin_data]
+//! struct Data {
+//! #[pin]
+//! flag: Mutex<bool>,
+//! #[pin]
+//! cond: CondVar,
+//! }
+//!
+//! impl Data {
+//! fn new() -> impl PinInit<Self, kernel::error::Error> {
+//! try_pin_init!(Self {
+//! flag <- new_mutex!(false),
+//! cond <- new_condvar!(),
+//! })
+//! }
+//! }
+//!
+//! let data = Arc::pin_init(Data::new(), GFP_KERNEL)?;
+//! let data2 = data.clone();
+//!
+//! let handle = schedule_function(Ktime::from_ns(200_000_000), move || {
+//! pr_info!("Hello from the future");
+//! *data2.flag.lock() = true;
+//! data2.cond.notify_all();
+//! });
+//!
+//! let mut guard = data.flag.lock();
+//! while !*guard {
+//! data.cond.wait(&mut guard);
+//! }
+//!
+//! pr_info!("Flag raised\n");
+//! # Ok::<(), kernel::error::Error>(())
+//! ```
use crate::{init::PinInit, prelude::*, time::Ktime, types::Opaque};
use core::marker::PhantomData;
@@ -497,5 +543,8 @@ unsafe fn raw_get_timer(ptr: *const Self) ->
mod tbox;
mod arc;
+mod closure;
mod pin;
mod pin_mut;
+
+pub use closure::schedule_function;
diff --git a/rust/kernel/hrtimer/closure.rs b/rust/kernel/hrtimer/closure.rs
new file mode 100644
index 000000000000..96286a12e87a
--- /dev/null
+++ b/rust/kernel/hrtimer/closure.rs
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0
+
+use super::{pin_init, tbox::BoxTimerHandle, Timer, TimerCallback, TimerPointer, TimerRestart};
+use crate::{
+ alloc::{flags, Flags},
+ impl_has_timer, new_mutex,
+ prelude::*,
+ sync::Mutex,
+ time::Ktime,
+};
+use macros::pin_data;
+
+#[pin_data]
+pub struct ClosureTimer<T> {
+ #[pin]
+ timer: Timer<ClosureTimer<T>>,
+ #[pin]
+ callback: Mutex<Option<T>>,
+}
+
+impl_has_timer! {
+ impl{T} HasTimer<Self> for ClosureTimer<T> { self.timer }
+}
+
+impl<T> TimerCallback for ClosureTimer<T>
+where
+ T: FnOnce() + 'static,
+{
+ type CallbackTarget<'a> = Pin<Box<ClosureTimer<T>>>;
+ type CallbackPointer<'a> = &'a ClosureTimer<T>;
+
+ fn run(this: Self::CallbackPointer<'_>) -> TimerRestart
+ where
+ Self: Sized,
+ {
+ if let Some(callback) = this.callback.lock().take() {
+ callback();
+ }
+ TimerRestart::NoRestart
+ }
+}
+
+impl<T> ClosureTimer<T>
+where
+ T: FnOnce() + 'static,
+ T: Send,
+ T: Sync,
+{
+ fn new(f: T, flags: Flags) -> Result<Pin<Box<Self>>> {
+ Box::pin_init(
+ pin_init!(
+ Self {
+ timer <- Timer::new(),
+ callback <- new_mutex!(Some(f)),
+ }
+ ),
+ flags,
+ )
+ }
+}
+
+/// Schedule `f` for execution after `expires` time.
+pub fn schedule_function<T>(expires: Ktime, f: T) -> Result<BoxTimerHandle<ClosureTimer<T>>>
+where
+ T: FnOnce() + 'static,
+ T: Send,
+ T: Sync,
+{
+ let timer = ClosureTimer::<T>::new(f, flags::GFP_KERNEL)?;
+ let handle = <Pin<Box<ClosureTimer<T>>> as TimerPointer>::schedule(timer, expires);
+ Ok(handle)
+}
--
2.46.0
next prev parent reply other threads:[~2024-09-17 22:29 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-17 22:27 [PATCH v2 00/14] hrtimer Rust API Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 01/14] rust: time: Add Ktime::from_ns() Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 02/14] rust: hrtimer: introduce hrtimer support Andreas Hindborg
2024-09-18 18:13 ` Benno Lossin
2024-09-19 5:43 ` Andreas Hindborg
2024-09-19 14:09 ` Benno Lossin
2024-09-23 16:35 ` Andreas Hindborg
2024-09-23 16:59 ` Benno Lossin
2024-10-10 12:24 ` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 03/14] rust: sync: add `Arc::as_ptr` Andreas Hindborg
2024-09-19 14:03 ` Benno Lossin
2024-09-21 15:58 ` Gary Guo
2024-09-21 18:17 ` Benno Lossin
2024-09-23 8:14 ` Alice Ryhl
2024-10-01 4:56 ` Dirk Behme
2024-10-01 8:39 ` Benno Lossin
2024-09-17 22:27 ` [PATCH v2 04/14] rust: sync: add `Arc::clone_from_raw` Andreas Hindborg
2024-09-18 18:19 ` Benno Lossin
2024-09-18 20:12 ` Gary Guo
2024-09-18 21:09 ` Benno Lossin
2024-09-19 6:00 ` Andreas Hindborg
2024-09-19 14:15 ` Benno Lossin
2024-09-20 8:25 ` Andreas Hindborg
2024-09-19 5:54 ` Andreas Hindborg
2024-09-19 6:19 ` Andreas Hindborg
2024-09-19 6:41 ` Alice Ryhl
2024-09-17 22:27 ` [PATCH v2 05/14] rust: hrtimer: implement `TimerPointer` for `Arc` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 06/14] rust: hrtimer: allow timer restart from timer handler Andreas Hindborg
2024-09-20 14:25 ` kernel test robot
2024-09-17 22:27 ` [PATCH v2 07/14] rust: hrtimer: add `UnsafeTimerPointer` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 08/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&T>` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 09/14] rust: hrtimer: implement `UnsafeTimerPointer` for `Pin<&mut T>` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 10/14] rust: hrtimer: add `hrtimer::ScopedTimerPointer` Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 11/14] rust: hrtimer: allow specifying a distinct callback parameter Andreas Hindborg
2024-09-17 22:27 ` [PATCH v2 12/14] rust: hrtimer: implement `TimerPointer` for `Pin<Box<T>>` Andreas Hindborg
2024-09-17 22:27 ` Andreas Hindborg [this message]
2024-09-17 22:27 ` [PATCH v2 14/14] rust: hrtimer: add maintainer entry Andreas Hindborg
2024-10-12 15:19 ` Boqun Feng
2024-09-30 9:36 ` [PATCH v2 00/14] hrtimer Rust API Anna-Maria Behnsen
2024-10-04 10:47 ` Andreas Hindborg
2024-10-01 12:37 ` Dirk Behme
2024-10-01 14:42 ` Boqun Feng
2024-10-03 8:14 ` Dirk Behme
2024-10-03 13:03 ` Boqun Feng
2024-10-03 16:18 ` Dirk Behme
2024-10-11 14:52 ` Andreas Hindborg
2024-10-11 15:43 ` Dirk Behme
2024-10-11 23:21 ` Boqun Feng
2024-10-12 5:19 ` Dirk Behme
2024-10-12 7:41 ` Boqun Feng
2024-10-12 7:50 ` Dirk Behme
2024-10-12 22:26 ` Boqun Feng
2024-10-13 17:39 ` Dirk Behme
2024-10-13 21:06 ` Boqun Feng
2024-10-14 6:58 ` Dirk Behme
2024-10-14 9:17 ` Andreas Hindborg
2024-10-14 9:38 ` Alice Ryhl
2024-10-14 11:53 ` Dirk Behme
2024-10-14 11:58 ` Alice Ryhl
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=20240917222739.1298275-14-a.hindborg@kernel.org \
--to=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=frederic@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tglx@linutronix.de \
/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
all inboxes | Powered by JetHome®