From 08db8dd6f8fc29fb92941804485fff355f8ddcdc Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Tue, 14 Sep 2021 20:35:54 -0700 Subject: [PATCH] add support for atomic-traits --- Cargo.toml | 1 + src/lib.rs | 10 +++ src/traits_f32.rs | 152 ++++++++++++++++++++++++++++++++++++++++++++++ src/traits_f64.rs | 152 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 315 insertions(+) create mode 100644 src/traits_f32.rs create mode 100644 src/traits_f64.rs diff --git a/Cargo.toml b/Cargo.toml index 6f2ad64..242ec77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ default = ["atomic_f64"] atomic_f64 = [] [dependencies] +atomic-traits = { version = "0.3.0", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 68e93d5..d36fa43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,6 +81,9 @@ mod atomic_f32; pub use atomic_f32::AtomicF32; +#[cfg(feature = "atomic-traits")] +mod traits_f32; + #[cfg(all( feature = "atomic_f64", not(any(target_arch = "powerpc", target_arch = "mips", force_disable_atomic64)) @@ -93,6 +96,13 @@ mod atomic_f64; ))] pub use atomic_f64::AtomicF64; +#[cfg(all( + feature = "atomic_f64", + feature = "atomic-traits", + not(any(target_arch = "powerpc", target_arch = "mips", force_disable_atomic64)) +))] +mod traits_f64; + use core::sync::atomic::Ordering; #[inline] diff --git a/src/traits_f32.rs b/src/traits_f32.rs new file mode 100644 index 0000000..feab781 --- /dev/null +++ b/src/traits_f32.rs @@ -0,0 +1,152 @@ +use core::sync::atomic::Ordering; + +use atomic_traits::{ + //fetch::{And, Nand, Or, Xor}, + //Bitwise, + fetch::{Add, Max, Min, Sub, Update}, + Atomic, + NumOps, +}; + +use crate::AtomicF32; + +impl Atomic for AtomicF32 { + type Type = f32; + + fn new(v: Self::Type) -> Self { + Self::new(v) + } + + fn get_mut(&mut self) -> &mut Self::Type { + Self::get_mut(self) + } + + fn into_inner(self) -> Self::Type { + Self::into_inner(self) + } + + fn load(&self, order: Ordering) -> Self::Type { + Self::load(&self, order) + } + + fn store(&self, val: Self::Type, order: Ordering) { + Self::store(&self, val, order) + } + + fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::swap(&self, val, order) + } + + fn compare_and_swap( + &self, + current: Self::Type, + new: Self::Type, + order: Ordering, + ) -> Self::Type { + Self::compare_and_swap(&self, current, new, order) + } + + fn compare_exchange( + &self, + current: Self::Type, + new: Self::Type, + success: Ordering, + failure: Ordering, + ) -> Result { + Self::compare_exchange(&self, current, new, success, failure) + } + + fn compare_exchange_weak( + &self, + current: Self::Type, + new: Self::Type, + success: Ordering, + failure: Ordering, + ) -> Result { + Self::compare_exchange_weak(&self, current, new, success, failure) + } +} + +impl Add for AtomicF32 { + type Type = f32; + + fn fetch_add(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_add(&self, val, order) + } +} + +//impl And for AtomicF32 { +// type Type = f32; +// +// fn fetch_and(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_and(&self, val, order) +// } +//} + +impl Max for AtomicF32 { + type Type = f32; + + fn fetch_max(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_max(&self, val, order) + } +} + +impl Min for AtomicF32 { + type Type = f32; + + fn fetch_min(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_min(&self, val, order) + } +} + +//impl Nand for AtomicF32 { +// type Type = f32; +// +// fn fetch_nand(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_nand(&self, val, order) +// } +//} + +//impl Or for AtomicF32 { +// type Type = f32; +// +// fn fetch_or(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_or(&self, val, order) +// } +//} + +impl Sub for AtomicF32 { + type Type = f32; + + fn fetch_sub(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_sub(&self, val, order) + } +} + +impl Update for AtomicF32 { + type Type = f32; + + fn fetch_update( + &self, + fetch_order: Ordering, + set_order: Ordering, + f: F, + ) -> Result + where + F: FnMut(Self::Type) -> Option, + { + Self::fetch_update(&self, fetch_order, set_order, f) + } +} + +//impl Xor for AtomicF32 { +// type Type = f32; +// +// fn fetch_xor(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_xor(&self, val, order) +// } +//} + +//impl Bitwise for AtomicF32 {} + +impl NumOps for AtomicF32 {} diff --git a/src/traits_f64.rs b/src/traits_f64.rs new file mode 100644 index 0000000..939d10c --- /dev/null +++ b/src/traits_f64.rs @@ -0,0 +1,152 @@ +use core::sync::atomic::Ordering; + +use atomic_traits::{ + //fetch::{And, Nand, Or, Xor}, + //Bitwise, + fetch::{Add, Max, Min, Sub, Update}, + Atomic, + NumOps, +}; + +use crate::AtomicF64; + +impl Atomic for AtomicF64 { + type Type = f64; + + fn new(v: Self::Type) -> Self { + Self::new(v) + } + + fn get_mut(&mut self) -> &mut Self::Type { + Self::get_mut(self) + } + + fn into_inner(self) -> Self::Type { + Self::into_inner(self) + } + + fn load(&self, order: Ordering) -> Self::Type { + Self::load(&self, order) + } + + fn store(&self, val: Self::Type, order: Ordering) { + Self::store(&self, val, order) + } + + fn swap(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::swap(&self, val, order) + } + + fn compare_and_swap( + &self, + current: Self::Type, + new: Self::Type, + order: Ordering, + ) -> Self::Type { + Self::compare_and_swap(&self, current, new, order) + } + + fn compare_exchange( + &self, + current: Self::Type, + new: Self::Type, + success: Ordering, + failure: Ordering, + ) -> Result { + Self::compare_exchange(&self, current, new, success, failure) + } + + fn compare_exchange_weak( + &self, + current: Self::Type, + new: Self::Type, + success: Ordering, + failure: Ordering, + ) -> Result { + Self::compare_exchange_weak(&self, current, new, success, failure) + } +} + +impl Add for AtomicF64 { + type Type = f64; + + fn fetch_add(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_add(&self, val, order) + } +} + +//impl And for AtomicF64 { +// type Type = f64; +// +// fn fetch_and(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_and(&self, val, order) +// } +//} + +impl Max for AtomicF64 { + type Type = f64; + + fn fetch_max(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_max(&self, val, order) + } +} + +impl Min for AtomicF64 { + type Type = f64; + + fn fetch_min(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_min(&self, val, order) + } +} + +//impl Nand for AtomicF64 { +// type Type = f64; +// +// fn fetch_nand(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_nand(&self, val, order) +// } +//} + +//impl Or for AtomicF64 { +// type Type = f64; +// +// fn fetch_or(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_or(&self, val, order) +// } +//} + +impl Sub for AtomicF64 { + type Type = f64; + + fn fetch_sub(&self, val: Self::Type, order: Ordering) -> Self::Type { + Self::fetch_sub(&self, val, order) + } +} + +impl Update for AtomicF64 { + type Type = f64; + + fn fetch_update( + &self, + fetch_order: Ordering, + set_order: Ordering, + f: F, + ) -> Result + where + F: FnMut(Self::Type) -> Option, + { + Self::fetch_update(&self, fetch_order, set_order, f) + } +} + +//impl Xor for AtomicF64 { +// type Type = f64; +// +// fn fetch_xor(&self, val: Self::Type, order: Ordering) -> Self::Type { +// Self::fetch_xor(&self, val, order) +// } +//} + +//impl Bitwise for AtomicF64 {} + +impl NumOps for AtomicF64 {}