use core::fmt;
use core::ops::{Deref, DerefMut};
use core::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicPtr, AtomicU16,
AtomicU32, AtomicU64, AtomicU8, AtomicUsize,
};
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)]
#[cfg_attr(
any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "arm64ec",
target_arch = "powerpc64",
),
repr(align(128))
)]
#[cfg_attr(
any(
target_arch = "arm",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "sparc",
target_arch = "hexagon",
),
repr(align(32))
)]
#[cfg_attr(target_arch = "m68k", repr(align(16)))]
#[cfg_attr(target_arch = "s390x", repr(align(256)))]
#[cfg_attr(
not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "arm64ec",
target_arch = "powerpc64",
target_arch = "arm",
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "sparc",
target_arch = "hexagon",
target_arch = "m68k",
target_arch = "s390x",
)),
repr(align(64))
)]
pub struct CachePadded<T> {
value: T,
}
unsafe impl<T: Send> Send for CachePadded<T> {}
unsafe impl<T: Sync> Sync for CachePadded<T> {}
impl<T> CachePadded<T> {
pub const fn new(t: T) -> Self {
Self { value: t }
}
pub fn into_inner(self) -> T {
self.value
}
}
impl<T> Deref for CachePadded<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
impl<T> DerefMut for CachePadded<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.value
}
}
impl<T: fmt::Debug> fmt::Debug for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CachePadded")
.field("value", &self.value)
.finish()
}
}
impl<T> From<T> for CachePadded<T> {
fn from(t: T) -> Self {
Self::new(t)
}
}
impl<T: fmt::Display> fmt::Display for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f)
}
}
macro_rules! cache_padded_atomic_number {
($name:ident, $atomic_type:ident, $number_type:ident) => {
#[allow(
rustdoc::redundant_explicit_links,
reason = "It is needed for right IDE doc formating"
)]
#[doc = concat!(
"Alias to [`CachePadded`](CachePadded)`<`[`", stringify!($atomic_type), "`]`>`."
)]
pub struct $name(CachePadded<$atomic_type>);
impl $name {
#[doc = concat!(
"Creates a new [`CachePadded`](CachePadded)`<`[`", stringify!($atomic_type), "`]`>`."
)]
#[inline(always)]
pub const fn new(t: $number_type) -> Self {
Self($crate::cache_padded::CachePadded::new($atomic_type::new(t)))
}
}
impl core::ops::Deref for $name {
type Target = $atomic_type;
fn deref(&self) -> &$atomic_type {
&self.0
}
}
impl core::ops::DerefMut for $name {
fn deref_mut(&mut self) -> &mut $atomic_type {
&mut self.0
}
}
impl Default for $name {
fn default() -> Self {
Self::new($number_type::default())
}
}
};
}
cache_padded_atomic_number!(CachePaddedAtomicU8, AtomicU8, u8);
cache_padded_atomic_number!(CachePaddedAtomicU16, AtomicU16, u16);
cache_padded_atomic_number!(CachePaddedAtomicU32, AtomicU32, u32);
cache_padded_atomic_number!(CachePaddedAtomicU64, AtomicU64, u64);
cache_padded_atomic_number!(CachePaddedAtomicUsize, AtomicUsize, usize);
cache_padded_atomic_number!(CachePaddedAtomicI8, AtomicI8, i8);
cache_padded_atomic_number!(CachePaddedAtomicI16, AtomicI16, i16);
cache_padded_atomic_number!(CachePaddedAtomicI32, AtomicI32, i32);
cache_padded_atomic_number!(CachePaddedAtomicI64, AtomicI64, i64);
cache_padded_atomic_number!(CachePaddedAtomicIsize, AtomicIsize, isize);
cache_padded_atomic_number!(CachePaddedAtomicBool, AtomicBool, bool);
#[allow(
rustdoc::redundant_explicit_links,
reason = "It is needed for right IDE doc formating"
)]
#[allow(
clippy::doc_markdown,
reason = "It is needed for right IDE doc formating"
)]
pub struct CachePaddedAtomicPtr<T>(CachePadded<AtomicPtr<T>>);
impl<T> CachePaddedAtomicPtr<T> {
#[allow(
clippy::doc_markdown,
reason = "It is needed for right IDE doc formating"
)]
#[inline(always)]
pub const fn new(ptr: *mut T) -> Self {
Self(CachePadded::new(AtomicPtr::new(ptr)))
}
}
impl<T> Deref for CachePaddedAtomicPtr<T> {
type Target = AtomicPtr<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for CachePaddedAtomicPtr<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}