public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "FUJITA Tomonori" <fujita.tomonori@gmail.com>,
	<boqun.feng@gmail.com>, <ojeda@kernel.org>,
	<peterz@infradead.org>, <will@kernel.org>
Cc: <a.hindborg@kernel.org>, <aliceryhl@google.com>,
	<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 v3 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use
Date: Mon, 12 Jan 2026 12:51:53 +0000	[thread overview]
Message-ID: <DFMMHWYPIFWB.1DLKD9OLUNGNU@garyguo.net> (raw)
In-Reply-To: <20260111050558.3147975-3-fujita.tomonori@gmail.com>

On Sun Jan 11, 2026 at 5:05 AM GMT, FUJITA Tomonori wrote:
> Provide AtomicFlag as a thin wrapper around Atomic<Flag> so users can
> work with a simple bool API for load/store/xchg while keeping ordering
> controls.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
>  rust/kernel/sync/atomic.rs | 46 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
>
> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
> index 473d3c07e234..c3039289b989 100644
> --- a/rust/kernel/sync/atomic.rs
> +++ b/rust/kernel/sync/atomic.rs
> @@ -622,3 +622,49 @@ 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.

Need an `#[inline]` here.

> +    pub const fn new(b: bool) -> Self {
> +        if b {
> +            AtomicFlag(Atomic::new(Flag::Set))
> +        } else {
> +            AtomicFlag(Atomic::new(Flag::Clear))
> +        }
> +    }

Would it make sense to have an inherent method on `Flag`?

    impl Flag {
        #[inline]
        pub const fn new(b: bool) -> Self { .. }
    }

Best,
Gary

> +
> +    /// 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)
> +    }
> +
> +    /// Stores a value to the atomic flag and returns the previous value.
> +    #[inline(always)]
> +    pub fn xchg<Ordering: ordering::Ordering>(&self, b: bool, o: Ordering) -> bool {
> +        self.0.xchg(b.into(), o).into()
> +    }
> +
> +    /// Store a value to the atomic flag if the current value is equal to `old`.
> +    #[inline(always)]
> +    pub fn cmpxchg<Ordering: ordering::Ordering>(
> +        &self,
> +        old: bool,
> +        new: bool,
> +        o: Ordering,
> +    ) -> Result<bool, bool> {
> +        match self.0.cmpxchg(old.into(), new.into(), o) {
> +            Ok(v) => Ok(v.into()),
> +            Err(v) => Err(v.into()),
> +        }
> +    }
> +}


  reply	other threads:[~2026-01-12 12:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-11  5:05 [PATCH v3 0/2] rust: sync: atomic flag helpers FUJITA Tomonori
2026-01-11  5:05 ` [PATCH v3 1/2] rust: sync: atomic: Add i32-backed Flag for atomic booleans FUJITA Tomonori
2026-01-12  9:18   ` Alice Ryhl
2026-01-12 22:30     ` FUJITA Tomonori
2026-01-13  1:12       ` Gary Guo
2026-01-13  1:45         ` FUJITA Tomonori
2026-01-11  5:05 ` [PATCH v3 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use FUJITA Tomonori
2026-01-12 12:51   ` Gary Guo [this message]
2026-01-12 22:40     ` 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=DFMMHWYPIFWB.1DLKD9OLUNGNU@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --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