|
| 1 | +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause |
| 3 | + |
| 4 | +use std::sync::atomic::Ordering; |
| 5 | + |
| 6 | +/// Objects that implement this trait must consist exclusively of atomic types |
| 7 | +/// from [`std::sync::atomic`](https://doc.rust-lang.org/std/sync/atomic/), except for |
| 8 | +/// [`AtomicPtr<T>`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicPtr.html) and |
| 9 | +/// [`AtomicBool`](https://doc.rust-lang.org/std/sync/atomic/struct.AtomicBool.html). |
| 10 | +pub unsafe trait AtomicInteger: Sync + Send { |
| 11 | + /// The raw value type associated with the atomic integer (i.e. `u16` for `AtomicU16`). |
| 12 | + type V; |
| 13 | + |
| 14 | + /// Create a new instance of `Self`. |
| 15 | + fn new(v: Self::V) -> Self; |
| 16 | + |
| 17 | + /// Loads a value from the atomic integer. |
| 18 | + fn load(&self, order: Ordering) -> Self::V; |
| 19 | + |
| 20 | + /// Stores a value into the atomic integer. |
| 21 | + fn store(&self, val: Self::V, order: Ordering); |
| 22 | +} |
| 23 | + |
| 24 | +macro_rules! impl_atomic_integer_ops { |
| 25 | + ($T:path, $V:ty) => { |
| 26 | + unsafe impl AtomicInteger for $T { |
| 27 | + type V = $V; |
| 28 | + |
| 29 | + fn new(v: Self::V) -> Self { |
| 30 | + Self::new(v) |
| 31 | + } |
| 32 | + |
| 33 | + fn load(&self, order: Ordering) -> Self::V { |
| 34 | + self.load(order) |
| 35 | + } |
| 36 | + |
| 37 | + fn store(&self, val: Self::V, order: Ordering) { |
| 38 | + self.store(val, order) |
| 39 | + } |
| 40 | + } |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +// TODO: Detect availability using #[cfg(target_has_atomic) when it is stabilized. |
| 45 | +// Right now we essentially assume we're running on either x86 or Arm (32 or 64 bit). AFAIK, |
| 46 | +// Rust starts using additional synchronization primitives to implement atomics when they're |
| 47 | +// not natively available, and that doesn't interact safely with how we cast pointers to |
| 48 | +// atomic value references. We should be wary of this when looking at a broader range of |
| 49 | +// platforms. |
| 50 | + |
| 51 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicI8, i8); |
| 52 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicI16, i16); |
| 53 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicI32, i32); |
| 54 | +#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] |
| 55 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicI64, i64); |
| 56 | + |
| 57 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicU8, u8); |
| 58 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicU16, u16); |
| 59 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicU32, u32); |
| 60 | +#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] |
| 61 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicU64, u64); |
| 62 | + |
| 63 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicIsize, isize); |
| 64 | +impl_atomic_integer_ops!(std::sync::atomic::AtomicUsize, usize); |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + use std::fmt::Debug; |
| 71 | + use std::sync::atomic::AtomicU32; |
| 72 | + |
| 73 | + fn check_atomic_integer_ops<A: AtomicInteger>() |
| 74 | + where |
| 75 | + A::V: Copy + Debug + From<u8> + PartialEq, |
| 76 | + { |
| 77 | + let v = A::V::from(0); |
| 78 | + let a = A::new(v); |
| 79 | + assert_eq!(a.load(Ordering::Relaxed), v); |
| 80 | + |
| 81 | + let v2 = A::V::from(100); |
| 82 | + a.store(v2, Ordering::Relaxed); |
| 83 | + assert_eq!(a.load(Ordering::Relaxed), v2); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_atomic_integer_ops() { |
| 88 | + check_atomic_integer_ops::<AtomicU32>() |
| 89 | + } |
| 90 | +} |
0 commit comments