echeneis 0.1.1

Testing framework for non-blocking code
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
pub use crate::core::sync::atomic::Ordering;

macro_rules! atomic_int {
    ($name: ident, $int_type: ident) => {
        #[doc = concat!(
            " Mock implementation of `portable_atomic::` or `std::sync::atomic::` `", stringify!($name), "`"
        )]
        #[derive(Debug)]
        pub struct $name($crate::core::sync::atomic::$name);

        impl $name {
            #[track_caller]
            fn access<R>(&self, f: impl FnOnce(&$crate::core::sync::atomic::$name) -> R) -> R {
                $crate::core::rt::env::yield_current(
                    $crate::core::rt::YieldData::AtomicTransition(
                        Some(std::panic::Location::caller())
                    )
                );
                f(&self.0)
            }

            #[doc = concat!(" Creates a new instance of `", stringify!($name), "`.")]
            #[track_caller]
            pub fn new(v: $int_type) -> Self {
                Self($crate::core::sync::atomic::$name::new(v))
            }

            /// Get access to a mutable reference to the inner value.
            #[track_caller]
            pub fn get_mut(&mut self) -> &mut $int_type {
                // Exclusive mutable borrows do not need an execution checkpoint
                // because no other thread can concurrently observe this state.
                self.0.get_mut()
            }

            /// Consumes the atomic and returns the contained value.
            #[track_caller]
            pub fn into_inner(self) -> $int_type {
                self.0.into_inner()
            }

            /// Loads a value from the atomic integer.
            #[track_caller]
            pub fn load(&self, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.load(order))
            }

            /// Stores a value into the atomic integer.
            #[track_caller]
            pub fn store(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) {
                self.access(|inner| inner.store(val, order))
            }

            /// Stores a value into the atomic integer, returning the previous value.
            #[track_caller]
            pub fn swap(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.swap(val, order))
            }

            #[cfg(not(feature = "portable-atomic"))]
            /// Stores a value into the atomic integer if the current value is the same as the `current` value.
            #[track_caller]
            pub fn compare_and_swap(
                &self,
                current: $int_type,
                new: $int_type,
                order: $crate::core::sync::atomic::Ordering,
            ) -> $int_type {
                #[allow(deprecated)]
                self.access(|inner| inner.compare_and_swap(current, new, order))
            }

            /// Stores a value into the atomic if the current value is the same as the `current` value.
            #[track_caller]
            pub fn compare_exchange(
                &self,
                current: $int_type,
                new: $int_type,
                success: $crate::core::sync::atomic::Ordering,
                failure: $crate::core::sync::atomic::Ordering,
            ) -> Result<$int_type, $int_type> {
                self.access(|inner| inner.compare_exchange(current, new, success, failure))
            }

            /// Stores a value into the atomic if the current value is the same as the current value.
            #[track_caller]
            pub fn compare_exchange_weak(
                &self,
                current: $int_type,
                new: $int_type,
                success: $crate::core::sync::atomic::Ordering,
                failure: $crate::core::sync::atomic::Ordering,
            ) -> Result<$int_type, $int_type> {
                self.compare_exchange(current, new, success, failure)
            }

            /// Adds to the current value, returning the previous value.
            #[track_caller]
            pub fn fetch_add(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_add(val, order))
            }

            /// Subtracts from the current value, returning the previous value.
            #[track_caller]
            pub fn fetch_sub(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_sub(val, order))
            }

            /// Bitwise "and" with the current value.
            #[track_caller]
            pub fn fetch_and(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_and(val, order))
            }

            /// Bitwise "nand" with the current value.
            #[track_caller]
            pub fn fetch_nand(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_nand(val, order))
            }

            /// Bitwise "or" with the current value.
            #[track_caller]
            pub fn fetch_or(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_or(val, order))
            }

            /// Bitwise "xor" with the current value.
            #[track_caller]
            pub fn fetch_xor(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_xor(val, order))
            }

            /// Stores the maximum of the current and provided value, returning the previous value
            #[track_caller]
            pub fn fetch_max(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_max(val, order))
            }

            /// Stores the minimum of the current and provided value, returning the previous value
            #[track_caller]
            pub fn fetch_min(&self, val: $int_type, order: $crate::core::sync::atomic::Ordering) -> $int_type {
                self.access(|inner| inner.fetch_min(val, order))
            }

            /// Fetches the value, and applies a function to it that returns an optional new value.
            #[track_caller]
            pub fn fetch_update<F>(
                &self,
                set_order: $crate::core::sync::atomic::Ordering,
                fetch_order: $crate::core::sync::atomic::Ordering,
                f: F,
            ) -> Result<$int_type, $int_type>
            where
                F: FnMut($int_type) -> Option<$int_type>,
            {
                self.access(|inner| inner.fetch_update(set_order, fetch_order, f))
            }
        }

        impl Default for $name {
            fn default() -> Self {
                Self::new(Default::default())
            }
        }

        impl From<$int_type> for $name {
            fn from(v: $int_type) -> Self {
                Self::new(v)
            }
        }

    };
}

