[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260111120209.6133-1-boqun.feng@gmail.com>
Date: Sun, 11 Jan 2026 20:01:36 +0800
From: Boqun Feng <boqun.feng@...il.com>
To: "Peter Zijlstra" <peterz@...radead.org>,
"Ingo Molnar" <mingo@...nel.org>
Cc: rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
"Will Deacon" <will@...nel.org>,
"Mark Rutland" <mark.rutland@....com>,
"Thomas Gleixner" <tglx@...utronix.de>,
"Miguel Ojeda" <ojeda@...nel.org>,
"Gary Guo" <gary@...yguo.net>,
"Alice Ryhl" <aliceryhl@...gle.com>,
"Andreas Hindborg" <a.hindborg@...nel.org>,
"Benno Lossin" <lossin@...nel.org>,
"Danilo Krummrich" <dakr@...nel.org>,
FUJITA Tomonori <fujita.tomonori@...il.com>,
Boqun Feng <boqun.feng@...il.com>
Subject: [PATCH 03/36] rust: sync: set_once: Implement Send and Sync
From: FUJITA Tomonori <fujita.tomonori@...il.com>
Implement Send and Sync for SetOnce<T> to allow it to be used across
thread boundaries.
Send: SetOnce<T> can be transferred across threads when T: Send, as
the contained value is also transferred and will be dropped on the
destination thread.
Sync: SetOnce<T> can be shared across threads when T: Sync, as
as_ref() provides shared references &T and atomic operations ensure
proper synchronization. Since the inner T may be dropped on any
thread, we also require T: Send.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@...il.com>
Reviewed-by: Andreas Hindborg <a.hindborg@...nel.org>
Reviewed-by: Gary Guo <gary@...yguo.net>
Signed-off-by: Boqun Feng <boqun.feng@...il.com>
Link: https://patch.msgid.link/20251216000901.221375-1-fujita.tomonori@gmail.com
---
rust/kernel/sync/set_once.rs | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs
index bdba601807d8..139cef05e935 100644
--- a/rust/kernel/sync/set_once.rs
+++ b/rust/kernel/sync/set_once.rs
@@ -123,3 +123,11 @@ fn drop(&mut self) {
}
}
}
+
+// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can.
+unsafe impl<T: Send> Send for SetOnce<T> {}
+
+// SAFETY: `SetOnce` synchronises access to the inner value via atomic operations,
+// so shared references are safe when `T: Sync`. Since the inner `T` may be dropped
+// on any thread, we also require `T: Send`.
+unsafe impl<T: Send + Sync> Sync for SetOnce<T> {}
--
2.51.0
Powered by blists - more mailing lists