mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: sync: condvar: show interruptible wait in CondVar doc example
@ 2026-11-27 11:29 Alice Ryhl
  0 siblings, 0 replies; only message in thread
From: Alice Ryhl @ 2026-11-27 11:29 UTC (permalink / raw)
  To: Boqun Feng, Gary Guo, Lyude Paul, Daniel Almeida,
	Onur Özkan, Miguel Ojeda, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	Tamir Duberstein, Alexandre Courbot
  Cc: linux-kernel, rust-for-linux, Sid Nayyar, Alice Ryhl

The top-level `CondVar` documentation example currently only shows an
uninterruptible `wait()`, even though interruptible waits are usually
preferred so waiting tasks can respond to signals. Add an interruptible
wait function alongside the uninterruptible one in the example.

This came up while I was teaching the Rust for Linux course.

Suggested-by: Sid Nayyar <sidnayyar@google.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/sync/condvar.rs | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
index 69d58dfbad7b..fba82650842c 100644
--- a/rust/kernel/sync/condvar.rs
+++ b/rust/kernel/sync/condvar.rs
@@ -54,8 +54,22 @@ macro_rules! new_condvar {
 ///     value_changed: CondVar,
 /// }
 ///
-/// /// Waits for `e.value` to become `v`.
-/// fn wait_for_value(e: &Example, v: u32) {
+/// /// Waits for `e.value` to become `v`, returning `Err(ERESTARTSYS)` if a signal is received.
+/// ///
+/// /// Prefer interruptible waits when the caller can handle interruption, so waiting tasks remain
+/// /// killable.
+/// fn wait_for_value(e: &Example, v: u32) -> Result {
+///     let mut guard = e.value.lock();
+///     while *guard != v {
+///         if e.value_changed.wait_interruptible(&mut guard) {
+///             return Err(ERESTARTSYS);
+///         }
+///     }
+///     Ok(())
+/// }
+///
+/// /// Waits uninterruptibly for `e.value` to become `v`.
+/// fn wait_for_value_uninterruptible(e: &Example, v: u32) {
 ///     let mut guard = e.value.lock();
 ///     while *guard != v {
 ///         e.value_changed.wait(&mut guard);

---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261126-condvar-doc-interruptible-72bb4754c9bd

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-27 11:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 11:29 [PATCH] rust: sync: condvar: show interruptible wait in CondVar doc example Alice Ryhl

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®