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
//! Synchronization facade to choose between `core` primitives and `loom` primitives.

#[cfg(all(feature = "portable-atomic", not(loom)))]
mod sync_impl {
    pub(crate) use core::cell;
    pub(crate) use portable_atomic as atomic;

    #[cfg(not(feature = "std"))]
    pub(crate) use atomic::hint::spin_loop;

    #[cfg(feature = "std")]
    pub(crate) use std::thread::yield_now;
}

#[cfg(all(not(feature = "portable-atomic"), not(loom)))]
mod sync_impl {
    pub(crate) use core::cell;
    pub(crate) use core::sync::atomic;

    #[cfg(not(feature = "std"))]
    #[inline]
    pub(crate) fn spin_loop() {
        #[allow(deprecated)]
        atomic::spin_loop_hint();
    }

    #[cfg(feature = "std")]
    pub(crate) use std::thread::yield_now;
}

#[cfg(loom)]
mod sync_impl {
    pub(crate) use loom::cell;

    pub(crate) mod atomic {
        pub(crate) use loom::sync::atomic::*;
    }

    #[cfg(not(feature = "std"))]
    pub(crate) use loom::hint::spin_loop;
    #[cfg(feature = "std")]
    pub(crate) use loom::thread::yield_now;
}

pub(crate) use sync_impl::*;

/// Notify the CPU that we are currently busy-waiting.
#[inline]
pub(crate) fn busy_wait() {
    #[cfg(feature = "std")]
    yield_now();

    #[cfg(not(feature = "std"))]
    spin_loop();
}

#[cfg(loom)]
pub(crate) mod prelude {}

#[cfg(not(loom))]
pub(crate) mod prelude {
    use super::{atomic, cell};

    /// Emulate `loom::UnsafeCell`'s API.
    pub(crate) trait UnsafeCellExt {
        type Value;

        fn with_mut<R, F>(&self, f: F) -> R
        where
            F: FnOnce(*mut Self::Value) -> R;
    }

    impl<T> UnsafeCellExt for cell::UnsafeCell<T> {
        type Value = T;

        fn with_mut<R, F>(&self, f: F) -> R
        where
            F: FnOnce(*mut Self::Value) -> R,
        {
            f(self.get())
        }
    }

    /// Emulate `loom::Atomic*`'s API.
    pub(crate) trait AtomicExt {
        type Value;

        fn with_mut<R, F>(&mut self, f: F) -> R
        where
            F: FnOnce(&mut Self::Value) -> R;
    }

    impl AtomicExt for atomic::AtomicUsize {
        type Value = usize;

        fn with_mut<R, F>(&mut self, f: F) -> R
        where
            F: FnOnce(&mut Self::Value) -> R,
        {
            f(self.get_mut())
        }
    }

    impl<T> AtomicExt for atomic::AtomicPtr<T> {
        type Value = *mut T;

        fn with_mut<R, F>(&mut self, f: F) -> R
        where
            F: FnOnce(&mut Self::Value) -> R,
        {
            f(self.get_mut())
        }
    }
}