From daec0d3abd75308d8146a26a2cf1a6d0757e7d7a Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Sat, 25 Feb 2023 18:24:46 -0800 Subject: [PATCH 1/2] inner_ptr -> as_ptr --- src/lib.rs | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1f89ca6..2cfb4aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,8 +91,8 @@ impl fmt::Debug for Atomic { impl Atomic { /// Creates a new `Atomic`. #[inline] - pub const fn new(v: T) -> Atomic { - Atomic { + pub const fn new(v: T) -> Self { + Self { v: UnsafeCell::new(MaybeUninit::new(v)), } } @@ -109,8 +109,11 @@ impl Atomic { } impl Atomic { + /// Returns a mutable pointer to the underlying type. + /// + #[inline] - fn inner_ptr(&self) -> *mut T { + pub fn as_ptr(&self) -> *mut T { self.v.get() as *mut T } @@ -120,7 +123,7 @@ impl Atomic { /// concurrently accessing the atomic data. #[inline] pub fn get_mut(&mut self) -> &mut T { - unsafe { &mut *self.inner_ptr() } + unsafe { &mut *self.as_ptr() } } /// Consumes the atomic and returns the contained value. @@ -142,7 +145,7 @@ impl Atomic { /// Panics if `order` is `Release` or `AcqRel`. #[inline] pub fn load(&self, order: Ordering) -> T { - unsafe { ops::atomic_load(self.inner_ptr(), order) } + unsafe { ops::atomic_load(self.as_ptr(), order) } } /// Stores a value into the `Atomic`. @@ -156,7 +159,7 @@ impl Atomic { #[inline] pub fn store(&self, val: T, order: Ordering) { unsafe { - ops::atomic_store(self.inner_ptr(), val, order); + ops::atomic_store(self.as_ptr(), val, order); } } @@ -166,7 +169,7 @@ impl Atomic { /// of this operation. #[inline] pub fn swap(&self, val: T, order: Ordering) -> T { - unsafe { ops::atomic_swap(self.inner_ptr(), val, order) } + unsafe { ops::atomic_swap(self.as_ptr(), val, order) } } /// Stores a value into the `Atomic` if the current value is the same as the @@ -189,7 +192,7 @@ impl Atomic { success: Ordering, failure: Ordering, ) -> Result { - unsafe { ops::atomic_compare_exchange(self.inner_ptr(), current, new, success, failure) } + unsafe { ops::atomic_compare_exchange(self.as_ptr(), current, new, success, failure) } } /// Stores a value into the `Atomic` if the current value is the same as the @@ -214,9 +217,7 @@ impl Atomic { success: Ordering, failure: Ordering, ) -> Result { - unsafe { - ops::atomic_compare_exchange_weak(self.inner_ptr(), current, new, success, failure) - } + unsafe { ops::atomic_compare_exchange_weak(self.as_ptr(), current, new, success, failure) } } /// Fetches the value, and applies a function to it that returns an optional @@ -285,7 +286,7 @@ impl Atomic { /// Returns the previous value. #[inline] pub fn fetch_and(&self, val: bool, order: Ordering) -> bool { - unsafe { ops::atomic_and(self.inner_ptr(), val, order) } + unsafe { ops::atomic_and(self.as_ptr(), val, order) } } /// Logical "or" with a boolean value. @@ -296,7 +297,7 @@ impl Atomic { /// Returns the previous value. #[inline] pub fn fetch_or(&self, val: bool, order: Ordering) -> bool { - unsafe { ops::atomic_or(self.inner_ptr(), val, order) } + unsafe { ops::atomic_or(self.as_ptr(), val, order) } } /// Logical "xor" with a boolean value. @@ -307,7 +308,7 @@ impl Atomic { /// Returns the previous value. #[inline] pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool { - unsafe { ops::atomic_xor(self.inner_ptr(), val, order) } + unsafe { ops::atomic_xor(self.as_ptr(), val, order) } } } @@ -317,31 +318,31 @@ macro_rules! atomic_ops_common { /// Add to the current value, returning the previous value. #[inline] pub fn fetch_add(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_add(self.inner_ptr(), val, order) } + unsafe { ops::atomic_add(self.as_ptr(), val, order) } } /// Subtract from the current value, returning the previous value. #[inline] pub fn fetch_sub(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_sub(self.inner_ptr(), val, order) } + unsafe { ops::atomic_sub(self.as_ptr(), val, order) } } /// Bitwise and with the current value, returning the previous value. #[inline] pub fn fetch_and(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_and(self.inner_ptr(), val, order) } + unsafe { ops::atomic_and(self.as_ptr(), val, order) } } /// Bitwise or with the current value, returning the previous value. #[inline] pub fn fetch_or(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_or(self.inner_ptr(), val, order) } + unsafe { ops::atomic_or(self.as_ptr(), val, order) } } /// Bitwise xor with the current value, returning the previous value. #[inline] pub fn fetch_xor(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_xor(self.inner_ptr(), val, order) } + unsafe { ops::atomic_xor(self.as_ptr(), val, order) } } } )*); @@ -354,13 +355,13 @@ macro_rules! atomic_ops_signed { /// Minimum with the current value. #[inline] pub fn fetch_min(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_min(self.inner_ptr(), val, order) } + unsafe { ops::atomic_min(self.as_ptr(), val, order) } } /// Maximum with the current value. #[inline] pub fn fetch_max(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_max(self.inner_ptr(), val, order) } + unsafe { ops::atomic_max(self.as_ptr(), val, order) } } } )* @@ -374,13 +375,13 @@ macro_rules! atomic_ops_unsigned { /// Minimum with the current value. #[inline] pub fn fetch_min(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_umin(self.inner_ptr(), val, order) } + unsafe { ops::atomic_umin(self.as_ptr(), val, order) } } /// Maximum with the current value. #[inline] pub fn fetch_max(&self, val: $t, order: Ordering) -> $t { - unsafe { ops::atomic_umax(self.inner_ptr(), val, order) } + unsafe { ops::atomic_umax(self.as_ptr(), val, order) } } } )* From d14961f0916a1a13336034bb058e1241256a5a87 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Sat, 25 Feb 2023 18:24:56 -0800 Subject: [PATCH 2/2] inline -> inline(always) --- src/lib.rs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2cfb4aa..7e47aa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ unsafe impl Sync for Atomic {} impl RefUnwindSafe for Atomic {} impl Default for Atomic { - #[inline] + #[inline(always)] fn default() -> Self { Self::new(Default::default()) } @@ -90,7 +90,7 @@ impl fmt::Debug for Atomic { impl Atomic { /// Creates a new `Atomic`. - #[inline] + #[inline(always)] pub const fn new(v: T) -> Self { Self { v: UnsafeCell::new(MaybeUninit::new(v)), @@ -102,7 +102,7 @@ impl Atomic { /// If an `Atomic` is not lock-free then it may be implemented using locks /// internally, which makes it unsuitable for some situations (such as /// communicating with a signal handler). - #[inline] + #[inline(always)] pub const fn is_lock_free() -> bool { ops::atomic_is_lock_free::() } @@ -112,7 +112,7 @@ impl Atomic { /// Returns a mutable pointer to the underlying type. /// - #[inline] + #[inline(always)] pub fn as_ptr(&self) -> *mut T { self.v.get() as *mut T } @@ -121,7 +121,7 @@ impl Atomic { /// /// This is safe because the mutable reference guarantees that no other threads are /// concurrently accessing the atomic data. - #[inline] + #[inline(always)] pub fn get_mut(&mut self) -> &mut T { unsafe { &mut *self.as_ptr() } } @@ -130,7 +130,7 @@ impl Atomic { /// /// This is safe because passing `self` by value guarantees that no other threads are /// concurrently accessing the atomic data. - #[inline] + #[inline(always)] pub fn into_inner(self) -> T { unsafe { self.v.into_inner().assume_init() } } @@ -143,7 +143,7 @@ impl Atomic { /// # Panics /// /// Panics if `order` is `Release` or `AcqRel`. - #[inline] + #[inline(always)] pub fn load(&self, order: Ordering) -> T { unsafe { ops::atomic_load(self.as_ptr(), order) } } @@ -156,7 +156,7 @@ impl Atomic { /// # Panics /// /// Panics if `order` is `Acquire` or `AcqRel`. - #[inline] + #[inline(always)] pub fn store(&self, val: T, order: Ordering) { unsafe { ops::atomic_store(self.as_ptr(), val, order); @@ -167,7 +167,7 @@ impl Atomic { /// /// `swap` takes an `Ordering` argument which describes the memory ordering /// of this operation. - #[inline] + #[inline(always)] pub fn swap(&self, val: T, order: Ordering) -> T { unsafe { ops::atomic_swap(self.as_ptr(), val, order) } } @@ -184,7 +184,7 @@ impl Atomic { /// the operation succeeds while the second describes the required ordering /// when the operation fails. The failure ordering can't be `Release` or /// `AcqRel` and must be equivalent or weaker than the success ordering. - #[inline] + #[inline(always)] pub fn compare_exchange( &self, current: T, @@ -209,7 +209,7 @@ impl Atomic { /// when the operation fails. The failure ordering can't be `Release` or /// `AcqRel` and must be equivalent or weaker than the success ordering. /// success ordering. - #[inline] + #[inline(always)] pub fn compare_exchange_weak( &self, current: T, @@ -256,7 +256,7 @@ impl Atomic { /// assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8)); /// assert_eq!(x.load(Ordering::SeqCst), 9); /// ``` - #[inline] + #[inline(always)] pub fn fetch_update( &self, set_order: Ordering, @@ -284,7 +284,7 @@ impl Atomic { /// `val`, and sets the new value to the result. /// /// Returns the previous value. - #[inline] + #[inline(always)] pub fn fetch_and(&self, val: bool, order: Ordering) -> bool { unsafe { ops::atomic_and(self.as_ptr(), val, order) } } @@ -295,7 +295,7 @@ impl Atomic { /// `val`, and sets the new value to the result. /// /// Returns the previous value. - #[inline] + #[inline(always)] pub fn fetch_or(&self, val: bool, order: Ordering) -> bool { unsafe { ops::atomic_or(self.as_ptr(), val, order) } } @@ -306,7 +306,7 @@ impl Atomic { /// `val`, and sets the new value to the result. /// /// Returns the previous value. - #[inline] + #[inline(always)] pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool { unsafe { ops::atomic_xor(self.as_ptr(), val, order) } } @@ -316,31 +316,31 @@ macro_rules! atomic_ops_common { ($($t:ty)*) => ($( impl Atomic<$t> { /// Add to the current value, returning the previous value. - #[inline] + #[inline(always)] pub fn fetch_add(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_add(self.as_ptr(), val, order) } } /// Subtract from the current value, returning the previous value. - #[inline] + #[inline(always)] pub fn fetch_sub(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_sub(self.as_ptr(), val, order) } } /// Bitwise and with the current value, returning the previous value. - #[inline] + #[inline(always)] pub fn fetch_and(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_and(self.as_ptr(), val, order) } } /// Bitwise or with the current value, returning the previous value. - #[inline] + #[inline(always)] pub fn fetch_or(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_or(self.as_ptr(), val, order) } } /// Bitwise xor with the current value, returning the previous value. - #[inline] + #[inline(always)] pub fn fetch_xor(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_xor(self.as_ptr(), val, order) } } @@ -353,13 +353,13 @@ macro_rules! atomic_ops_signed { $( impl Atomic<$t> { /// Minimum with the current value. - #[inline] + #[inline(always)] pub fn fetch_min(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_min(self.as_ptr(), val, order) } } /// Maximum with the current value. - #[inline] + #[inline(always)] pub fn fetch_max(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_max(self.as_ptr(), val, order) } } @@ -373,13 +373,13 @@ macro_rules! atomic_ops_unsigned { $( impl Atomic<$t> { /// Minimum with the current value. - #[inline] + #[inline(always)] pub fn fetch_min(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_umin(self.as_ptr(), val, order) } } /// Maximum with the current value. - #[inline] + #[inline(always)] pub fn fetch_max(&self, val: $t, order: Ordering) -> $t { unsafe { ops::atomic_umax(self.as_ptr(), val, order) } }