rust: workqueue: Add an example for try_spawn()
RFC PATCH 1/1 rust: workqueue: Add an example for try_spawn()
try_spawn() could use an example to demonstrate the usage, and arguably it's the most simple usage of workqueue in case someone needs a deferred work, so add it.
---
Miguel, Alice and Tejun, while I'm at it, should we also rename the function to spawn() because of the motivation mentioned here [1]?
Also I find the { <clone> || { } } is really good if I only need to clone the Arc for passing to a callback closure, but I'm not sure how people feel about it, so criticism is welcome ;-)
rust/kernel/workqueue.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
rust/kernel/workqueue.rs +27 -0
@@ -331,6 +331,33 @@ pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu/// Tries to spawn the given function or closure as a work item. /// /// This method can fail because it allocates memory to store the work item. +/// +/// # Examples +/// +/// ``` +/// use kernel::{alloc::flags, sync::{Arc, Completion, new_spinlock}, workqueue}; +/// +/// let work_done = Arc::pin_init(Completion::new(), flags::GFP_KERNEL)?; +/// let data = Arc::pin_init(new_spinlock!(0), flags::GFP_KERNEL)?; +/// +/// workqueue::system().try_spawn( +/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; +/// +/// work_done.wait_for_completion(); +/// +/// // `work_done` being completed implies the observation of the write of `data` in the work. +/// assert_eq!(*data.lock(), 42); +/// # Ok::<(), Error>(()) +/// ``` pub fn try_spawn <T : 'static +Send +FnOnce ()>( &self, flags: Flags,
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 3 lines · Boqun Feng
Miguel, Alice and Tejun, while I'm at it, should we also rename the function to
spawn()because of the motivation mentioned here [1]?
Sounds good to me.
Cheers,
Miguel
Quoted 7 lines · Miguel Ojeda
Quoted 3 lines · Boqun Feng
Miguel, Alice and Tejun, while I'm at it, should we also rename the function to
spawn()because of the motivation mentioned here [1]?Sounds good to me.
Thanks, I will add it in v2 (probably as a separate patch) if no one shows any objection.
Regards,
Boqun
Quoted 2 lines
Cheers,
Miguel
Quoted 7 lines · Boqun Feng
try_spawn()could use an example to demonstrate the usage, and arguably it's the most simple usage of workqueue in case someone needs a deferred work, so add it.Cc Joel Fernandes <[email protected]>Cc "Paul E. McKenney" <[email protected]>Signed-off-by Boqun Feng <[email protected]>
Makes sense, LGTM.
thanks,
- Joel
Quoted 51 lines
---
Miguel, Alice and Tejun, while I'm at it, should we also rename the function tospawn()because of the motivation mentioned here [1]?Also I find the
{ <clone> || { } }is really good if I only need to clone the Arc for passing to a callback closure, but I'm not sure how people feel about it, so criticism is welcome ;-)rust/kernel/workqueue.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)rust/kernel/workqueue.rs +27
-0@@ -331,6 +331,33 @@ pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu/// Tries to spawn the given function or closure as a work item. /// /// This method can fail because it allocates memory to store the work item. +/// +/// # Examples +/// +/// ``` +/// use kernel::{alloc::flags, sync::{Arc, Completion, new_spinlock}, workqueue}; +/// +/// let work_done = Arc::pin_init(Completion::new(), flags::GFP_KERNEL)?; +/// let data = Arc::pin_init(new_spinlock!(0), flags::GFP_KERNEL)?; +/// +/// workqueue::system().try_spawn( +/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; +/// +/// work_done.wait_for_completion(); +/// +/// // `work_done` being completed implies the observation of the write of `data` in the work. +/// assert_eq!(*data.lock(), 42); +/// # Ok::<(), Error>(()) +/// ``` pub fn try_spawn <T : 'static +Send +FnOnce ()>( &self, flags: Flags,
Benno Lossin
reviewerQuoted 11 lines · Boqun Feng
try_spawn()could use an example to demonstrate the usage, and arguably it's the most simple usage of workqueue in case someone needs a deferred work, so add it.Cc Joel Fernandes <[email protected]>Cc "Paul E. McKenney" <[email protected]>Signed-off-by Boqun Feng <[email protected]>---
Miguel, Alice and Tejun, while I'm at it, should we also rename the function tospawn()because of the motivation mentioned here [1]?
Quoted 5 lines
Also I find the
{ <clone> || { } }is really good if I only need to clone the Arc for passing to a callback closure, but I'm not sure how people feel about it, so criticism is welcome ;-)
I'm not so sure, see below :)
Quoted 21 lines
rust/kernel/workqueue.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)rust/kernel/workqueue.rs +10
-0@@ -331,6 +331,33 @@ pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu/// Tries to spawn the given function or closure as a work item. /// /// This method can fail because it allocates memory to store the work item. +/// +/// # Examples +/// +/// ``` +/// use kernel::{alloc::flags, sync::{Arc, Completion, new_spinlock}, workqueue}; +/// +/// let work_done = Arc::pin_init(Completion::new(), flags::GFP_KERNEL)?; +/// let data = Arc::pin_init(new_spinlock!(0), flags::GFP_KERNEL)?; +/// +/// workqueue::system().try_spawn(
rust/kernel/workqueue.rs pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu · line 353
Quoted 10 lines
rust/kernel/workqueue.rs +10
-0+/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?;
Not doing your pattern and instead adding a 2 postfix we get:
let work_done2 = work_done.clone();
let data2 = data.clone();
workqueue::system().try_spawn(flags::GFP_KERNEL, move || {
*data2.lock() = 42;
work_done2.complete_all();
})?;There are some discussions of introducing some better syntax for (cheap) cloning, so maybe we can use that in the future.
---
Cheers,
Benno
Quoted 10 lines
rust/kernel/workqueue.rs +7
-0+/// +/// work_done.wait_for_completion(); +/// +/// // `work_done` being completed implies the observation of the write of `data` in the work. +/// assert_eq!(*data.lock(), 42); +/// # Ok::<(), Error>(()) +/// ``` pub fn try_spawn <T : 'static +Send +FnOnce ()>( &self, flags: Flags,
Quoted 54 lines · Benno Lossin
Quoted 11 lines · Boqun Feng
try_spawn()could use an example to demonstrate the usage, and arguably it's the most simple usage of workqueue in case someone needs a deferred work, so add it.Cc Joel Fernandes <[email protected]>Cc "Paul E. McKenney" <[email protected]>Signed-off-by Boqun Feng <[email protected]>---
Miguel, Alice and Tejun, while I'm at it, should we also rename the function tospawn()because of the motivation mentioned here [1]?Quoted 5 lines
Also I find the
{ <clone> || { } }is really good if I only need to clone the Arc for passing to a callback closure, but I'm not sure how people feel about it, so criticism is welcome ;-)I'm not so sure, see below :)
Quoted 21 lines
rust/kernel/workqueue.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)rust/kernel/workqueue.rs +10
-0@@ -331,6 +331,33 @@ pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu/// Tries to spawn the given function or closure as a work item. /// /// This method can fail because it allocates memory to store the work item. +/// +/// # Examples +/// +/// ``` +/// use kernel::{alloc::flags, sync::{Arc, Completion, new_spinlock}, workqueue}; +/// +/// let work_done = Arc::pin_init(Completion::new(), flags::GFP_KERNEL)?; +/// let data = Arc::pin_init(new_spinlock!(0), flags::GFP_KERNEL)?; +/// +/// workqueue::system().try_spawn( rust/kernel/workqueue.rs pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu · line 353
Quoted 10 lines
rust/kernel/workqueue.rs +10
-0+/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; Not doing your pattern and instead adding a
2postfix we get:
Quoted 3 lines
let work_done2 = work_done.clone();
let data2 = data.clone();Yeah, the thing I want to achieve with my pattern is: make it clear that the work and the task that queues the work are sharing the same work_done and data (well, no the same Arc exactly, but the Arcs that are pointing to the same object). This pattern here doesn't show that clearly imo.
That said, I'm not really against using work_done2 and data2, just I'm afraid that may be more confusing.
Quoted 7 lines
workqueue::system().try_spawn(flags::GFP_KERNEL, move || { *data2.lock() = 42; work_done2.complete_all(); })?;There are some discussions of introducing some better syntax for (cheap) cloning, so maybe we can use that in the future.
Do you have links to these discussions.
Regards,
Boqun
Quoted 16 lines
---
Cheers,
BennoQuoted 10 lines
rust/kernel/workqueue.rs +7
-0+/// +/// work_done.wait_for_completion(); +/// +/// // `work_done` being completed implies the observation of the write of `data` in the work. +/// assert_eq!(*data.lock(), 42); +/// # Ok::<(), Error>(()) +/// ``` pub fn try_spawn <T : 'static +Send +FnOnce ()>( &self, flags: Flags,
Benno Lossin
reviewerQuoted 20 lines · Boqun Feng
Quoted 13 lines · Benno Lossin
rust/kernel/workqueue.rs pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu · line 353
Quoted 11 lines · Boqun Feng
rust/kernel/workqueue.rs +11
-0+/// workqueue::system().try_spawn( +/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; Quoted 5 lines
Not doing your pattern and instead adding a
2postfix we get:let work_done2 = work_done.clone(); let data2 = data.clone();
Quoted 5 lines
Yeah, the thing I want to achieve with my pattern is: make it clear that the work and the task that queues the work are sharing the same
work_doneanddata(well, no the sameArcexactly, but theArcs that are pointing to the same object). This pattern here doesn't show that clearly imo.
I think it's fine, that pattern is often used for that. Not heavily opposed to doing it your way, but I feel like the code looks a bit weird & my instinct is to move the let bindings out (which would produce code that doesn't compile).
Quoted 2 lines
That said, I'm not really against using
work_done2anddata2, just I'm afraid that may be more confusing.
I don't think that's a problem.
Quoted 9 lines
Quoted 7 lines
workqueue::system().try_spawn(flags::GFP_KERNEL, move || { *data2.lock() = 42; work_done2.complete_all(); })?;There are some discussions of introducing some better syntax for (cheap) cloning, so maybe we can use that in the future.
Do you have links to these discussions.
It's an RFC:
https://github.com/rust-lang/rfcs/pull/368
There probably are more discussions on zulip, but I haven't read those. The RFC also has a project goal:
https://rust-lang.github.io/rust-project-goals/2025h2/ergonomic-rc.html
---
Cheers,
Benno
Quoted 21 lines · Benno Lossin
Quoted 20 lines · Boqun Feng
Quoted 18 lines · Benno Lossin
rust/kernel/workqueue.rs pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu · line 353
Quoted 11 lines · Boqun Feng
rust/kernel/workqueue.rs +11
-0+/// workqueue::system().try_spawn( +/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; Not doing your pattern and instead adding a
2postfix we get:let work_done2 = work_done.clone(); let data2 = data.clone();
Quoted 8 lines
Quoted 5 lines
Yeah, the thing I want to achieve with my pattern is: make it clear that the work and the task that queues the work are sharing the same
work_doneanddata(well, no the sameArcexactly, but theArcs that are pointing to the same object). This pattern here doesn't show that clearly imo.I think it's fine, that pattern is often used for that. Not heavily opposed to doing it your way, but I feel like the code looks a bit weird
Ok, I will drop my style and use work_done2 and data2, because it'll be at the general documentation, but I might keep using my pattern in other code because it looks reasonable to me ;-)
Quoted 18 lines
& my instinct is to move the let bindings out (which would produce code that doesn't compile).
Quoted 2 lines
That said, I'm not really against using
work_done2anddata2, just I'm afraid that may be more confusing.I don't think that's a problem.
Quoted 9 lines
Quoted 7 lines
workqueue::system().try_spawn(flags::GFP_KERNEL, move || { *data2.lock() = 42; work_done2.complete_all(); })?;There are some discussions of introducing some better syntax for (cheap) cloning, so maybe we can use that in the future.
Do you have links to these discussions.
Quoted 8 lines
It's an RFC:
https://github.com/rust-lang/rfcs/pull/368There probably are more discussions on zulip, but I haven't read those. The RFC also has a project goal:
https://rust-lang.github.io/rust-project-goals/2025h2/ergonomic-rc.html
Thanks for the pointer.
Regards,
Boqun
Quoted 4 lines
---
Cheers,
Benno
Benno Lossin
reviewerQuoted 28 lines · Boqun Feng
Quoted 27 lines · Benno Lossin
Quoted 25 lines · Boqun Feng
Quoted 13 lines · Benno Lossin
rust/kernel/workqueue.rs pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu · line 353
Quoted 11 lines · Boqun Feng
rust/kernel/workqueue.rs +11
-0+/// workqueue::system().try_spawn( +/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; Quoted 5 lines
Not doing your pattern and instead adding a
2postfix we get:let work_done2 = work_done.clone(); let data2 = data.clone();Yeah, the thing I want to achieve with my pattern is: make it clear that the work and the task that queues the work are sharing the same
work_doneanddata(well, no the sameArcexactly, but theArcs that are pointing to the same object). This pattern here doesn't show that clearly imo.
Quoted 6 lines
Quoted 2 lines
I think it's fine, that pattern is often used for that. Not heavily opposed to doing it your way, but I feel like the code looks a bit weird
Ok, I will drop my style and use
work_done2and data2, because it'll be at the general documentation, but I might keep using my pattern in other code because it looks reasonable to me ;-)
It is reasonable :) If you do want to use the same name for them, then I personally think this looks better than moving the let bindings inside of the expression:
{
let work_done = work_done.clone();
let data = data.clone();
workqueue::system().try_spawn(flags::GFP_KERNEL, move || {
*data.lock() = 42;
work_done.complete_all();
})?;
}But that gives the try_spawn call extra indentation which also isn't ideal... I'd be best for the language feature to exist :)
---
Cheers,
Benno
Alice Ryhl
reviewerQuoted 4 lines · Boqun Feng
try_spawn()could use an example to demonstrate the usage, and arguably it's the most simple usage of workqueue in case someone needs a deferred work, so add it.
Quoted 8 lines
Cc Joel Fernandes <[email protected]>Cc "Paul E. McKenney" <[email protected]>Signed-off-by Boqun Feng <[email protected]>---
Miguel, Alice and Tejun, while I'm at it, should we also rename the function tospawn()because of the motivation mentioned here [1]?
Renaming sounds good to me.
Quoted 3 lines
Also I find the
{ <clone> || { } }is really good if I only need to clone the Arc for passing to a callback closure, but I'm not sure how people feel about it, so criticism is welcome ;-)
It's a neat trick, but I think it risks confusing people. I would probably not include it in an example like this one.
Quoted 44 lines
rust/kernel/workqueue.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)rust/kernel/workqueue.rs +27
-0@@ -331,6 +331,33 @@ pub fn enqueue_delayed<W, const ID: u64>(&self, w: W, delay: Jiffies) -> W::Enqu/// Tries to spawn the given function or closure as a work item. /// /// This method can fail because it allocates memory to store the work item. +/// +/// # Examples +/// +/// ``` +/// use kernel::{alloc::flags, sync::{Arc, Completion, new_spinlock}, workqueue}; +/// +/// let work_done = Arc::pin_init(Completion::new(), flags::GFP_KERNEL)?; +/// let data = Arc::pin_init(new_spinlock!(0), flags::GFP_KERNEL)?; +/// +/// workqueue::system().try_spawn( +/// flags::GFP_KERNEL, +/// { +/// let work_done = work_done.clone(); +/// let data = data.clone(); +/// move || { +/// *data.lock() = 42; +/// work_done.complete_all(); +/// } +/// } +/// )?; +/// +/// work_done.wait_for_completion(); +/// +/// // `work_done` being completed implies the observation of the write of `data` in the work. +/// assert_eq!(*data.lock(), 42); +/// # Ok::<(), Error>(()) +/// ``` pub fn try_spawn <T : 'static +Send +FnOnce ()>( &self, flags: Flags,Signature · 1 line
2.39.5 (Apple Git-154)