Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0134651

Browse files
committed
Auto merge of #136316 - GrigorenkoPV:generic_atomic, r=Mark-Simulacrum
Create `Atomic<T>` type alias (rebase) Rebase of #130543. Additional changes: - Switch from `allow` to `expect` for `private_bounds` on `AtomicPrimitive` - Unhide `AtomicPrimitive::AtomicInner` from docs, because rustdoc shows the definition `pub type Atomic<T> = <T as AtomicPrimitive>::AtomicInner;` and generated links for it. - `NonZero` did not have this issue, because they kept the new alias private before the direction was changed. - Use `Atomic<_>` in more places, including inside `Once`'s `Futex`. This is possible thanks to rust-lang/rust-clippy#14125 The rest will either get moved back to #130543 or #130543 will be closed in favor of this instead. --- * ACP: rust-lang/libs-team#443 (comment) * Tracking issue: #130539
2 parents 21079f5 + df3dd87 commit 0134651

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+370
-265
lines changed

‎library/alloc/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#![feature(fmt_internals)]
123123
#![feature(fn_traits)]
124124
#![feature(formatting_options)]
125+
#![feature(generic_atomic)]
125126
#![feature(hasher_prefixfree_extras)]
126127
#![feature(inplace_iteration)]
127128
#![feature(iter_advance_by)]

‎library/alloc/src/sync.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use core::pin::{Pin, PinCoerceUnsized};
2626
use core::ptr::{self, NonNull};
2727
#[cfg(not(no_global_oom_handling))]
2828
use core::slice::from_raw_parts_mut;
29-
use core::sync::atomic;
3029
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
30+
use core::sync::atomic::{self, Atomic};
3131
use core::{borrow, fmt, hint};
3232

3333
#[cfg(not(no_global_oom_handling))]
@@ -369,12 +369,12 @@ impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
369369
// inner types.
370370
#[repr(C)]
371371
struct ArcInner<T: ?Sized> {
372-
strong: atomic::AtomicUsize,
372+
strong: Atomic<usize>,
373373

374374
// the value usize::MAX acts as a sentinel for temporarily "locking" the
375375
// ability to upgrade weak pointers or downgrade strong ones; this is used
376376
// to avoid races in `make_mut` and `get_mut`.
377-
weak: atomic::AtomicUsize,
377+
weak: Atomic<usize>,
378378

379379
data: T,
380380
}
@@ -2813,8 +2813,8 @@ impl<T, A: Allocator> Weak<T, A> {
28132813
/// Helper type to allow accessing the reference counts without
28142814
/// making any assertions about the data field.
28152815
struct WeakInner<'a> {
2816-
weak: &'a atomic::AtomicUsize,
2817-
strong: &'a atomic::AtomicUsize,
2816+
weak: &'a Atomic<usize>,
2817+
strong: &'a Atomic<usize>,
28182818
}
28192819

