atomex 0.1.1

Atomics extensions in Rust
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use core::{marker::PhantomData, sync::atomic::*};
use crate::fetch;

pub trait TrAtomicCell {
    /// The underlying primitive value type
    type Value: Copy;

    fn new(val: Self::Value) -> Self;

    fn into_inner(self) -> Self::Value;

    /// Loads a value from the cell.
    /// 
    /// `load` takes an [`Ordering`] argument which describes the memory ordering
    /// of this operation. Possible values are [`SeqCst`], [`Acquire`] and [`Relaxed`].
    ///
    /// # Panics
    ///
    /// Panics if `order` is [`Release`] or [`AcqRel`].
    fn load(
        &self,
        order: Ordering,
    ) -> Self::Value;

    /// Stores a value into the cell.
    /// 
    /// `store` takes an [`Ordering`] argument which describes the memory ordering
    /// of this operation. Possible values are [`SeqCst`], [`Release`] and [`Relaxed`].
    ///
    /// # Panics
    ///
    /// Panics if `order` is [`Acquire`] or [`AcqRel`].
    fn store(
        &self,
        val: Self::Value,
        order: Ordering,
    );

    /// Stores a value into the cell, returning the previous value.
    /// 
    /// `swap` takes an [`Ordering`] argument which describes the memory ordering
    /// of this operation. All ordering modes are possible. Note that using
    /// [`Acquire`] makes the store part of this operation [`Relaxed`], and
    /// using [`Release`] makes the load part [`Relaxed`].
    ///
    /// **Note:** This method is only available on platforms that support atomic
    /// operations on pointers.
    fn swap(
        &self,
        val: Self::Value,
        order: Ordering,
    ) -> Self::Value;

    /// Stores a value into the atomic type if the current value is the same as
    /// the `current` value.
    ///
    /// The return value is a result indicating whether the new value was
    /// written and containing the previous value. On success this value is
    /// guaranteed to be equal to `current`.
    fn compare_exchange(
        &self,
        current: Self::Value,
        desired: Self::Value,
        success: Ordering,
        failure: Ordering,
    ) -> Result<Self::Value, Self::Value>;

    /// Stores a value into the atomic type if the current value is the same as
    /// the current value.
    ///
    /// Unlike `compare_exchange`, this function is allowed to spuriously fail
    /// even when the comparison succeeds, which can result in more
    /// efficient code on some platforms. The return value is a result
    /// indicating whether the new value was written and containing the previous
    /// value.
    fn compare_exchange_weak(
        &self,
        current: Self::Value,
        desired: Self::Value,
        success: Ordering,
        failure: Ordering,
    ) -> Result<Self::Value, Self::Value>;
}

pub trait TrAtomicData {
    type AtomicCell: TrAtomicCell<Value = Self>;
}

/// The trait for types implementing atomic bitwise operations
pub trait Bitwise:
    TrAtomicCell
    + fetch::And<Value = <Self as TrAtomicCell>::Value>
    + fetch::Nand<Value = <Self as TrAtomicCell>::Value>
    + fetch::Or<Value = <Self as TrAtomicCell>::Value>
    + fetch::Xor<Value = <Self as TrAtomicCell>::Value>
{}

/// The trait for types implementing atomic numeric operations
pub trait NumOps:
    TrAtomicCell
    + fetch::Add<Value = <Self as TrAtomicCell>::Value>
    + fetch::Sub<Value = <Self as TrAtomicCell>::Value>
    + fetch::Update<Value = <Self as TrAtomicCell>::Value>
    + fetch::Max<Value = <Self as TrAtomicCell>::Value>
    + fetch::Min<Value = <Self as TrAtomicCell>::Value>
{}

/// An helper trait to define spinlock ordering used in atomic operation
pub trait TrCmpxchOrderings: Unpin {
    const SUCC_ORDERING: Ordering;
    const FAIL_ORDERING: Ordering;
    const LOAD_ORDERING: Ordering;
}

