1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
pub use core::sync::atomic::Ordering;

/// Trait describing types that are atomically sized, i.e. it's possible to perform
/// atomic operations on them.
pub trait AtomicallySized: Sized {
    type AtomicVersion: Atomic<Self>;
}

/// Trait describing an atomic version of an atomically-sized type.
pub trait Atomic<T>: Send + Sync + Sized
where
    T: Sized,
{
    /// Generate a reference to the atomic version of this type using its normal
    /// version.
    unsafe fn from_raw(val: &T) -> &Self {
        &*(val as *const T as *const Self)
    }

    fn new(val: T) -> Self;

    fn store(&self, val: T, order: Ordering);

    fn load(&self, order: Ordering) -> T;

    fn swap(&self, val: T, order: Ordering) -> T;

    fn get_mut(&mut self) -> &mut T;

    fn into_inner(self) -> T;

    fn compare_and_swap(&self, current: T, new: T, order: Ordering) -> T;

    fn compare_exchange(
        &self,
        current: T,
        new: T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<T, T>;

    fn compare_exchange_weak(
        &self,
        current: T,
        new: T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<T, T>;
}

/// Trait describing structs that implement atomic integer operations.
///
/// Makes generic containers a little easier.
pub trait AtomicNumeric<T>: Atomic<T> {
    fn fetch_add(&self, val: T, order: Ordering) -> T;
    fn fetch_sub(&self, val: T, order: Ordering) -> T;
}

/// Trait describing structs that implement atomic bitwise operations.
///
/// Makes generic containers a little easier.
pub trait AtomicBitwise<T>: Atomic<T> {
    fn fetch_and(&self, val: T, order: Ordering) -> T;
    fn fetch_or(&self, val: T, order: Ordering) -> T;
    fn fetch_xor(&self, val: T, order: Ordering) -> T;
    fn fetch_nand(&self, val: T, order: Ordering) -> T;
}