All of lore.kernel.org
 help / color / mirror / Atom feed
From: jens.korinth@tuta.io
To: Daniel Sedlak <daniel@sedlak.dev>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Rust For Linux" <rust-for-linux@vger.kernel.org>,
	"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
	"Dirk Behme" <dirk.behme@gmail.com>,
	"Linux Kernel" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v4 1/3] rust: Add `OnceLite` for executing code once
Date: Wed, 27 Nov 2024 20:46:01 +0100 (CET)	[thread overview]
Message-ID: <OCj9As8--F-9@tuta.io> (raw)
In-Reply-To: <230b3602-5d68-4e79-969d-0d2df1fdf033@sedlak.dev>

> Have you considered it to be implemented like `AtomicU32`? I think™ that
> one atomic variable is more than enough.

Just to clarify - you mean something like this? Nevermind the magic numbers,
I'd replace them, of course.

diff --git a/rust/kernel/once_lite.rs b/rust/kernel/once_lite.rs
index 723c3244fc85..0622ecbfced5 100644
--- a/rust/kernel/once_lite.rs
+++ b/rust/kernel/once_lite.rs
@@ -16,7 +16,7 @@
//!
//! Reference: <https://doc.rust-lang.org/std/sync/struct.Once.html>
 
-use core::sync::atomic::{AtomicBool, Ordering::Relaxed};
+use core::sync::atomic::{AtomicU32, Ordering::Acquire, Ordering::Relaxed};
 
/// A low-level synchronization primitive for one-time global execution.
///
@@ -44,13 +44,13 @@
/// assert_eq!(x, 42);
/// ```
///
-pub struct OnceLite(AtomicBool, AtomicBool);
+pub struct OnceLite(AtomicU32);
 
impl OnceLite {
     /// Creates a new `OnceLite` value.
     #[inline(always)]
     pub const fn new() -> Self {
-        Self(AtomicBool::new(false), AtomicBool::new(false))
+        Self(AtomicU32::new(0))
     }
 
     /// Performs an initialization routine once and only once. The given
@@ -71,10 +71,10 @@ pub const fn new() -> Self {
     /// [`DO_ONCE_LITE_IF`]: srctree/include/once_lite.h
     #[inline(always)]
     pub fn call_once<F: FnOnce()>(&self, f: F) {
-        if !self.0.load(Relaxed) && !self.0.swap(true, Relaxed) {
-            f()
+        if self.0.load(Relaxed) == 0 && self.0.compare_exchange(0, 1, Acquire, Relaxed) == Ok(0) {
+            f();
+            self.0.store(2, Relaxed);
         };
-        self.1.store(true, Relaxed);
     }
 
     /// Returns `true` if some `call_once` call has completed successfully.
@@ -98,7 +98,7 @@ pub fn call_once<F: FnOnce()>(&self, f: F) {
     /// ```
     #[inline(always)]
     pub fn is_completed(&self) -> bool {
-        self.1.load(Relaxed)
+        self.0.load(Relaxed) > 1
     }
}

> The `rust` part should be default value for rustdoc tests, can we please
> omit that?

Will do!

Jens

  reply	other threads:[~2024-11-27 19:52 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <8gSHb0iuAT_Wz0rR1-qsnFIVndSpW229fA0lq-fslngTt5k1ooiMw5eAIc82F26Mdma4nkyU4X7_1RLZcnQf-Q==@protonmail.internalid>
2024-11-26 16:40 ` [PATCH v4 0/3] rust: Add pr_*_once macros Jens Korinth
2024-11-26 16:40   ` Jens Korinth via B4 Relay
2024-11-26 16:40   ` [PATCH v4 1/3] rust: Add `OnceLite` for executing code once Jens Korinth
2024-11-26 16:40     ` Jens Korinth via B4 Relay
2024-11-26 18:57     ` Daniel Sedlak
2024-11-27 19:46       ` jens.korinth [this message]
2024-11-29  8:22         ` Daniel Sedlak
2024-11-26 19:00     ` Miguel Ojeda
2024-11-27 12:26     ` Alice Ryhl
2024-11-27 20:12       ` jens.korinth
2024-11-27 20:18         ` Alice Ryhl
2024-12-05  6:49           ` jens.korinth
2024-12-05  8:47             ` Alice Ryhl
2025-02-04 11:11     ` Andreas Hindborg
2024-11-26 16:40   ` [PATCH v4 2/3] rust: print: Add pr_*_once macros Jens Korinth
2024-11-26 16:40     ` Jens Korinth via B4 Relay
2024-11-26 16:40   ` [PATCH v4 3/3] rust: error: Replace pr_warn by pr_warn_once Jens Korinth
2024-11-26 16:40     ` Jens Korinth via B4 Relay
2024-11-26 17:07     ` Miguel Ojeda
2024-11-27 20:39       ` jens.korinth
2024-11-30 18:18         ` Miguel Ojeda
2024-12-05  6:30           ` jens.korinth
2024-12-05 11:55             ` Miguel Ojeda
2024-12-05 19:22               ` jens.korinth
2024-11-26 16:59   ` [PATCH v4 0/3] rust: Add pr_*_once macros Miguel Ojeda
2025-02-11 15:04   ` Andreas Hindborg
2025-02-11 15:29     ` jens.korinth
2025-02-11 15:42       ` Andreas Hindborg
2025-07-17 16:07         ` [PATCH v4 0/3] rust: Add pr_*_once macros134 Onur Özkan
2025-07-19  3:33           ` Boqun Feng
2025-07-19 16:33             ` Onur

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=OCj9As8--F-9@tuta.io \
    --to=jens.korinth@tuta.io \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel@sedlak.dev \
    --cc=dirk.behme@gmail.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.