28202820
impl<T: ?Sized> Weak<T> {

‎library/core/src/sync/atomic.rs‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,100 @@ use crate::cell::UnsafeCell;
247247
use crate::hint::spin_loop;
248248
use crate::{fmt, intrinsics};
249249

250+
trait Sealed {}
251+
252+
/// A marker trait for primitive types which can be modified atomically.
253+
///
254+
/// This is an implementation detail for <code>[Atomic]\<T></code> which may disappear or be replaced at any time.
255+
///
256+
/// # Safety
257+
///
258+
/// Types implementing this trait must be primitives that can be modified atomically.
259+
///
260+
/// The associated `Self::AtomicInner` type must have the same size and bit validity as `Self`,
261+
/// but may have a higher alignment requirement, so the following `transmute`s are sound:
262+
///
263+
/// - `&mut Self::AtomicInner` as `&mut Self`
264+
/// - `Self` as `Self::AtomicInner` or the reverse
265+
#[unstable(
266+
feature = "atomic_internals",
267+
reason = "implementation detail which may disappear or be replaced at any time",
268+
issue = "none"
269+
)]
270+
#[expect(private_bounds)]
271+
pub unsafe trait AtomicPrimitive: Sized + Copy + Sealed {
272+
/// Temporary implementation detail.
273+
type AtomicInner: Sized;
274+
}
275+
276+
macro impl_atomic_primitive(
277+
$Atom:ident $(<$T:ident>)? ($Primitive:ty),
278+
size($size:literal),
279+
align($align:literal) $(,)?
280+
) {
281+
impl $(<$T>)? Sealed for $Primitive {}
282+
283+
#[unstable(
284+
feature = "atomic_internals",
285+
reason = "implementation detail which may disappear or be replaced at any time",
286+
issue = "none"
287+
)]
288+
#[cfg(target_has_atomic_load_store = $size)]
289+
unsafe impl $(<$T>)? AtomicPrimitive for $Primitive {
290+
type AtomicInner = $Atom $(<$T>)?;
291+
}
292+
}
293+
294+
impl_atomic_primitive!(AtomicBool(bool), size("8"), align(1));
295+
impl_atomic_primitive!(AtomicI8(i8), size("8"), align(1));
296+
impl_atomic_primitive!(AtomicU8(u8), size("8"), align(1));
297+
impl_atomic_primitive!(AtomicI16(i16), size("16"), align(2));
298+
impl_atomic_primitive!(AtomicU16(u16), size("16"), align(2));
299+
impl_atomic_primitive!(AtomicI32(i32), size("32"), align(4));
300+
impl_atomic_primitive!(AtomicU32(u32), size("32"), align(4));
301+
impl_atomic_primitive!(AtomicI64(i64), size("64"), align(8));
302+
impl_atomic_primitive!(AtomicU64(u64), size("64"), align(8));
303+
impl_atomic_primitive!(AtomicI128(i128), size("128"), align(16));
304+
impl_atomic_primitive!(AtomicU128(u128), size("128"), align(16));
305+
306+
#[cfg(target_pointer_width = "16")]
307+
impl_atomic_primitive!(AtomicIsize(isize), size("ptr"), align(2));
308+
#[cfg(target_pointer_width = "32")]
309+
impl_atomic_primitive!(AtomicIsize(isize), size("ptr"), align(4));
310+
#[cfg(target_pointer_width = "64")]
311+
impl_atomic_primitive!(AtomicIsize(isize), size("ptr"), align(8));
312+
313+
#[cfg(target_pointer_width = "16")]
314+
impl_atomic_primitive!(AtomicUsize(usize), size("ptr"), align(2));
315+
#[cfg(target_pointer_width = "32")]
316+
impl_atomic_primitive!(AtomicUsize(usize), size("ptr"), align(4));
317+
#[cfg(target_pointer_width = "64")]
318+
impl_atomic_primitive!(AtomicUsize(usize), size("ptr"), align(8));
319+
320+
#[cfg(target_pointer_width = "16")]
321+
impl_atomic_primitive!(AtomicPtr<T>(*mut T), size("ptr"), align(2));
322+
#[cfg(target_pointer_width = "32")]
323+
impl_atomic_primitive!(AtomicPtr<T>(*mut T), size("ptr"), align(4));
324+
#[cfg(target_pointer_width = "64")]
325+
impl_atomic_primitive!(AtomicPtr<T>(*mut T), size("ptr"), align(8));
326+
327+
/// A memory location which can be safely modified from multiple threads.
328+
///
329+
/// This has the same size and bit validity as the underlying type `T`. However,
330+
/// the alignment of this type is always equal to its size, even on targets where
331+
/// `T` has alignment less than its size.
332+
///
333+
/// For more about the differences between atomic types and non-atomic types as
334+
/// well as information about the portability of this type, please see the
335+
/// [module-level documentation].
336+
///
337+
/// **Note:** This type is only available on platforms that support atomic loads
338+
/// and stores of `T`.
339+
///
340+
/// [module-level documentation]: crate::sync::atomic
341+
#[unstable(feature = "generic_atomic", issue = "130539")]
342+
pub type Atomic<T> = <T as AtomicPrimitive>::AtomicInner;
343+
250344
// Some architectures don't have byte-sized atomics, which results in LLVM
251345
// emulating them using a LL/SC loop. However for AtomicBool we can take
252346
// advantage of the fact that it only ever contains 0 or 1 and use atomic OR/AND

‎library/std/src/alloc.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#![stable(feature = "alloc_module", since = "1.28.0")]
5858

5959
use core::ptr::NonNull;
60-
use core::sync::atomic::{AtomicPtr, Ordering};
60+
use core::sync::atomic::{Atomic,AtomicPtr, Ordering};
6161
use core::{hint, mem, ptr};
6262

6363
#[stable(feature = "alloc_module", since = "1.28.0")]
@@ -287,7 +287,7 @@ unsafe impl Allocator for System {
287287
}
288288
}
289289

290-
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
290+
static HOOK: Atomic<*mut()> = AtomicPtr::new(ptr::null_mut());
291291

292292
/// Registers a custom allocation error hook, replacing any that was previously registered.
293293
///

