Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for atomic-traits #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ default = ["atomic_f64"]
atomic_f64 = []

[dependencies]
atomic-traits = { version = "0.3.0", optional = true }
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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]
Expand Down
152 changes: 152 additions & 0 deletions src/traits_f32.rs
Original file line number Diff line number Diff line change
@@ -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::Type, Self::Type> {
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::Type, Self::Type> {
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<F>(
&self,
fetch_order: Ordering,
set_order: Ordering,
f: F,
) -> Result<Self::Type, Self::Type>
where
F: FnMut(Self::Type) -> Option<Self::Type>,
{
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 {}
152 changes: 152 additions & 0 deletions src/traits_f64.rs
Original file line number Diff line number Diff line change
@@ -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::Type, Self::Type> {
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::Type, Self::Type> {
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<F>(
&self,
fetch_order: Ordering,
set_order: Ordering,
f: F,
) -> Result<Self::Type, Self::Type>
where
F: FnMut(Self::Type) -> Option<Self::Type>,
{
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 {}