All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/2] Add support for print exactly once
@ 2025-11-17  0:24 FUJITA Tomonori
  2025-11-17  0:24 ` [PATCH v3 1/2] rust: Add support for calling a function " FUJITA Tomonori
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: FUJITA Tomonori @ 2025-11-17  0:24 UTC (permalink / raw)
  To: alex.gaynor, ojeda, a.hindborg, aliceryhl, boqun.feng
  Cc: bjorn3_gh, dakr, gary, lossin, rust-for-linux, tmgross

This adds the Rust equivalent of the C's DO_ONCE_LITE and pr_*_once
macros.

A proposal for this feature was made in the past [1], but it didn't
reach consensus on the implementation and wasn't merged.

Unlike the previous proposal, this implements the C's DO_ONCE_LITE
mechanism using a single atomic variable. While it would be possible
to implement the feature entirely as a Rust macro, the functionality
that can be implemented as regular functions has been extracted and
implemented as the OnceLite struct for better code readability.

To make it clear that this feature is intended solely for
print-related functionality (just like in C), the implementation is
placed in print.rs. If an equivalent of std::sync::Once is needed in
the future, it should be based on SetOnce rather than OnceLite. Unlike
std::sync::Once and SetOnce, OnceLite only manages two states:
incomplete and complete.

The do_once_lite macro places the OnceLite object in the .data..once
section, which may be zeroed by memset at any time. While this means
tear reads might happen, OnceLite only manages two states (zero and
non-zero), so no actual problem occurs in practice.

OnceLite currently uses Atomic<i32>, but may be changed to use
Atomic<i8> [2] when it becomes available.

[1] https://lore.kernel.org/rust-for-linux/20241126-pr_once_macros-v4-0-410b8ca9643e@tuta.io/
[2] https://lore.kernel.org/rust-for-linux/20251115050305.3872412-1-fujita.tomonori@gmail.com/

v3:
- add relaxed load before expensive xchg
- move OnceLite to print.rs
v2: https://lore.kernel.org/rust-for-linux/20251112131619.3585510-1-fujita.tomonori@gmail.com/
- improve do_once_lite micro syntax
- move once_lite.rs to kernel/sync/
- add comments (including ORDERING)
- fix rustdoc errors
- use vertical layout for imports
v1: https://lore.kernel.org/rust-for-linux/20251105054731.3194118-1-fujita.tomonori@gmail.com/


FUJITA Tomonori (2):
  rust: Add support for calling a function exactly once
  rust: Add pr_*_once macros

 rust/kernel/print.rs | 153 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)


base-commit: 3b83f5d5e78ac5cddd811a5e431af73959864390
-- 
2.43.0


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-01-30 12:35 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-17  0:24 [PATCH v3 0/2] Add support for print exactly once FUJITA Tomonori
2025-11-17  0:24 ` [PATCH v3 1/2] rust: Add support for calling a function " FUJITA Tomonori
2025-12-18 11:38   ` Alice Ryhl
2025-12-23  6:49     ` FUJITA Tomonori
2025-12-23 12:18   ` Gary Guo
2025-12-25  6:14     ` FUJITA Tomonori
2025-11-17  0:24 ` [PATCH v3 2/2] rust: Add pr_*_once macros FUJITA Tomonori
2025-12-23 12:06   ` Gary Guo
2025-12-25  6:29     ` FUJITA Tomonori
2026-01-13  6:52     ` FUJITA Tomonori
2025-12-18 11:18 ` [PATCH v3 0/2] Add support for print exactly once FUJITA Tomonori
2026-01-30  4:51 ` Miguel Ojeda
2026-01-30 12:34   ` FUJITA Tomonori

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.