Skip to content

Commit

Permalink
bpf: Support atomic CAS
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Mar 26, 2023
1 parent 68f93e1 commit d4627d4
Show file tree
Hide file tree
Showing 6 changed files with 884 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/imp/core_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ struct NoRefUnwindSafe(UnsafeCell<()>);
// SAFETY: this is a marker type and we'll never access the value.
unsafe impl Sync for NoRefUnwindSafe {}

#[cfg(not(target_arch = "bpf"))]
#[repr(transparent)]
pub(crate) struct AtomicBool {
inner: core::sync::atomic::AtomicBool,
// Prevent RefUnwindSafe from being propagated from the std atomic type.
_marker: PhantomData<NoRefUnwindSafe>,
}
#[cfg(not(target_arch = "bpf"))]
impl AtomicBool {
#[inline]
pub(crate) const fn new(v: bool) -> Self {
Expand Down Expand Up @@ -70,9 +72,11 @@ impl AtomicBool {
}
}
}
#[cfg(not(target_arch = "bpf"))]
#[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(not(portable_atomic_no_atomic_cas)))]
#[cfg_attr(not(portable_atomic_no_cfg_target_has_atomic), cfg(target_has_atomic = "ptr"))]
no_fetch_ops_impl!(AtomicBool, bool);
#[cfg(not(target_arch = "bpf"))]
#[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(not(portable_atomic_no_atomic_cas)))]
#[cfg_attr(not(portable_atomic_no_cfg_target_has_atomic), cfg(target_has_atomic = "ptr"))]
impl AtomicBool {
Expand Down Expand Up @@ -105,6 +109,7 @@ impl AtomicBool {
self.inner.compare_exchange_weak(current, new, success, failure)
}
}
#[cfg(not(target_arch = "bpf"))]
impl core::ops::Deref for AtomicBool {
type Target = core::sync::atomic::AtomicBool;
#[inline]
Expand Down Expand Up @@ -453,12 +458,18 @@ macro_rules! atomic_int {

atomic_int!(AtomicIsize, isize);
atomic_int!(AtomicUsize, usize);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(AtomicI8, i8);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(AtomicU8, u8);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(AtomicI16, i16);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(AtomicU16, u16);
#[cfg(not(target_arch = "bpf"))]
#[cfg(not(target_pointer_width = "16"))] // cfg(target_has_atomic_load_store = "32")
atomic_int!(AtomicI32, i32);
#[cfg(not(target_arch = "bpf"))]
#[cfg(not(target_pointer_width = "16"))] // cfg(target_has_atomic_load_store = "32")
atomic_int!(AtomicU32, u32);
#[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(not(portable_atomic_no_atomic_64)))]
Expand Down
Loading

0 comments on commit d4627d4

Please sign in to comment.