From: Alice Ryhl <aliceryhl@google.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: ojeda@kernel.org, a.hindborg@kernel.org, gary@garyguo.net,
bjorn3_gh@protonmail.com, boqun.feng@gmail.com, dakr@kernel.org,
lossin@kernel.org, tmgross@umich.edu,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2] rust: sync: set_once: Implement Send and Sync
Date: Wed, 17 Dec 2025 20:45:35 +0000 [thread overview]
Message-ID: <aUMWb2VvA48s0jur@google.com> (raw)
In-Reply-To: <20251216000901.221375-1-fujita.tomonori@gmail.com>
On Tue, Dec 16, 2025 at 09:09:01AM +0900, FUJITA Tomonori wrote:
> 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@gmail.com>
> ---
> v2:
> - update the `Sync` impl to require `T: Send + Sync` with the comment adjusted
> v1: https://lore.kernel.org/rust-for-linux/20251211230919.1303926-2-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> {}
The reason you need Sync is because &SetOnce<T> allows you to access &T.
It's not because atomic operations are used.
Alice
next prev parent reply other threads:[~2025-12-17 20:45 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <M2WR1VEvXZ1Q6GMTaONdJJTInO1AhDjAHfEKH0smTXBb9W-NUBo297K6Vzaubm_onBroEaFLIrzsgMDQbU0pbw==@protonmail.internalid>
2025-12-16 0:09 ` [PATCH v2] rust: sync: set_once: Implement Send and Sync FUJITA Tomonori
2025-12-16 12:32 ` Gary Guo
2025-12-17 10:38 ` Andreas Hindborg
2025-12-17 20:44 ` Boqun Feng
2025-12-17 20:45 ` Alice Ryhl [this message]
2025-12-17 21:10 ` Boqun Feng
2025-12-18 7:58 ` Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aUMWb2VvA48s0jur@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox