Skip to content

Commit fea08d4

Browse files
CAD97GrigorenkoPV
andcommitted
use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files Co-authored-by: Pavel Grigorenko <[email protected]>
1 parent e117c9d commit fea08d4

Some content is hidden

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

77 files changed

+262
-255
lines changed

Diff for: library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#![feature(fmt_internals)]
121121
#![feature(fn_traits)]
122122
#![feature(formatting_options)]
123+
#![feature(generic_atomic)]
123124
#![feature(hasher_prefixfree_extras)]
124125
#![feature(inplace_iteration)]
125126
#![feature(iter_advance_by)]

Diff for: library/alloc/src/sync.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use core::pin::{Pin, PinCoerceUnsized};
2525
use core::ptr::{self, NonNull};
2626
#[cfg(not(no_global_oom_handling))]
2727
use core::slice::from_raw_parts_mut;
28-
use core::sync::atomic;
2928
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
29+
use core::sync::atomic::{self, Atomic};
3030
use core::{borrow, fmt, hint};
3131

3232
#[cfg(not(no_global_oom_handling))]
@@ -348,12 +348,12 @@ impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
348348
// inner types.
349349
#[repr(C)]
350350
struct ArcInner<T: ?Sized> {
351-
strong: atomic::AtomicUsize,
351+
strong: Atomic<usize>,
352352

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

358358
data: T,
359359
}
@@ -2724,8 +2724,8 @@ impl<T, A: Allocator> Weak<T, A> {
27242724
/// Helper type to allow accessing the reference counts without
27252725
/// making any assertions about the data field.
27262726
struct WeakInner<'a> {
2727-
weak: &'a atomic::AtomicUsize,
2728-
strong: &'a atomic::AtomicUsize,
2727+
weak: &'a Atomic<usize>,
2728+
strong: &'a Atomic<usize>,
27292729
}
27302730

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

Diff for: library/std/src/alloc.rs

+2-2
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
///

Diff for: library/std/src/backtrace.rs

+2-2
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,

Diff for: library/std/src/io/stdio.rs

+2-2
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
///

Diff for: library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
#![feature(float_gamma)]
337337
#![feature(float_minimum_maximum)]
338338
#![feature(fmt_internals)]
339+
#![feature(generic_atomic)]
339340
#![feature(hasher_prefixfree_extras)]
340341
#![feature(hashmap_internals)]
341342
#![feature(hint_must_use)]

Diff for: library/std/src/os/uefi/env.rs

+4-4
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<*mut c_void> = AtomicPtr::new(crate::ptr::null_mut());
10+
static IMAGE_HANDLE: Atomic<*mut c_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
///

Diff for: library/std/src/os/xous/services.rs

+2-2
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`

Diff for: library/std/src/os/xous/services/dns.rs

+2-2
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();

Diff for: library/std/src/os/xous/services/log.rs

+2-2
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

@@ -64,7 +64,7 @@ impl Into<usize> for LogLend {
6464
/// running. It is safe to call this multiple times, because the address is
6565
/// shared among all threads in a process.
6666
pub(crate) fn log_server() -> Connection {
67-
static LOG_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
67+
static LOG_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
6868

6969
let cid = LOG_SERVER_CONNECTION.load(Ordering::Relaxed);
7070
if cid != 0 {

Diff for: library/std/src/os/xous/services/net.rs

+2-2
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;
@@ -84,7 +84,7 @@ impl<'a> Into<[usize; 5]> for NetBlockingScalar {
8484
/// Returns a `Connection` to the Network server. This server provides all
8585
/// OS-level networking functions.
8686
pub(crate) fn net_server() -> Connection {
87-
static NET_CONNECTION: AtomicU32 = AtomicU32::new(0);
87+
static NET_CONNECTION: Atomic<u32> = AtomicU32::new(0);
8888
let cid = NET_CONNECTION.load(Ordering::Relaxed);
8989
if cid != 0 {
9090
return cid.into();

Diff for: library/std/src/os/xous/services/systime.rs

+2-2
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, connect};
44

@@ -17,7 +17,7 @@ impl Into<[usize; 5]> for SystimeScalar {
1717
/// Returns a `Connection` to the systime server. This server is used for reporting the
1818
/// realtime clock.
1919
pub(crate) fn systime_server() -> Connection {
20-
static SYSTIME_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
20+
static SYSTIME_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
2121
let cid = SYSTIME_SERVER_CONNECTION.load(Ordering::Relaxed);
2222
if cid != 0 {
2323
return cid.into();

Diff for: library/std/src/os/xous/services/ticktimer.rs

+2-2
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

@@ -31,7 +31,7 @@ impl Into<[usize; 5]> for TicktimerScalar {
3131
/// Returns a `Connection` to the ticktimer server. This server is used for synchronization
3232
/// primitives such as sleep, Mutex, and Condvar.
3333
pub(crate) fn ticktimer_server() -> Connection {
34-
static TICKTIMER_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
34+
static TICKTIMER_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
3535
let cid = TICKTIMER_SERVER_CONNECTION.load(Ordering::Relaxed);
3636
if cid != 0 {
3737
return cid.into();

Diff for: library/std/src/panic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![stable(feature = "std_panic", since = "1.9.0")]
44

55
use crate::any::Any;
6-
use crate::sync::atomic::{AtomicU8, Ordering};
6+
use crate::sync::atomic::{Atomic, AtomicU8, Ordering};
77
use crate::sync::{Condvar, Mutex, RwLock};
88
use crate::thread::Result;
99
use crate::{collections, fmt, panicking};
@@ -466,7 +466,7 @@ impl BacktraceStyle {
466466
// that backtrace.
467467
//
468468
// Internally stores equivalent of an Option<BacktraceStyle>.
469-
static SHOULD_CAPTURE: AtomicU8 = AtomicU8::new(0);
469+
static SHOULD_CAPTURE: Atomic<u8> = AtomicU8::new(0);
470470

471471
/// Configures whether the default panic hook will capture and display a
472472
/// backtrace.

Diff for: library/std/src/panicking.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::any::Any;
2121
use crate::io::try_set_output_capture;
2222
use crate::mem::{self, ManuallyDrop};
2323
use crate::panic::{BacktraceStyle, PanicHookInfo};
24-
use crate::sync::atomic::{AtomicBool, Ordering};
24+
use crate::sync::atomic::{Atomic, AtomicBool, Ordering};
2525
use crate::sync::{PoisonError, RwLock};
2626
use crate::sys::backtrace;
2727
use crate::sys::stdio::panic_output;
@@ -287,7 +287,7 @@ fn default_hook(info: &PanicHookInfo<'_>) {
287287
};
288288
});
289289

290-
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
290+
static FIRST_PANIC: Atomic<bool> = AtomicBool::new(true);
291291

292292
match backtrace {
293293
// SAFETY: we took out a lock just a second ago.
@@ -372,7 +372,7 @@ pub mod panic_count {
372372
#[unstable(feature = "update_panic_count", issue = "none")]
373373
pub mod panic_count {
374374
use crate::cell::Cell;
375-
use crate::sync::atomic::{AtomicUsize, Ordering};
375+
use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
376376

377377
const ALWAYS_ABORT_FLAG: usize = 1 << (usize::BITS - 1);
378378

@@ -414,7 +414,7 @@ pub mod panic_count {
414414
//
415415
// Stealing a bit is fine because it just amounts to assuming that each
416416
// panicking thread consumes at least 2 bytes of address space.
417-
static GLOBAL_PANIC_COUNT: AtomicUsize = AtomicUsize::new(0);
417+
static GLOBAL_PANIC_COUNT: Atomic<usize> = AtomicUsize::new(0);
418418

419419
// Increases the global and local panic count, and returns whether an
420420
// immediate abort is required.

Diff for: library/std/src/sync/mpmc/array.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ use super::waker::SyncWaker;
1616
use crate::cell::UnsafeCell;
1717
use crate::mem::MaybeUninit;
1818
use crate::ptr;
19-
use crate::sync::atomic::{self, AtomicUsize, Ordering};
19+
use crate::sync::atomic::{self, Atomic, AtomicUsize, Ordering};
2020
use crate::time::Instant;
2121

2222
/// A slot in a channel.
2323
struct Slot<T> {
2424
/// The current stamp.
25-
stamp: AtomicUsize,
25+
stamp: Atomic<usize>,
2626

2727
/// The message in this slot. Either read out in `read` or dropped through
2828
/// `discard_all_messages`.
@@ -55,7 +55,7 @@ pub(crate) struct Channel<T> {
5555
/// represent the lap. The mark bit in the head is always zero.
5656
///
5757
/// Messages are popped from the head of the channel.
58-
head: CachePadded<AtomicUsize>,
58+
head: CachePadded<Atomic<usize>>,
5959

6060
/// The tail of the channel.
6161
///
@@ -64,7 +64,7 @@ pub(crate) struct Channel<T> {
6464
/// represent the lap. The mark bit indicates that the channel is disconnected.
6565
///
6666
/// Messages are pushed into the tail of the channel.
67-
tail: CachePadded<AtomicUsize>,
67+
tail: CachePadded<Atomic<usize>>,
6868

6969
/// The buffer holding slots.
7070
buffer: Box<[Slot<T>]>,

Diff for: library/std/src/sync/mpmc/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::waker::current_thread_id;
55
use crate::cell::Cell;
66
use crate::ptr;
77
use crate::sync::Arc;
8-
use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
8+
use crate::sync::atomic::{Atomic, AtomicPtr, AtomicUsize, Ordering};
99
use crate::thread::{self, Thread};
1010
use crate::time::Instant;
1111

@@ -19,10 +19,10 @@ pub struct Context {
1919
#[derive(Debug)]
2020
struct Inner {
2121
/// Selected operation.
22-
select: AtomicUsize,
22+
select: Atomic<usize>,
2323

2424
/// A slot into which another thread may store a pointer to its `Packet`.
25-
packet: AtomicPtr<()>,
25+
packet: Atomic<*mut ()>,
2626

2727
/// Thread handle.
2828
thread: Thread,

Diff for: library/std/src/sync/mpmc/counter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1+
use crate::sync::atomic::{Atomic, AtomicBool, AtomicUsize, Ordering};
22
use crate::{ops, process};
33

44
/// Reference counter internals.
55
struct Counter<C> {
66
/// The number of senders associated with the channel.
7-
senders: AtomicUsize,
7+
senders: Atomic<usize>,
88

99
/// The number of receivers associated with the channel.
10-
receivers: AtomicUsize,
10+
receivers: Atomic<usize>,
1111

1212
/// Set to `true` if the last sender or the last receiver reference deallocates the channel.
13-
destroy: AtomicBool,
13+
destroy: Atomic<bool>,
1414

1515
/// The internal channel.
1616
chan: C,

Diff for: library/std/src/sync/mpmc/list.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::cell::UnsafeCell;
99
use crate::marker::PhantomData;
1010
use crate::mem::MaybeUninit;
1111
use crate::ptr;
12-
use crate::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
12+
use crate::sync::atomic::{self, Atomic, AtomicPtr, AtomicUsize, Ordering};
1313
use crate::time::Instant;
1414

1515
// Bits indicating the state of a slot:
@@ -37,7 +37,7 @@ struct Slot<T> {
3737
msg: UnsafeCell<MaybeUninit<T>>,
3838

3939
/// The state of the slot.
40-
state: AtomicUsize,
40+
state: Atomic<usize>,
4141
}
4242

4343
impl<T> Slot<T> {
@@ -55,7 +55,7 @@ impl<T> Slot<T> {
5555
/// Each block in the list can hold up to `BLOCK_CAP` messages.
5656
struct Block<T> {
5757
/// The next block in the linked list.
58-
next: AtomicPtr<Block<T>>,
58+
next: Atomic<*mut Block<T>>,
5959

6060
/// Slots for messages.
6161
slots: [Slot<T>; BLOCK_CAP],
@@ -65,11 +65,11 @@ impl<T> Block<T> {
6565
/// Creates an empty block.
6666
fn new() -> Box<Block<T>> {
6767
// SAFETY: This is safe because:
68-
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
68+
// [1] `Block::next` (Atomic<*mut _>) may be safely zero initialized.
6969
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
7070
// [3] `Slot::msg` (UnsafeCell) may be safely zero initialized because it
7171
// holds a MaybeUninit.
72-
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
72+
// [4] `Slot::state` (Atomic<usize>) may be safely zero initialized.
7373
unsafe { Box::new_zeroed().assume_init() }
7474
}
7575

@@ -110,10 +110,10 @@ impl<T> Block<T> {
110110
#[derive(Debug)]
111111
struct Position<T> {
112112
/// The index in the channel.
113-
index: AtomicUsize,
113+
index: Atomic<usize>,
114114

115115
/// The block in the linked list.
116-
block: AtomicPtr<Block<T>>,
116+
block: Atomic<*mut Block<T>>,
117117
}
118118

119119
/// The token type for the list flavor.

0 commit comments

Comments
 (0)