From: Gary Guo <gary@garyguo.net>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: boqun.feng@gmail.com, ojeda@kernel.org, a.hindborg@kernel.org,
aliceryhl@google.com, bjorn3_gh@protonmail.com, dakr@kernel.org,
lossin@kernel.org, tmgross@umich.edu, acourbot@nvidia.com,
rust-for-linux@vger.kernel.org, linux-arch@vger.kernel.org
Subject: Re: [PATCH v1] rust: sync: atomic: Add i32-backed Flag for atomic booleans
Date: Sat, 3 Jan 2026 19:05:10 +0000 [thread overview]
Message-ID: <20260103190511.2d267164.gary@garyguo.net> (raw)
In-Reply-To: <20260103.194448.560764475765900721.fujita.tomonori@gmail.com>
On Sat, 03 Jan 2026 19:44:48 +0900 (JST)
FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:
> On Thu, 1 Jan 2026 21:04:30 +0000
> Gary Guo <gary@garyguo.net> wrote:
>
> > On Thu, 1 Jan 2026 19:27:18 +0900
> > FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:
> >
> >> Add a new Flag enum (Clear/Set) with #[repr(i32)] and implement
> >> AtomicType for it, so users can use Atomic<Flag> for boolean flags.
> >>
> >> Document when Atomic<Flag> is generally preferable to Atomic<bool>: in
> >> particular, when RMW operations such as xchg()/cmpxchg() may be used
> >> and minimizing memory usage is not the top priority. On some
> >> architectures without byte-sized RMW instructions, Atomic<bool> can be
> >> slower for RMW operations.
> >>
> >> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> >> ---
> >> rust/kernel/sync/atomic.rs | 35 +++++++++++++++++++++++++++++++++++
> >> 1 file changed, 35 insertions(+)
> >>
> >> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
> >> index 4aebeacb961a..d98ab51ae4fc 100644
> >> --- a/rust/kernel/sync/atomic.rs
> >> +++ b/rust/kernel/sync/atomic.rs
> >> @@ -560,3 +560,38 @@ pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering)
> >> unsafe { from_repr(ret) }
> >> }
> >> }
> >> +
> >> +/// An atomic flag type backed by `i32`.
> >
> > I would recommend that we document that the backing type is the
> > (perf-)optimal type on the target architecure, so arch can decide to use
> > i8 as backing type if they prefer.
>
> I'm not sure I fully understand the intent yet.
>
> Do you mean we should document Flag as being backed by the
> (perf-)optimal integer type for the target architecture, so that the
> backing type can remain an implementation detail and potentially be
> selected per-arch (e.g. i8 on x86) via cfg?
Yes, I don't want anyone to rely on it being i32 (at least for now, before
a concrete use case of doing so appears).
>
> Yeah, that sounds like a good addition.
>
> +
> +impl From<Flag> for bool {
> + fn from(f: Flag) -> Self {
> + f == Flag::Set
> + }
> +}
A `#[inline]` is warranted here. Also, it'll be good to have the
conversion for the other direction too.
Best,
Gary
next prev parent reply other threads:[~2026-01-03 19:05 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-01 10:27 [PATCH v1] rust: sync: atomic: Add i32-backed Flag for atomic booleans FUJITA Tomonori
2026-01-01 21:04 ` Gary Guo
2026-01-03 10:44 ` FUJITA Tomonori
2026-01-03 19:05 ` Gary Guo [this message]
2026-01-03 21:53 ` FUJITA Tomonori
2026-01-04 8:36 ` Boqun Feng
2026-01-04 12:07 ` Miguel Ojeda
2026-01-08 5:17 ` FUJITA Tomonori
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=20260103190511.2d267164.gary@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=linux-arch@vger.kernel.org \
--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