use crate::bindings;
use crate::macros::paste;
use core::cell::UnsafeCell;
use ffi::c_void;
mod private {
pub trait Sealed {}
}
impl private::Sealed for i8 {}
impl private::Sealed for i16 {}
impl private::Sealed for *const c_void {}
impl private::Sealed for i32 {}
impl private::Sealed for i64 {}
pub trait AtomicImpl: Sized + Copy + private::Sealed {
type Delta;
}
crate::static_assert!(
cfg!(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW),
"The current implementation of atomic i8/i16/ptr relies on the architecure being \
ARCH_SUPPORTS_ATOMIC_RMW"
);
impl AtomicImpl for i8 {
type Delta = Self;
}
impl AtomicImpl for i16 {
type Delta = Self;
}
impl AtomicImpl for *const c_void {
type Delta = isize;
}
impl AtomicImpl for i32 {
type Delta = Self;
}
impl AtomicImpl for i64 {
type Delta = Self;
}
#[repr(transparent)]
pub struct AtomicRepr<T: AtomicImpl>(UnsafeCell<T>);
impl<T: AtomicImpl> AtomicRepr<T> {
pub const fn new(v: T) -> Self {
Self(UnsafeCell::new(v))
}
pub const fn as_ptr(&self) -> *mut T {
self.0.get()
}
}
macro_rules! declare_atomic_method {
(
$(#[doc=$doc:expr])*
$func:ident($($arg:ident : $arg_type:ty),*) $(-> $ret:ty)?
) => {
paste!(
$(#[doc = $doc])*
fn [< atomic_ $func >]($($arg: $arg_type,)*) $(-> $ret)?;
);
};
(
$(#[doc=$doc:expr])*
$func:ident [$variant:ident $($rest:ident)*]($($arg_sig:tt)*) $(-> $ret:ty)?
) => {
paste!(
declare_atomic_method!(
$(#[doc = $doc])*
[< $func _ $variant >]($($arg_sig)*) $(-> $ret)?
);
);
declare_atomic_method!(
$(#[doc = $doc])*
$func [$($rest)*]($($arg_sig)*) $(-> $ret)?
);
};
(
$(#[doc=$doc:expr])*
$func:ident []($($arg_sig:tt)*) $(-> $ret:ty)?
) => {
declare_atomic_method!(
$(#[doc = $doc])*
$func($($arg_sig)*) $(-> $ret)?
);
}
}
macro_rules! impl_atomic_method {
(
($ctype:ident) $func:ident($($arg:ident: $arg_type:ty),*) $(-> $ret:ty)? {
$unsafe:tt { call($($c_arg:expr),*) }
}
) => {
paste!(
#[inline(always)]
fn [< atomic_ $func >]($($arg: $arg_type,)*) $(-> $ret)? {
$unsafe { bindings::[< $ctype _ $func >]($($c_arg,)*) }
}
);
};
(
($ctype:ident) $func:ident[$variant:ident $($rest:ident)*]($($arg_sig:tt)*) $(-> $ret:ty)? {
$unsafe:tt { call($($arg:tt)*) }
}
) => {
paste!(
impl_atomic_method!(
($ctype) [< $func _ $variant >]($($arg_sig)*) $( -> $ret)? {
$unsafe { call($($arg)*) }
}
);
);
impl_atomic_method!(
($ctype) $func [$($rest)*]($($arg_sig)*) $( -> $ret)? {
$unsafe { call($($arg)*) }
}
);
};
(
($ctype:ident) $func:ident[]($($arg_sig:tt)*) $( -> $ret:ty)? {
$unsafe:tt { call($($arg:tt)*) }
}
) => {
impl_atomic_method!(
($ctype) $func($($arg_sig)*) $(-> $ret)? {
$unsafe { call($($arg)*) }
}
);
}
}
macro_rules! declare_atomic_ops_trait {
(
$(#[$attr:meta])* $pub:vis trait $ops:ident {
$(
$(#[doc=$doc:expr])*
fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? {
$unsafe:tt { bindings::#call($($arg:tt)*) }
}
)*
}
) => {
$(#[$attr])*
$pub trait $ops: AtomicImpl {
$(
declare_atomic_method!(
$(#[doc=$doc])*
$func[$($variant)*]($($arg_sig)*) $(-> $ret)?
);
)*
}
}
}
macro_rules! impl_atomic_ops_for_one {
(
$ty:ty => $ctype:ident,
$(#[$attr:meta])* $pub:vis trait $ops:ident {
$(
$(#[doc=$doc:expr])*
fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? {
$unsafe:tt { bindings::#call($($arg:tt)*) }
}
)*
}
) => {
impl $ops for $ty {
$(
impl_atomic_method!(
($ctype) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? {
$unsafe { call($($arg)*) }
}
);
)*
}
}
}
macro_rules! declare_and_impl_atomic_methods {
(
[ $($map:tt)* ]
$(#[$attr:meta])* $pub:vis trait $ops:ident { $($body:tt)* }
) => {
declare_and_impl_atomic_methods!(
@with_ops_def
[ $($map)* ]
( $(#[$attr])* $pub trait $ops { $($body)* } )
);
};
(@with_ops_def [ $($map:tt)* ] ( $($ops_def:tt)* )) => {
declare_atomic_ops_trait!( $($ops_def)* );
declare_and_impl_atomic_methods!(
@munch
[ $($map)* ]
( $($ops_def)* )
);
};
(@munch [] ( $($ops_def:tt)* )) => {};
(@munch [ $ty:ty => $ctype:ident $(, $($rest:tt)*)? ] ( $($ops_def:tt)* )) => {
impl_atomic_ops_for_one!(
$ty => $ctype,
$($ops_def)*
);
declare_and_impl_atomic_methods!(
@munch
[ $($($rest)*)? ]
( $($ops_def)* )
);
};
}
declare_and_impl_atomic_methods!(
[ i8 => atomic_i8, i16 => atomic_i16, *const c_void => atomic_ptr, i32 => atomic, i64 => atomic64 ]
pub trait AtomicBasicOps {
fn read[acquire](a: &AtomicRepr<Self>) -> Self {
unsafe { bindings::#call(a.as_ptr().cast()) }
}
fn set[release](a: &AtomicRepr<Self>, v: Self) {
unsafe { bindings::#call(a.as_ptr().cast(), v) }
}
}
);
declare_and_impl_atomic_methods!(
[ i8 => atomic_i8, i16 => atomic_i16, *const c_void => atomic_ptr, i32 => atomic, i64 => atomic64 ]
pub trait AtomicExchangeOps {
fn xchg[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self) -> Self {
unsafe { bindings::#call(a.as_ptr().cast(), v) }
}
fn try_cmpxchg[acquire, release, relaxed](
a: &AtomicRepr<Self>, old: &mut Self, new: Self
) -> bool {
unsafe { bindings::#call(a.as_ptr().cast(), core::ptr::from_mut(old), new) }
}
}
);
declare_and_impl_atomic_methods!(
[ i32 => atomic, i64 => atomic64 ]
pub trait AtomicArithmeticOps {
fn add[](a: &AtomicRepr<Self>, v: Self::Delta) {
unsafe { bindings::#call(v, a.as_ptr().cast()) }
}
fn fetch_add[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> Self {
unsafe { bindings::#call(v, a.as_ptr().cast()) }
}
fn fetch_sub[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> Self {
unsafe { bindings::#call(v, a.as_ptr().cast()) }
}
}
);