atomic_int!(AtomicU8, u8);
atomic_int!(AtomicU16, u16);
atomic_int!(AtomicU32, u32);
atomic_int!(AtomicUsize, usize);

atomic_int!(AtomicI8, i8);
atomic_int!(AtomicI16, i16);
atomic_int!(AtomicI32, i32);
atomic_int!(AtomicIsize, isize);

#[cfg(any(target_has_atomic = "64", feature = "atomic-fallback"))]
atomic_int!(AtomicU64, u64);

#[cfg(any(target_has_atomic = "64", feature = "atomic-fallback"))]
atomic_int!(AtomicI64, i64);

#[cfg(any(target_has_atomic = "128", feature = "atomic-fallback"))]
atomic_int!(AtomicU128, u128);

#[cfg(any(target_has_atomic = "128", feature = "atomic-fallback"))]
atomic_int!(AtomicI128, i128);

#[cfg(feature = "atomic-float")]
macro_rules! atomic_float {
    ($name: ident, $float_type: ident) => {
        #[doc = concat!(
                                    " Mock implementation of `portable_atomic::", stringify!($name), "`"
                                )]
        #[derive(Debug)]
        pub struct $name($crate::core::sync::atomic::$name);

        impl $name {
            #[track_caller]
            fn access<R>(&self, f: impl FnOnce(&$crate::core::sync::atomic::$name) -> R) -> R {
                $crate::core::rt::env::yield_current(
                    $crate::core::rt::YieldData::AtomicTransition(Some(
                        std::panic::Location::caller(),
                    )),
                );
                f(&self.0)
            }

            #[doc = concat!(" Creates a new instance of `", stringify!($name), "`.")]
            #[track_caller]
            pub fn new(v: $float_type) -> Self {
                Self($crate::core::sync::atomic::$name::new(v))
            }

            /// Consumes the atomic and returns the contained value.
            #[track_caller]
            pub fn into_inner(self) -> $float_type {
                self.0.into_inner()
            }

            /// Loads a value from the atomic integer.
            #[track_caller]
            pub fn load(&self, order: $crate::core::sync::atomic::Ordering) -> $float_type {
                self.access(|inner| inner.load(order))
            }

            /// Stores a value into the atomic integer.
            #[track_caller]
            pub fn store(&self, val: $float_type, order: $crate::core::sync::atomic::Ordering) {
                self.access(|inner| inner.store(val, order))
            }

            /// Get access to a mutable reference to the inner value.
            #[track_caller]
            pub fn get_mut(&mut self) -> &mut $float_type {
                // Exclusive mutable borrows do not need an execution checkpoint
                // because no other thread can concurrently observe this state.
                self.0.get_mut()
            }

            /// Stores a value into the atomic integer, returning the previous value.
            #[track_caller]
            pub fn swap(
                &self,
                val: $float_type,
                order: $crate::core::sync::atomic::Ordering,
            ) -> $float_type {
                self.access(|inner| inner.swap(val, order))
            }

            /// Stores a value into the atomic if the current value is the same as the `current` value.
            #[track_caller]
            pub fn compare_exchange(
                &self,
                current: $float_type,
                new: $float_type,
                success: $crate::core::sync::atomic::Ordering,
                failure: $crate::core::sync::atomic::Ordering,
            ) -> Result<$float_type, $float_type> {
                self.access(|inner| inner.compare_exchange(current, new, success, failure))
            }

            /// Stores a value into the atomic if the current value is the same as the current value.
            #[track_caller]
            pub fn compare_exchange_weak(
                &self,
                current: $float_type,
                new: $float_type,
                success: $crate::core::sync::atomic::Ordering,
                failure: $crate::core::sync::atomic::Ordering,
            ) -> Result<$float_type, $float_type> {
                self.compare_exchange(current, new, success, failure)
            }

            /// Adds to the current value, returning the previous value.
            #[track_caller]
            pub fn fetch_add(
                &self,
                val: $float_type,
                order: $crate::core::sync::atomic::Ordering,
            ) -> $float_type {
                self.access(|inner| inner.fetch_add(val, order))
            }

            /// Subtracts from the current value, returning the previous value.
            #[track_caller]
            pub fn fetch_sub(
                &self,
                val: $float_type,
                order: $crate::core::sync::atomic::Ordering,
            ) -> $float_type {
                self.access(|inner| inner.fetch_sub(val, order))
            }

            /// Stores the maximum of the current and provided value, returning the previous value
            #[track_caller]
            pub fn fetch_max(
                &self,
                val: $float_type,
                order: $crate::core::sync::atomic::Ordering,
            ) -> $float_type {
                self.access(|inner| inner.fetch_max(val, order))
            }

            /// Stores the minimum of the current and provided value, returning the previous value
            #[track_caller]
            pub fn fetch_min(
                &self,
                val: $float_type,
                order: $crate::core::sync::atomic::Ordering,
            ) -> $float_type {
                self.access(|inner| inner.fetch_min(val, order))
            }

            /// Fetches the value, and applies a function to it that returns an optional new value.
            #[track_caller]
            pub fn fetch_update<F>(
                &self,
                set_order: $crate::core::sync::atomic::Ordering,
                fetch_order: $crate::core::sync::atomic::Ordering,
                f: F,
            ) -> Result<$float_type, $float_type>
            where
                F: FnMut($float_type) -> Option<$float_type>,
            {
                self.access(|inner| inner.fetch_update(set_order, fetch_order, f))
            }
        }

        impl Default for $name {
            fn default() -> Self {
                Self::new(Default::default())
            }
        }

        impl From<$float_type> for $name {
            fn from(v: $float_type) -> Self {
                Self::new(v)
            }
        }
    };
}

