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 Feb 23, 2023
1 parent fe84be8 commit d2bd1e9
Show file tree
Hide file tree
Showing 5 changed files with 867 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/imp/core_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

use core::sync::atomic::Ordering;

#[cfg(not(target_arch = "bpf"))]
#[repr(transparent)]
pub(crate) struct AtomicBool {
inner: core::sync::atomic::AtomicBool,
}
#[cfg(not(target_arch = "bpf"))]
impl AtomicBool {
#[inline]
pub(crate) const fn new(v: bool) -> Self {
Expand Down Expand Up @@ -43,9 +45,11 @@ impl AtomicBool {
self.inner.store(val, order);
}
}
#[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 @@ -78,6 +82,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 @@ -411,12 +416,18 @@ macro_rules! atomic_int {

atomic_int!(int, AtomicIsize, isize);
atomic_int!(uint, AtomicUsize, usize);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(int, AtomicI8, i8);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(uint, AtomicU8, u8);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(int, AtomicI16, i16);
#[cfg(not(target_arch = "bpf"))]
atomic_int!(uint, AtomicU16, u16);
#[cfg(not(target_arch = "bpf"))]
#[cfg(not(target_pointer_width = "16"))] // cfg(target_has_atomic_load_store = "32")
atomic_int!(int, AtomicI32, i32);
#[cfg(not(target_arch = "bpf"))]
#[cfg(not(target_pointer_width = "16"))] // cfg(target_has_atomic_load_store = "32")
atomic_int!(uint, 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 d2bd1e9

Please sign in to comment.