Skip to content

Commit

Permalink
use generic Atomic type where possible
Browse files Browse the repository at this point in the history
in core/alloc/std only for now, and ignoring test files
  • Loading branch information
CAD97 committed Sep 19, 2024
1 parent 87be1c0 commit bacfd33
Show file tree
Hide file tree
Showing 85 changed files with 279 additions and 370 deletions.
4 changes: 2 additions & 2 deletions library/alloc/src/collections/binary_heap/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ fn panic_safe() {
use rand::seq::SliceRandom;
use std::cmp;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{Atomic, AtomicUsize, Ordering};

static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);
static DROP_COUNTER: Atomic<usize> = AtomicUsize::new(0);

#[derive(Eq, PartialEq, Ord, Clone, Debug)]
struct PanicOrd<T>(T, bool);
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::assert_matches::assert_matches;
use std::iter;
use std::ops::Bound::{Excluded, Included, Unbounded};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use std::sync::atomic::{Atomic, AtomicUsize, Ordering::SeqCst};

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

static COMPARES: AtomicUsize = AtomicUsize::new(0);
static COMPARES: Atomic<usize> = AtomicUsize::new(0);
impl Ord for EvilTwin {
fn cmp(&self, other: &Self) -> Ordering {
let ord = self.0.cmp(&other.0);
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
#![feature(extend_one)]
#![feature(fmt_internals)]
#![feature(fn_traits)]
#![feature(generic_atomic)]
#![feature(hasher_prefixfree_extras)]
#![feature(hint_assert_unchecked)]
#![feature(inplace_iteration)]
Expand Down
90 changes: 4 additions & 86 deletions library/alloc/src/slice/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::cmp::Ordering::{self, Equal, Greater, Less};
use core::convert::identity;
use core::fmt;
use core::mem;
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
use core::sync::atomic::{Atomic, AtomicUsize, Ordering::Relaxed};
use rand::{distributions::Standard, prelude::*, Rng, RngCore};
use std::panic;

Expand Down Expand Up @@ -62,91 +62,9 @@ macro_rules! do_test {

const MAX_LEN: usize = 80;

static DROP_COUNTS: [AtomicUsize; MAX_LEN] = [
// FIXME(RFC 1109): AtomicUsize is not Copy.
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
AtomicUsize::new(0),
];

static VERSIONS: AtomicUsize = AtomicUsize::new(0);
static DROP_COUNTS: [Atomic<usize>; MAX_LEN] = [const { AtomicUsize::new(0) }; MAX_LEN];

static VERSIONS: Atomic<usize> = AtomicUsize::new(0);

#[derive(Clone, Eq)]
struct DropCounter {
Expand Down
10 changes: 5 additions & 5 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use core::pin::Pin;
use core::ptr::{self, NonNull};
#[cfg(not(no_global_oom_handling))]
use core::slice::from_raw_parts_mut;
use core::sync::atomic;
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use core::sync::atomic::{self, Atomic};

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

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

data: T,
}
Expand Down Expand Up @@ -2646,8 +2646,8 @@ impl<T, A: Allocator> Weak<T, A> {
/// Helper type to allow accessing the reference counts without
/// making any assertions about the data field.
struct WeakInner<'a> {
weak: &'a atomic::AtomicUsize,
strong: &'a atomic::AtomicUsize,
weak: &'a Atomic<usize>,
strong: &'a Atomic<usize>,
}

impl<T: ?Sized> Weak<T> {
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ pub enum Ordering {
note = "the `new` function is now preferred",
suggestion = "AtomicBool::new(false)"
)]
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
pub const ATOMIC_BOOL_INIT: Atomic<bool> = AtomicBool::new(false);

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

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

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

use core::hint;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicPtr, Ordering};
use core::sync::atomic::{Atomic, AtomicPtr, Ordering};
use core::{mem, ptr};

#[stable(feature = "alloc_module", since = "1.28.0")]
Expand Down Expand Up @@ -288,7 +288,7 @@ unsafe impl Allocator for System {
}
}

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

/// Registers a custom allocation error hook, replacing any that was previously registered.
///
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ use crate::env;
use crate::ffi::c_void;
use crate::fmt;
use crate::panic::UnwindSafe;
use crate::sync::atomic::{AtomicU8, Ordering::Relaxed};
use crate::sync::atomic::{Atomic, AtomicU8, Ordering::Relaxed};
use crate::sync::LazyLock;
use crate::sys::backtrace::{lock, output_filename, set_image_base};

Expand Down Expand Up @@ -254,7 +254,7 @@ impl Backtrace {
// Cache the result of reading the environment variables to make
// backtrace captures speedy, because otherwise reading environment
// variables every time can be somewhat slow.
static ENABLED: AtomicU8 = AtomicU8::new(0);
static ENABLED: Atomic<u8> = AtomicU8::new(0);
match ENABLED.load(Relaxed) {
0 => {}
1 => return false,
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::io::{
self, BorrowedCursor, BufReader, IoSlice, IoSliceMut, LineWriter, Lines, SpecReadByte,
};
use crate::panic::{RefUnwindSafe, UnwindSafe};
use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sync::atomic::{Atomic, AtomicBool, Ordering};
use crate::sync::{Arc, Mutex, MutexGuard, OnceLock, ReentrantLock, ReentrantLockGuard};
use crate::sys::stdio;
use crate::thread::AccessError;
Expand All @@ -38,7 +38,7 @@ thread_local! {
/// have a consistent order between set_output_capture and print_to *within
/// the same thread*. Within the same thread, things always have a perfectly
/// consistent order. So Ordering::Relaxed is fine.
static OUTPUT_CAPTURE_USED: AtomicBool = AtomicBool::new(false);
static OUTPUT_CAPTURE_USED: Atomic<bool> = AtomicBool::new(false);

/// A handle to a raw instance of the standard input stream of this process.
///
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
#![feature(float_minimum_maximum)]
#![feature(float_next_up_down)]
#![feature(fmt_internals)]
#![feature(generic_atomic)]
#![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)]
#![feature(hint_assert_unchecked)]
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/os/uefi/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#![unstable(feature = "uefi_std", issue = "100499")]

use crate::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use crate::sync::atomic::{Atomic, AtomicBool, AtomicPtr, Ordering};
use crate::{ffi::c_void, ptr::NonNull};

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

/// Initializes the global System Table and Image Handle pointers.
///
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/xous/services.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::os::xous::ffi::Connection;
use core::sync::atomic::{AtomicU32, Ordering};
use core::sync::atomic::{Atomic, AtomicU32, Ordering};

mod dns;
pub(crate) use dns::*;
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn try_connect(name: &str) -> Option<Connection> {
ns::try_connect_with_name(name)
}

static NAME_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
static NAME_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);

/// Return a `Connection` to the name server. If the name server has not been started,
/// then this call will block until the name server has been started. The `Connection`
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/xous/services/dns.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::os::xous::ffi::Connection;
use crate::os::xous::services::connect;
use core::sync::atomic::{AtomicU32, Ordering};
use core::sync::atomic::{Atomic, AtomicU32, Ordering};

#[repr(usize)]
pub(crate) enum DnsLendMut {
Expand All @@ -16,7 +16,7 @@ impl Into<usize> for DnsLendMut {
/// Return a `Connection` to the DNS lookup server. This server is used for
/// querying domain name values.
pub(crate) fn dns_server() -> Connection {
static DNS_CONNECTION: AtomicU32 = AtomicU32::new(0);
static DNS_CONNECTION: Atomic<u32> = AtomicU32::new(0);
let cid = DNS_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
return cid.into();
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/xous/services/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::os::xous::ffi::Connection;
use core::sync::atomic::{AtomicU32, Ordering};
use core::sync::atomic::{Atomic, AtomicU32, Ordering};

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

let cid = LOG_SERVER_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/xous/services/net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::os::xous::ffi::Connection;
use crate::os::xous::services::connect;
use core::sync::atomic::{AtomicU32, Ordering};
use core::sync::atomic::{Atomic, AtomicU32, Ordering};

pub(crate) enum NetBlockingScalar {
StdGetTtlUdp(u16 /* fd */), /* 36 */
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<'a> Into<[usize; 5]> for NetBlockingScalar {
/// Return a `Connection` to the Network server. This server provides all
/// OS-level networking functions.
pub(crate) fn net_server() -> Connection {
static NET_CONNECTION: AtomicU32 = AtomicU32::new(0);
static NET_CONNECTION: Atomic<u32> = AtomicU32::new(0);
let cid = NET_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
return cid.into();
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/xous/services/systime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::os::xous::ffi::{connect, Connection};
use core::sync::atomic::{AtomicU32, Ordering};
use core::sync::atomic::{Atomic, AtomicU32, Ordering};

pub(crate) enum SystimeScalar {
GetUtcTimeMs,
Expand All @@ -16,7 +16,7 @@ impl Into<[usize; 5]> for SystimeScalar {
/// Return a `Connection` to the systime server. This server is used for reporting the
/// realtime clock.
pub(crate) fn systime_server() -> Connection {
static SYSTIME_SERVER_CONNECTION: AtomicU32 = AtomicU32::new(0);
static SYSTIME_SERVER_CONNECTION: Atomic<u32> = AtomicU32::new(0);
let cid = SYSTIME_SERVER_CONNECTION.load(Ordering::Relaxed);
if cid != 0 {
return cid.into();
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/xous/services/ticktimer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::os::xous::ffi::Connection;
use core::sync::atomic::{AtomicU32, Ordering};
use core::sync::atomic::{Atomic, AtomicU32, Ordering};

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

0 comments on commit bacfd33

Please sign in to comment.