#[cfg(feature = "atomic-float")]
atomic_float!(AtomicF32, f32);
#[cfg(feature = "atomic-float")]
atomic_float!(AtomicF64, f64);

/// Mock implementation of `std::sync::atomic::AtomicBool` or `portable_atomic::AtomicBool`.
#[derive(Debug)]
pub struct AtomicBool(crate::core::sync::atomic::AtomicBool);

impl AtomicBool {
    #[track_caller]
    fn access<R>(&self, f: impl FnOnce(&crate::core::sync::atomic::AtomicBool) -> R) -> R {
        crate::core::rt::env::yield_current(crate::core::rt::YieldData::AtomicTransition(Some(
            std::panic::Location::caller(),
        )));
        f(&self.0)
    }

    /// Creates a new instance of `AtomicBool`.
    pub fn new(v: bool) -> Self {
        Self(crate::core::sync::atomic::AtomicBool::new(v))
    }

    /// Get access to a mutable reference to the inner value.
    pub fn get_mut(&mut self) -> &mut bool {
        self.0.get_mut()
    }

    /// Consumes the atomic and returns the contained value.
    #[track_caller]
    pub fn into_inner(self) -> bool {
        self.0.into_inner()
    }

    /// Loads a value from the atomic bool.
    #[track_caller]
    pub fn load(&self, order: Ordering) -> bool {
        self.access(|inner| inner.load(order))
    }

    /// Stores a value into the atomic bool.
    #[track_caller]
    pub fn store(&self, val: bool, order: Ordering) {
        self.access(|inner| inner.store(val, order));
    }

    /// Stores a value into the atomic bool, returning the previous value.
    #[track_caller]
    pub fn swap(&self, val: bool, order: Ordering) -> bool {
        self.access(|inner| inner.swap(val, order))
    }

    #[cfg(not(feature = "portable-atomic"))]
    /// Stores a value into the atomic bool if the current value is the same as the `current` value.
    #[track_caller]
    pub fn compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool {
        #[allow(deprecated)]
        self.access(|inner| inner.compare_and_swap(current, new, order))
    }

    /// Stores a value into the atomic if the current value is the same as the `current` value.
    #[track_caller]
    pub fn compare_exchange(
        &self,
        current: bool,
        new: bool,
        success: Ordering,
        failure: Ordering,
    ) -> Result<bool, bool> {
        self.access(|inner| inner.compare_exchange(current, new, success, failure))
    }

    /// Stores a value into the atomic if the current value is the same as the current value.
    #[track_caller]
    pub fn compare_exchange_weak(
        &self,
        current: bool,
        new: bool,
        success: Ordering,
        failure: Ordering,
    ) -> Result<bool, bool> {
        self.compare_exchange(current, new, success, failure)
    }

    /// Logical "and" with the current value.
    #[track_caller]
    pub fn fetch_and(&self, val: bool, order: Ordering) -> bool {
        self.access(|inner| inner.fetch_and(val, order))
    }

    /// Logical "nand" with the current value.
    #[track_caller]
    pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool {
        self.access(|inner| inner.fetch_nand(val, order))
    }

    /// Logical "or" with the current value.
    #[track_caller]
    pub fn fetch_or(&self, val: bool, order: Ordering) -> bool {
        self.access(|inner| inner.fetch_or(val, order))
    }