/// Provide the most strict orderings with cost of higher overhead.
///
/// If you don't know which one to use, this is the best choice.
#[derive(Clone, Copy, Debug, Default)]
pub struct StrictOrderings;

impl TrCmpxchOrderings for StrictOrderings {
    const SUCC_ORDERING: Ordering = Ordering::SeqCst;
    const FAIL_ORDERING: Ordering = Ordering::SeqCst;
    const LOAD_ORDERING: Ordering = Ordering::SeqCst;
}

#[derive(Clone, Copy, Debug, Default)]
pub struct LocksOrderings;

impl TrCmpxchOrderings for LocksOrderings {
    const SUCC_ORDERING: Ordering = Ordering::Acquire;
    const FAIL_ORDERING: Ordering = Ordering::Relaxed;
    const LOAD_ORDERING: Ordering = Ordering::Acquire;
}

pub type PhantomAtomicPtr<T> = PhantomData<AtomicPtr<T>>;

#[cfg(target_has_atomic = "8")]
impl TrAtomicData for i8 {
    type AtomicCell = AtomicI8;
}

#[cfg(target_has_atomic = "8")]
impl TrAtomicData for u8 {
    type AtomicCell = AtomicU8;
}

#[cfg(target_has_atomic = "16")]
impl TrAtomicData for i16 {
    type AtomicCell = AtomicI16;
}

#[cfg(target_has_atomic = "16")]
impl TrAtomicData for u16 {
    type AtomicCell = AtomicU16;
}

#[cfg(target_has_atomic = "32")]
impl TrAtomicData for i32 {
    type AtomicCell = AtomicI32;
}

#[cfg(target_has_atomic = "32")]
impl TrAtomicData for u32 {
    type AtomicCell = AtomicU32;
}

#[cfg(target_has_atomic = "64")]
impl TrAtomicData for i64 {
    type AtomicCell = AtomicI64;
}

#[cfg(target_has_atomic = "64")]
impl TrAtomicData for u64 {
    type AtomicCell = AtomicU64;
}

#[cfg(all(target_has_atomic = "128", feature = "support_u128_i128_atomics"))]
impl TrAtomicData for i128 {
    type AtomicCell = AtomicI128;
}

#[cfg(all(target_has_atomic = "128", feature = "support_u128_i128_atomics"))]
impl TrAtomicData for u128 {
    type AtomicCell = AtomicU128;
}

impl TrAtomicData for isize {
    type AtomicCell = AtomicIsize;
}

impl TrAtomicData for usize {
    type AtomicCell = AtomicUsize;
}

impl TrAtomicData for bool {
    type AtomicCell = AtomicBool;
}

impl<T> TrAtomicData for *mut T {
    type AtomicCell = AtomicPtr<T>;
}

