Switch OwnedStore handle count to AtomicU32

This commit is contained in:
GnomedDev 2024-01-31 13:36:37 +00:00
parent cdaa12e3df
commit 3cc601ac7e
No known key found for this signature in database
GPG Key ID: 9BF10F8372B254D1
3 changed files with 10 additions and 10 deletions

View File

@ -3,6 +3,7 @@
use super::*; use super::*;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::atomic::AtomicU32;
macro_rules! define_handles { macro_rules! define_handles {
( (
@ -12,8 +13,8 @@ macro_rules! define_handles {
#[repr(C)] #[repr(C)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub struct HandleCounters { pub struct HandleCounters {
$($oty: AtomicUsize,)* $($oty: AtomicU32,)*
$($ity: AtomicUsize,)* $($ity: AtomicU32,)*
} }
impl HandleCounters { impl HandleCounters {
@ -21,8 +22,8 @@ impl HandleCounters {
// a wrapper `fn` pointer, once `const fn` can reference `static`s. // a wrapper `fn` pointer, once `const fn` can reference `static`s.
extern "C" fn get() -> &'static Self { extern "C" fn get() -> &'static Self {
static COUNTERS: HandleCounters = HandleCounters { static COUNTERS: HandleCounters = HandleCounters {
$($oty: AtomicUsize::new(1),)* $($oty: AtomicU32::new(1),)*
$($ity: AtomicUsize::new(1),)* $($ity: AtomicU32::new(1),)*
}; };
&COUNTERS &COUNTERS
} }

View File

@ -4,7 +4,7 @@
use std::hash::Hash; use std::hash::Hash;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicU32, Ordering};
use super::fxhash::FxHashMap; use super::fxhash::FxHashMap;
@ -13,12 +13,12 @@
/// A store that associates values of type `T` with numeric handles. A value can /// A store that associates values of type `T` with numeric handles. A value can
/// be looked up using its handle. /// be looked up using its handle.
pub(super) struct OwnedStore<T: 'static> { pub(super) struct OwnedStore<T: 'static> {
counter: &'static AtomicUsize, counter: &'static AtomicU32,
data: BTreeMap<Handle, T>, data: BTreeMap<Handle, T>,
} }
impl<T> OwnedStore<T> { impl<T> OwnedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self { pub(super) fn new(counter: &'static AtomicU32) -> Self {
// Ensure the handle counter isn't 0, which would panic later, // Ensure the handle counter isn't 0, which would panic later,
// when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`. // when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`.
assert_ne!(counter.load(Ordering::SeqCst), 0); assert_ne!(counter.load(Ordering::SeqCst), 0);
@ -30,7 +30,7 @@ pub(super) fn new(counter: &'static AtomicUsize) -> Self {
impl<T> OwnedStore<T> { impl<T> OwnedStore<T> {
pub(super) fn alloc(&mut self, x: T) -> Handle { pub(super) fn alloc(&mut self, x: T) -> Handle {
let counter = self.counter.fetch_add(1, Ordering::SeqCst); let counter = self.counter.fetch_add(1, Ordering::SeqCst);
let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed"); let handle = Handle::new(counter).expect("`proc_macro` handle counter overflowed");
assert!(self.data.insert(handle, x).is_none()); assert!(self.data.insert(handle, x).is_none());
handle handle
} }
@ -60,7 +60,7 @@ pub(super) struct InternedStore<T: 'static> {
} }
impl<T: Copy + Eq + Hash> InternedStore<T> { impl<T: Copy + Eq + Hash> InternedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self { pub(super) fn new(counter: &'static AtomicU32) -> Self {
InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() } InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() }
} }

View File

@ -16,7 +16,6 @@
use std::ops::Bound; use std::ops::Bound;
use std::ops::Range; use std::ops::Range;
use std::panic; use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::Once; use std::sync::Once;
use std::thread; use std::thread;