[PATCH 1/9] rust: sync: completion: add single-shot and timed operations
Mike Lothian <[email protected]>
| Newsgroups | org.kernel.vger.rust-for-linux,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
Expose complete() and wait_for_completion_timeout() so Rust drivers can coordinate asynchronous completions without open-coding access to struct completion. Assisted-by: Claude:claude-opus-5 Signed-off-by: Mike Lothian <[email protected]> --- rust/kernel/sync/completion.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/completion.rs b/rust/kernel/sync/completion.rs index 35ff049ff078..df4f6e11ef37 100644 --- a/rust/kernel/sync/completion.rs +++ b/rust/kernel/sync/completion.rs @@ -6,7 +6,12 @@ //! //! C header: [`include/linux/completion.h`](srctree/include/linux/completion.h) -use crate::{bindings, prelude::*, types::Opaque}; +use crate::{ + bindings, + prelude::*, + time::Jiffies, + types::Opaque, // +}; /// Synchronization primitive to signal when a certain task has been completed. /// @@ -90,6 +95,15 @@ fn as_raw(&self) -> *mut bindings::completion { self.inner.get() } + /// Signal one task waiting for this completion. + /// + /// If no task is waiting, the signal is retained and consumed by a future waiter. + #[inline] + pub fn complete(&self) { + // SAFETY: `self.as_raw()` is a pointer to a valid `struct completion`. + unsafe { bindings::complete(self.as_raw()) }; + } + /// Signal all tasks waiting on this completion. /// /// This method wakes up all tasks waiting on this completion; after this operation the @@ -111,4 +125,13 @@ pub fn wait_for_completion(&self) { // SAFETY: `self.as_raw()` is a pointer to a valid `struct completion`. unsafe { bindings::wait_for_completion(self.as_raw()) }; } + + /// Wait at most `timeout` jiffies for one completion signal. + /// + /// Returns `true` after consuming a signal and `false` when the timeout expires. + #[inline] + pub fn wait_for_completion_timeout(&self, timeout: Jiffies) -> bool { + // SAFETY: `self.as_raw()` is a pointer to a valid `struct completion`. + unsafe { bindings::wait_for_completion_timeout(self.as_raw(), timeout) != 0 } + } }