    /// Logical "xor" with the current value.
    #[track_caller]
    pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
        self.access(|inner| inner.fetch_xor(val, order))
    }

    /// Fetches the value, and applies a function to it that returns an optional new value.
    #[track_caller]
    pub fn fetch_update<F>(
        &self,
        set_order: Ordering,
        fetch_order: Ordering,
        f: F,
    ) -> Result<bool, bool>
    where
        F: FnMut(bool) -> Option<bool>,
    {
        self.access(|inner| inner.fetch_update(set_order, fetch_order, f))
    }
}

impl Default for AtomicBool {
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl From<bool> for AtomicBool {
    fn from(v: bool) -> Self {
        Self::new(v)
    }
}

/// Mock implementation of `std::sync::atomic::AtomicPtr` or `portable_atomic::AtomicPtr`.
pub struct AtomicPtr<T>(crate::core::sync::atomic::AtomicPtr<T>);

impl<T> std::fmt::Debug for AtomicPtr<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self.0, f)
    }
}

impl<T> AtomicPtr<T> {
    #[track_caller]
    fn access<R>(&self, f: impl FnOnce(&crate::core::sync::atomic::AtomicPtr<T>) -> R) -> R {
        crate::core::rt::env::yield_current(crate::core::rt::YieldData::AtomicTransition(Some(
            std::panic::Location::caller(),
        )));
        f(&self.0)
    }

    /// Creates a new instance of `AtomicPtr`.
    pub fn new(v: *mut T) -> Self {
        Self(crate::core::sync::atomic::AtomicPtr::new(v))
    }

    #[cfg(not(feature = "portable-atomic"))]
    /// Get access to a mutable reference to the inner value.
    #[track_caller]
    pub fn get_mut(&mut self) -> &mut *mut T {
        self.0.get_mut()
    }

    /// Consumes the atomic and returns the contained value.
    #[track_caller]
    pub fn into_inner(self) -> *mut T {
        self.0.into_inner()
    }

    /// Loads a value from the pointer.
    #[track_caller]
    pub fn load(&self, order: Ordering) -> *mut T {
        self.access(|inner| inner.load(order))
    }

    /// Stores a value into the pointer.
    #[track_caller]
    pub fn store(&self, val: *mut T, order: Ordering) {
        self.access(|inner| inner.store(val, order));
    }

    /// Stores a value into the pointer, returning the previous value.
    #[track_caller]
    pub fn swap(&self, val: *mut T, order: Ordering) -> *mut T {
        self.access(|inner| inner.swap(val, order))
    }

    #[cfg(not(feature = "portable-atomic"))]
    /// Stores a value into the pointer if the current value is the same as the `current` value.
    #[track_caller]
    pub fn compare_and_swap(&self, current: *mut T, new: *mut T, order: Ordering) -> *mut T {
        #[allow(deprecated)]
        self.access(|inner| inner.compare_and_swap(current, new, order))
    }

    /// Stores a value into the pointer if the current value is the same as the `current` value.
    #[track_caller]
    pub fn compare_exchange(
        &self,
        current: *mut T,
        new: *mut T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<*mut T, *mut T> {
        self.access(|inner| inner.compare_exchange(current, new, success, failure))
    }

    /// Stores a value into the atomic if the current value is the same as the current value.
    #[track_caller]
    pub fn compare_exchange_weak(
        &self,
        current: *mut T,
        new: *mut T,
        success: Ordering,
        failure: Ordering,
    ) -> Result<*mut T, *mut T> {
        self.compare_exchange(current, new, success, failure)
    }

    /// Fetches the value, and applies a function to it that returns an optional new value.
    #[track_caller]
    pub fn fetch_update<F>(
        &self,
        set_order: Ordering,
        fetch_order: Ordering,
        f: F,
    ) -> Result<*mut T, *mut T>
    where
        F: FnMut(*mut T) -> Option<*mut T>,
    {
        self.access(|inner| inner.fetch_update(set_order, fetch_order, f))
    }
}

impl<T> Default for AtomicPtr<T> {
    fn default() -> Self {
        Self::new(std::ptr::null_mut())
    }
}

impl<T> From<*mut T> for AtomicPtr<T> {
    fn from(v: *mut T) -> Self {
        Self::new(v)
    }
}

/// Mock implementation of `std::sync::atomic::fence`.
#[track_caller]
pub fn fence(order: Ordering) {
    crate::core::rt::env::yield_current(crate::core::rt::YieldData::AtomicTransition(Some(
        std::panic::Location::caller(),
    )));
    crate::core::sync::atomic::fence(order);
}