From: FUJITA Tomonori Date: Tue, 16 Dec 2025 00:09:01 +0000 (+0900) Subject: rust: sync: set_once: Implement Send and Sync X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8a581130b1cbc17c702298b8325e3df98c792760;p=thirdparty%2Fkernel%2Flinux.git rust: sync: set_once: Implement Send and Sync Implement Send and Sync for SetOnce to allow it to be used across thread boundaries. Send: SetOnce 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 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 Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Signed-off-by: Boqun Feng Link: https://patch.msgid.link/20251216000901.221375-1-fujita.tomonori@gmail.com --- diff --git a/rust/kernel/sync/set_once.rs b/rust/kernel/sync/set_once.rs index bdba601807d8b..139cef05e935f 100644 --- a/rust/kernel/sync/set_once.rs +++ b/rust/kernel/sync/set_once.rs @@ -123,3 +123,11 @@ impl Drop for SetOnce { } } } + +// SAFETY: `SetOnce` can be transferred across thread boundaries iff the data it contains can. +unsafe impl Send for SetOnce {} + +// 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 Sync for SetOnce {}