‎library/std/src/backtrace.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ use crate::backtrace_rs::{self, BytesOrWideString};
9292
use crate::ffi::c_void;
9393
use crate::panic::UnwindSafe;
9494
use crate::sync::LazyLock;
95-
use crate::sync::atomic::AtomicU8;
9695
use crate::sync::atomic::Ordering::Relaxed;
96+
use crate::sync::atomic::{Atomic, AtomicU8};
9797
use crate::sys::backtrace::{lock, output_filename, set_image_base};
9898
use crate::{env, fmt};
9999

@@ -254,7 +254,7 @@ impl Backtrace {
254254
// Cache the result of reading the environment variables to make
255255
// backtrace captures speedy, because otherwise reading environment
256256
// variables every time can be somewhat slow.
257-
static ENABLED: AtomicU8 = AtomicU8::new(0);
257+
static ENABLED: Atomic<u8> = AtomicU8::new(0);
258258
match ENABLED.load(Relaxed) {
259259
0 => {}
260260
1 => return false,

‎library/std/src/io/stdio.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::io::{
1111
self, BorrowedCursor, BufReader, IoSlice, IoSliceMut, LineWriter, Lines, SpecReadByte,
1212
};
1313
use crate::panic::{RefUnwindSafe, UnwindSafe};
14-
use crate::sync::atomic::{AtomicBool, Ordering};
14+
use crate::sync::atomic::{Atomic,AtomicBool, Ordering};
1515
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantLock, ReentrantLockGuard};
1616
use crate::sys::stdio;
1717
use crate::thread::AccessError;
@@ -37,7 +37,7 @@ thread_local! {
3737
/// have a consistent order between set_output_capture and print_to *within
3838
/// the same thread*. Within the same thread, things always have a perfectly
3939
/// consistent order. So Ordering::Relaxed is fine.
40-
static OUTPUT_CAPTURE_USED: AtomicBool = AtomicBool::new(false);
40+
static OUTPUT_CAPTURE_USED: Atomic<bool> = AtomicBool::new(false);
4141

4242
/// A handle to a raw instance of the standard input stream of this process.
4343
///

‎library/std/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@
343343
#![feature(float_gamma)]
344344
#![feature(float_minimum_maximum)]
345345
#![feature(fmt_internals)]
346+
#![feature(generic_atomic)]
346347
#![feature(hasher_prefixfree_extras)]
347348
#![feature(hashmap_internals)]
348349
#![feature(hint_must_use)]

‎library/std/src/os/uefi/env.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use crate::ffi::c_void;
66
use crate::ptr::NonNull;
7-
use crate::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
7+
use crate::sync::atomic::{Atomic,AtomicBool, AtomicPtr, Ordering};
88

9-
static SYSTEM_TABLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut());
10-
static IMAGE_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut());
9+
static SYSTEM_TABLE: Atomic<*mutc_void> = AtomicPtr::new(crate::ptr::null_mut());
10+
static IMAGE_HANDLE: Atomic<*mutc_void> = AtomicPtr::new(crate::ptr::null_mut());
1111
// Flag to check if BootServices are still valid.
1212
// Start with assuming that they are not available
13-
static BOOT_SERVICES_FLAG: AtomicBool = AtomicBool::new(false);
13+
static BOOT_SERVICES_FLAG: Atomic<bool> = AtomicBool::new(false);
1414

1515
/// Initializes the global System Table and Image Handle pointers.
1616
///

‎library/std/src/os/xous/services.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::sync::atomic::{AtomicU32, Ordering};
1+
use core::sync::atomic::{Atomic,AtomicU32, Ordering};
22

33
use crate::os::xous::ffi::Connection;
44

@@ -106,7 +106,7 @@ pub fn try_connect(name: &str) -> Option<Connection> {
106106
ns::try_connect_with_name(name)
107107
}
108108

109-
static NAME_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
109+
static NAME_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
110110

111111
/// Returns a `Connection` to the name server. If the name server has not been started,
112112
/// then this call will block until the name server has been started. The `Connection`

‎library/std/src/os/xous/services/dns.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::sync::atomic::{AtomicU32, Ordering};
1+
use core::sync::atomic::{Atomic,AtomicU32, Ordering};
22

33
use crate::os::xous::ffi::Connection;
44
use crate::os::xous::services::connect;
@@ -17,7 +17,7 @@ impl Into<usize> for DnsLendMut {
1717
/// Returns a `Connection` to the DNS lookup server. This server is used for
1818
/// querying domain name values.
1919
pub(crate) fn dns_server() -> Connection {
20-
static DNS_CONNECTION: AtomicU32 = AtomicU32::new(0);
20+
static DNS_CONNECTION: Atomic<u32> = AtomicU32::new(0);
2121
let cid = DNS_CONNECTION.load(Ordering::Relaxed);
2222
if cid != 0 {
2323
return cid.into();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /