Skip to content

Commit bacfd33

Browse files
committed
use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files
1 parent 87be1c0 commit bacfd33

Some content is hidden

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

85 files changed

+279
-370
lines changed

library/alloc/src/collections/binary_heap/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,9 @@ fn panic_safe() {
507507
use rand::seq::SliceRandom;
508508
use std::cmp;
509509
use std::panic::{self, AssertUnwindSafe};
510-
use std::sync::atomic::{AtomicUsize, Ordering};
510+
use std::sync::atomic::{Atomic, AtomicUsize, Ordering};
511511

512-
static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);
512+
static DROP_COUNTER: Atomic<usize> = AtomicUsize::new(0);
513513

514514
#[derive(Eq, PartialEq, Ord, Clone, Debug)]
515515
struct PanicOrd<T>(T, bool);

library/alloc/src/collections/btree/map/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::assert_matches::assert_matches;
1010
use std::iter;
1111
use std::ops::Bound::{Excluded, Included, Unbounded};
1212
use std::panic::{catch_unwind, AssertUnwindSafe};
13-
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
13+
use std::sync::atomic::{Atomic, AtomicUsize, Ordering::SeqCst};
1414

1515
// Minimum number of elements to insert, to guarantee a tree with 2 levels,
1616
// i.e., a tree who's root is an internal node at height 1, with edges to leaf nodes.
@@ -782,7 +782,7 @@ fn test_range_finding_ill_order_in_range_ord() {
782782
}
783783
}
784784

785-
static COMPARES: AtomicUsize = AtomicUsize::new(0);
785+
static COMPARES: Atomic<usize> = AtomicUsize::new(0);
786786
impl Ord for EvilTwin {
787787
fn cmp(&self, other: &Self) -> Ordering {
788788
let ord = self.0.cmp(&other.0);

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![feature(extend_one)]
126126
#![feature(fmt_internals)]
127127
#![feature(fn_traits)]
128+
#![feature(generic_atomic)]
128129
#![feature(hasher_prefixfree_extras)]
129130
#![feature(hint_assert_unchecked)]
130131
#![feature(inplace_iteration)]

library/alloc/src/slice/tests.rs

+4-86
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::cmp::Ordering::{self, Equal, Greater, Less};
99
use core::convert::identity;
1010
use core::fmt;
1111
use core::mem;
12-
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
12+
use core::sync::atomic::{Atomic, AtomicUsize, Ordering::Relaxed};
1313
use rand::{distributions::Standard, prelude::*, Rng, RngCore};
1414
use std::panic;
1515

@@ -62,91 +62,9 @@ macro_rules! do_test {
6262

6363
const MAX_LEN: usize = 80;
6464

65-
static DROP_COUNTS: [AtomicUsize; MAX_LEN] = [
66-
// FIXME(RFC 1109): AtomicUsize is not Copy.
67-
AtomicUsize::new(0),
68-
AtomicUsize::new(0),
69-
AtomicUsize::new(0),
70-
AtomicUsize::new(0),
71-
AtomicUsize::new(0),
72-
AtomicUsize::new(0),
73-
AtomicUsize::new(0),
74-
AtomicUsize::new(0),
75-
AtomicUsize::new(0),
76-
AtomicUsize::new(0),
77-
AtomicUsize::new(0),
78-
AtomicUsize::new(0),
79-
AtomicUsize::new(0),
80-
AtomicUsize::new(0),
81-
AtomicUsize::new(0),
82-
AtomicUsize::new(0),
83-
AtomicUsize::new(0),
84-
AtomicUsize::new(0),
85-
AtomicUsize::new(0),
86-
AtomicUsize::new(0),
87-
AtomicUsize::new(0),
88-
AtomicUsize::new(0),
89-
AtomicUsize::new(0),
90-
AtomicUsize::new(0),
91-
AtomicUsize::new(0),
92-
AtomicUsize::new(0),
93-
AtomicUsize::new(0),
94-
AtomicUsize::new(0),
95-
AtomicUsize::new(0),
96-
AtomicUsize::new(0),
97-
AtomicUsize::new(0),
98-
AtomicUsize::new(0),
99-
AtomicUsize::new(0),
100-
AtomicUsize::new(0),
101-
AtomicUsize::new(0),
102-
AtomicUsize::new(0),
103-
AtomicUsize::new(0),
104-
AtomicUsize::new(0),
105-
AtomicUsize::new(0),
106-
AtomicUsize::new(0),
107-
AtomicUsize::new(0),
108-
AtomicUsize::new(0),
109-
AtomicUsize::new(0),
110-
AtomicUsize::new(0),
111-
AtomicUsize::new(0),
112-
AtomicUsize::new(0),
113-
AtomicUsize::new(0),
114-
AtomicUsize::new(0),
115-
AtomicUsize::new(0),
116-
AtomicUsize::new(0),
117-
AtomicUsize::new(0),
118-
AtomicUsize::new(0),
119-
AtomicUsize::new(0),
120-
AtomicUsize::new(0),
121-
AtomicUsize::new(0),
122-
AtomicUsize::new(0),
123-
AtomicUsize::new(0),
124-
AtomicUsize::new(0),
125-
AtomicUsize::new(0),
126-
AtomicUsize::new(0),
127-
AtomicUsize::new(0),
128-
AtomicUsize::new(0),
129-
AtomicUsize::new(0),
130-
AtomicUsize::new(0),
131-
AtomicUsize::new(0),
132-
AtomicUsize::new(0),
133-
AtomicUsize::new(0),
134-
AtomicUsize::new(0),
135-
AtomicUsize::new(0),
136-
AtomicUsize::new(0),
137-
AtomicUsize::new(0),
138-
AtomicUsize::new(0),
139-
AtomicUsize::new(0),
140-
AtomicUsize::new(0),
141-
AtomicUsize::new(0),
142-
AtomicUsize::new(0),
143-
AtomicUsize::new(0),
144-
AtomicUsize::new(0),
145-
AtomicUsize::new(0),
146-
AtomicUsize::new(0),
147-
];
148-
149-
static VERSIONS: AtomicUsize = AtomicUsize::new(0);
65+
static DROP_COUNTS: [Atomic<usize>; MAX_LEN] = [const { AtomicUsize::new(0) }; MAX_LEN];
66+
67+
static VERSIONS: Atomic<usize> = AtomicUsize::new(0);
15068

15169
#[derive(Clone, Eq)]
15270
struct DropCounter {

library/alloc/src/sync.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use core::pin::Pin;
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

3131
#[cfg(not(no_global_oom_handling))]
3232
use crate::alloc::handle_alloc_error;
@@ -352,12 +352,12 @@ impl<T: ?Sized> fmt::Debug for Weak<T> {
352352
// inner types.
353353
#[repr(C)]
354354
struct ArcInner<T: ?Sized> {
355-
strong: atomic::AtomicUsize,
355+
strong: Atomic<usize>,
356356

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

362362
data: T,
363363
}
@@ -2646,8 +2646,8 @@ impl<T, A: Allocator> Weak<T, A> {
26462646
/// Helper type to allow accessing the reference counts without
26472647
/// making any assertions about the data field.
26482648
struct WeakInner<'a> {
2649-
weak: &'a atomic::AtomicUsize,
2650-
strong: &'a atomic::AtomicUsize,
2649+
weak: &'a Atomic<usize>,
2650+
strong: &'a Atomic<usize>,
26512651
}
26522652

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

library/core/src/sync/atomic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ pub enum Ordering {
485485
note = "the `new` function is now preferred",
486486
suggestion = "AtomicBool::new(false)"
487487
)]
488-
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
488+
pub const ATOMIC_BOOL_INIT: Atomic<bool> = AtomicBool::new(false);
489489

490490
#[cfg(target_has_atomic_load_store = "8")]
491491
impl AtomicBool {
@@ -3335,7 +3335,7 @@ macro_rules! atomic_int_ptr_sized {
33353335
note = "the `new` function is now preferred",
33363336
suggestion = "AtomicIsize::new(0)",
33373337
)]
3338-
pub const ATOMIC_ISIZE_INIT: AtomicIsize = AtomicIsize::new(0);
3338+
pub const ATOMIC_ISIZE_INIT: Atomic<isize> = AtomicIsize::new(0);
33393339

33403340
/// An [`AtomicUsize`] initialized to `0`.
33413341
#[cfg(target_pointer_width = $target_pointer_width)]
@@ -3345,7 +3345,7 @@ macro_rules! atomic_int_ptr_sized {
33453345
note = "the `new` function is now preferred",
33463346
suggestion = "AtomicUsize::new(0)",
33473347
)]
3348-
pub const ATOMIC_USIZE_INIT: AtomicUsize = AtomicUsize::new(0);
3348+
pub const ATOMIC_USIZE_INIT: Atomic<usize> = AtomicUsize::new(0);
33493349
)* };
33503350
}
33513351

library/std/src/alloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

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

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

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

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

library/std/src/backtrace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use crate::env;
9393
use crate::ffi::c_void;
9494
use crate::fmt;
9595
use crate::panic::UnwindSafe;
96-
use crate::sync::atomic::{AtomicU8, Ordering::Relaxed};
96+
use crate::sync::atomic::{Atomic, AtomicU8, Ordering::Relaxed};
9797
use crate::sync::LazyLock;
9898
use crate::sys::backtrace::{lock, output_filename, set_image_base};
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

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

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

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
#![feature(float_minimum_maximum)]
334334
#![feature(float_next_up_down)]
335335
#![feature(fmt_internals)]
336+
#![feature(generic_atomic)]
336337
#![feature(hasher_prefixfree_extras)]
337338
#![feature(hashmap_internals)]
338339
#![feature(hint_assert_unchecked)]

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
33
#![unstable(feature = "uefi_std", issue = "100499")]
44

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

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

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::os::xous::ffi::Connection;
2-
use core::sync::atomic::{AtomicU32, Ordering};
2+
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
33

44
mod dns;
55
pub(crate) use dns::*;
@@ -105,7 +105,7 @@ pub fn try_connect(name: &str) -> Option<Connection> {
105105
ns::try_connect_with_name(name)
106106
}
107107

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

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::os::xous::ffi::Connection;
22
use crate::os::xous::services::connect;
3-
use core::sync::atomic::{AtomicU32, Ordering};
3+
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
44

55
#[repr(usize)]
66
pub(crate) enum DnsLendMut {
@@ -16,7 +16,7 @@ impl Into<usize> for DnsLendMut {
1616
/// Return a `Connection` to the DNS lookup server. This server is used for
1717
/// querying domain name values.
1818
pub(crate) fn dns_server() -> Connection {
19-
static DNS_CONNECTION: AtomicU32 = AtomicU32::new(0);
19+
static DNS_CONNECTION: Atomic<u32> = AtomicU32::new(0);
2020
let cid = DNS_CONNECTION.load(Ordering::Relaxed);
2121
if cid != 0 {
2222
return cid.into();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::os::xous::ffi::Connection;
2-
use core::sync::atomic::{AtomicU32, Ordering};
2+
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
33

44
/// Group `usize` bytes into a `usize` and return it, beginning
55
/// from `offset` * sizeof(usize) bytes from the start. For example,
@@ -61,7 +61,7 @@ impl Into<usize> for LogLend {
6161
/// will block until the server is running. It is safe to call this multiple times,
6262
/// because the address is shared among all threads in a process.
6363
pub(crate) fn log_server() -> Connection {
64-
static LOG_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
64+
static LOG_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
6565

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::os::xous::ffi::Connection;
22
use crate::os::xous::services::connect;
3-
use core::sync::atomic::{AtomicU32, Ordering};
3+
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
44

55
pub(crate) enum NetBlockingScalar {
66
StdGetTtlUdp(u16 /* fd */), /* 36 */
@@ -83,7 +83,7 @@ impl<'a> Into<[usize; 5]> for NetBlockingScalar {
8383
/// Return a `Connection` to the Network server. This server provides all
8484
/// OS-level networking functions.
8585
pub(crate) fn net_server() -> Connection {
86-
static NET_CONNECTION: AtomicU32 = AtomicU32::new(0);
86+
static NET_CONNECTION: Atomic<u32> = AtomicU32::new(0);
8787
let cid = NET_CONNECTION.load(Ordering::Relaxed);
8888
if cid != 0 {
8989
return cid.into();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::os::xous::ffi::{connect, Connection};
2-
use core::sync::atomic::{AtomicU32, Ordering};
2+
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
33

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::os::xous::ffi::Connection;
2-
use core::sync::atomic::{AtomicU32, Ordering};
2+
use core::sync::atomic::{Atomic, AtomicU32, Ordering};
33

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

0 commit comments

Comments
 (0)