macro_rules! impl_atomic {
    ($atomic:ident : $primitive:ty ; $( $traits:tt ),*) => {
        impl_atomic!(__impl atomic $atomic : $primitive);

        $(
            impl_atomic!(__impl $traits $atomic : $primitive);
        )*

    };
    ($atomic:ident < $param:ident >) => {
        impl<$param> TrAtomicCell for $atomic <$param> {
            type Value = *mut $param;

            impl_atomic!(__impl atomic_methods $atomic);
        }
    };

    (__impl atomic $atomic:ident : $primitive:ty) => {
        impl TrAtomicCell for $atomic {
            type Value = $primitive;

            impl_atomic!(__impl atomic_methods $atomic);
        }
    };

    (__impl atomic_methods $atomic:ident) => {
        #[inline(always)]
        fn new(v: Self::Value) -> Self {
            Self::new(v)
        }

        #[inline(always)]
        fn into_inner(self) -> Self::Value {
            Self::into_inner(self)
        }

        #[inline(always)]
        fn load(&self, order: Ordering) -> Self::Value {
            Self::load(self, order)
        }

        #[inline(always)]
        fn store(&self, val: Self::Value, order: Ordering) {
            Self::store(self, val, order)
        }

        #[inline(always)]
        fn swap(&self, val: Self::Value, order: Ordering) -> Self::Value {
            Self::swap(self, val, order)
        }

        #[inline(always)]
        fn compare_exchange(
            &self,
            current: Self::Value,
            desired: Self::Value,
            success: Ordering,
            failure: Ordering,
        ) -> Result<Self::Value, Self::Value> {
            Self::compare_exchange(self, current, desired, success, failure)
        }

        #[inline(always)]
        fn compare_exchange_weak(
            &self,
            current: Self::Value,
            desired: Self::Value,
            success: Ordering,
            failure: Ordering,
        ) -> Result<Self::Value, Self::Value> {
            Self::compare_exchange_weak(self, current, desired, success, failure)
        }
    };

    (__impl bitwise $atomic:ident : $primitive:ty) => {
        impl Bitwise for $atomic {}

        impl $crate::fetch::And for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_and(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_and(self, val, order)
            }
        }

        impl $crate::fetch::Nand for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_nand(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_nand(self, val, order)
            }
        }

        impl $crate::fetch::Or for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_or(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_or(self, val, order)
            }
        }

        impl $crate::fetch::Xor for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_xor(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_xor(self, val, order)
            }
        }
    };

    (__impl numops $atomic:ident : $primitive:ty) => {
        impl NumOps for $atomic {}

        impl $crate::fetch::Add for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_add(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_add(self, val, order)
            }
        }

        impl $crate::fetch::Sub for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_sub(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_sub(self, val, order)
            }
        }

        impl $crate::fetch::Update for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_update<F>(
                &self,
                fetch_order: Ordering,
                set_order: Ordering,
                f: F,
            ) -> Result<Self::Value, Self::Value>
            where
                F: FnMut(Self::Value) -> Option<Self::Value> {
                Self::fetch_update(self, fetch_order, set_order, f)
            }
        }

        impl $crate::fetch::Max for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_max(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_max(self, val, order)
            }
        }

        impl $crate::fetch::Min for $atomic {
            type Value = $primitive;

            #[inline(always)]
            fn fetch_min(&self, val: Self::Value, order: Ordering) -> Self::Value {
                Self::fetch_min(self, val, order)
            }
        }
    };
}

impl_atomic!(AtomicBool: bool; bitwise);
impl_atomic!(AtomicIsize: isize; bitwise, numops);
impl_atomic!(AtomicUsize: usize; bitwise, numops);
impl_atomic!(AtomicPtr<T>);

#[cfg(target_has_atomic = "8")]
impl_atomic!(AtomicI8: i8; bitwise, numops);

#[cfg(target_has_atomic = "16")]
impl_atomic!(AtomicI16: i16; bitwise, numops);

#[cfg(target_has_atomic = "32")]
impl_atomic!(AtomicI32: i32; bitwise, numops);

#[cfg(target_has_atomic = "64")]
impl_atomic!(AtomicI64: i64; bitwise, numops);

#[cfg(all(target_has_atomic = "128", feature = "support_u128_i128_atomics"))]
impl_atomic!(AtomicI128: i128; bitwise, numops);

#[cfg(target_has_atomic = "8")]
impl_atomic!(AtomicU8: u8; bitwise, numops);

#[cfg(target_has_atomic = "16")]
impl_atomic!(AtomicU16: u16; bitwise, numops);

#[cfg(target_has_atomic = "32")]
impl_atomic!(AtomicU32: u32; bitwise, numops);

#[cfg(target_has_atomic = "64")]
impl_atomic!(AtomicU64: u64; bitwise, numops);

#[cfg(all(target_has_atomic = "128", feature = "support_u128_i128_atomics"))]
impl_atomic!(AtomicU128: u128; bitwise, numops);