public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: boqun.feng@gmail.com, ojeda@kernel.org, peterz@infradead.org,
	 will@kernel.org, a.hindborg@kernel.org,
	bjorn3_gh@protonmail.com,  dakr@kernel.org, gary@garyguo.net,
	lossin@kernel.org, mark.rutland@arm.com,  tmgross@umich.edu,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2] rust: sync: atomic: Add i32-backed Flag for atomic booleans
Date: Fri, 9 Jan 2026 08:23:04 +0000	[thread overview]
Message-ID: <aWC66BAmhDgyqpfJ@google.com> (raw)
In-Reply-To: <20260109.093641.1161098806794561807.fujita.tomonori@gmail.com>

On Fri, Jan 09, 2026 at 09:36:41AM +0900, FUJITA Tomonori wrote:
> On Thu, 8 Jan 2026 13:54:58 +0100
> Alice Ryhl <aliceryhl@google.com> wrote:
> 
> > On Thu, Jan 8, 2026 at 1:50 PM FUJITA Tomonori
> > <fujita.tomonori@gmail.com> wrote:
> >>
> >> Add a new Flag enum (Clear/Set) and implement AtomicType for it, so
> >> users can use Atomic<Flag> for boolean flags.
> >>
> >> The backing integer type is an implementation detail; it is currently
> >> i32 but may change in the future.
> >>
> >> 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>
> > 
> > Can we have a `struct AtomicFlag(Atomic<Flag>)` here whose load() /
> > store() methods use real booleans? Otherwise this seems inconvenient
> > to use.
> 
> Do you mean something along these lines?
> 
> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
> index 473d3c07e234..19750be1a239 100644
> --- a/rust/kernel/sync/atomic.rs
> +++ b/rust/kernel/sync/atomic.rs
> @@ -622,3 +622,29 @@ fn from(b: bool) -> Self {
>          }
>      }
>  }
> +
> +/// A convenience wrapper around [`Atomic<Flag>`] that exposes a `bool` API.
> +pub struct AtomicFlag(Atomic<Flag>);
> +
> +impl AtomicFlag {
> +    /// Creates a new atomic flag.
> +    pub const fn new(b: bool) -> Self {
> +        if b {
> +            AtomicFlag(Atomic::new(Flag::Set))
> +        } else {
> +            AtomicFlag(Atomic::new(Flag::Clear))
> +        }
> +    }
> +
> +    /// Loads the value from the atomic flag.
> +    #[inline(always)]
> +    pub fn load<Ordering: ordering::AcquireOrRelaxed>(&self, o: Ordering) -> bool {
> +        self.0.load(o).into()
> +    }
> +
> +    /// Stores a value to the atomic flag.
> +    #[inline(always)]
> +    pub fn store<Ordering: ordering::ReleaseOrRelaxed>(&self, b: bool, o: Ordering) {
> +        self.0.store(b.into(), o)
> +    }
> +}

Yes, that's what I meant.

Alice

  parent reply	other threads:[~2026-01-09  8:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-08 12:50 [PATCH v2] rust: sync: atomic: Add i32-backed Flag for atomic booleans FUJITA Tomonori
2026-01-08 12:54 ` Alice Ryhl
2026-01-09  0:36   ` FUJITA Tomonori
2026-01-09  0:50     ` FUJITA Tomonori
2026-01-09  8:23     ` Alice Ryhl [this message]
2026-01-10 12:48       ` FUJITA Tomonori
2026-01-10 14:18         ` Alice Ryhl
2026-01-11  5:11           ` 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=aWC66BAmhDgyqpfJ@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=mark.rutland@arm.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.org \
    /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