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
use core::cell::UnsafeCell;
use core::ops::Deref;
use core::ops::DerefMut;
use core::sync::atomic::AtomicU32;
use core::sync::atomic::Ordering;

/// Holds up 2^31 values
#[repr(C)]
pub struct Spinlock<T> {
    data: UnsafeCell<T>,
    lock: AtomicU32,
}

impl<T> Spinlock<T> {
    const LOCK: u32 = 1 << 31;
    const MASK: u32 = !(Spinlock::<T>::LOCK); // | Spinlock::PANIC

    #[inline(always)]
    pub const fn new(value: u32, data: T) -> Spinlock<T> {
        return Spinlock {
            lock: AtomicU32::new(value & Spinlock::<T>::MASK),
            data: UnsafeCell::new(data),
        };
    }

    #[cfg(any(test, feature = "std"))]
    #[inline(always)]
    fn increment_yield_counter(value: u32) -> u32 {
        if value > 2 {
            std::thread::yield_now();
            return 0;
        }
        return value + 1;
    }

    #[inline(always)]
    pub fn lock(&self) -> SpinlockGuard<T> {
        #[cfg(any(test, feature = "std"))]
        let mut counter = 0;
        let mut lock_value = self.lock.load(Ordering::Acquire);
        loop {
            if lock_value < Spinlock::<T>::LOCK {
                let target = lock_value | Spinlock::<T>::LOCK;
                match self.lock.compare_exchange_weak(
                    lock_value,
                    target,
                    Ordering::Acquire,
                    Ordering::Acquire,
                ) {
                    Ok(_) => {
                        return SpinlockGuard {
                            lock: self,
                            value: lock_value,
                        }
                    }
                    Err(x) => lock_value = x,
                }
            } else {
                #[cfg(any(test, feature = "std"))]
                {
                    counter = Spinlock::<T>::increment_yield_counter(counter);
                }

                lock_value = self.lock.load(Ordering::Acquire);
            }
            core::sync::atomic::spin_loop_hint();
        }
    }

    /// Since this call borrows the Lock mutably, no actual locking needs to take place --
    /// the mutable borrow statically guarantees no locks exist.
    #[inline(always)]
    pub fn get(&mut self) -> u32 {
        return self.lock.load(Ordering::Relaxed) & Spinlock::<T>::MASK;
    }

    /// Since this call borrows the Lock mutably, no actual locking needs to take place --
    /// the mutable borrow statically guarantees no locks exist.
    #[inline(always)]
    pub fn set(&mut self, value: u32) {
        return self
            .lock
            .store(value & Spinlock::<T>::MASK, Ordering::Relaxed);
    }

    /// Move the value out of the lock, consuming the lock;
    #[inline(always)]
    pub fn into_inner(self) -> u32 {
        return self.lock.load(Ordering::Relaxed) & Spinlock::<T>::MASK;
    }
}

unsafe impl<T: Send> Send for Spinlock<T> {}
unsafe impl<T: Send> Sync for Spinlock<T> {}

pub struct SpinlockGuard<'a, T: 'a> {
    lock: &'a Spinlock<T>,
    value: u32,
}

impl<'a, T> SpinlockGuard<'a, T> {
    /// Get the value that was set on the lock at acquisition time.
    #[inline(always)]
    pub fn read(&self) -> u32 {
        return self.value;
    }
    /// Set the value that will be written on release.
    #[inline(always)]
    pub fn write(&mut self, value: u32) {
        return self.value = value & Spinlock::<T>::MASK;
    }
}
impl<'a, T> Drop for SpinlockGuard<'a, T> {
    fn drop(&mut self) {
        self.lock.lock.store(self.value, Ordering::Release);
    }
}

impl<'a, T> Deref for SpinlockGuard<'a, T> {
    type Target = T;
    fn deref<'b>(&'b self) -> &'b T {
        return unsafe { &*self.lock.data.get() };
    }
}

impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
    fn deref_mut<'b>(&'b mut self) -> &'b mut T {
        return unsafe { &mut *self.lock.data.get() };
    }
}

/// Holds up 2^31 values
pub struct IndexSpinlock {
    lock: AtomicU32,
}

impl IndexSpinlock {
    const LOCK: u32 = 1 << 31;
    const MASK: u32 = !(IndexSpinlock::LOCK); // | IndexSpinlock::PANIC

    #[inline(always)]
    pub const fn new(value: u32) -> IndexSpinlock {
        return IndexSpinlock {
            lock: AtomicU32::new(value & IndexSpinlock::MASK),
        };
    }

    #[cfg(any(test, feature = "std"))]
    #[inline(always)]
    fn increment_yield_counter(value: u32) -> u32 {
        if value > 1 {
            std::thread::yield_now();
            return 0;
        }
        return value + 1;
    }

    #[inline(always)]
    pub fn lock(&self) -> IndexSpinlockGuard {
        #[cfg(any(test, feature = "std"))]
        let mut counter = 0;
        let mut lock_value = self.lock.load(Ordering::Acquire);
        loop {
            if lock_value < IndexSpinlock::LOCK {
                let target = lock_value | IndexSpinlock::LOCK;
                match self.lock.compare_exchange_weak(
                    lock_value,
                    target,
                    Ordering::Acquire,
                    Ordering::Acquire,
                ) {
                    Ok(_) => {
                        return IndexSpinlockGuard {
                            lock: self,
                            value: lock_value,
                        }
                    }
                    Err(x) => lock_value = x,
                }
            } else {
                #[cfg(any(test, feature = "std"))]
                {
                    counter = IndexSpinlock::increment_yield_counter(counter);
                }

                lock_value = self.lock.load(Ordering::Acquire);
            }
            core::sync::atomic::spin_loop_hint();
        }
    }

    /// Since this call borrows the Lock mutably, no actual locking needs to take place --
    /// the mutable borrow statically guarantees no locks exist.
    #[inline(always)]
    pub fn get(&mut self) -> u32 {
        return self.lock.load(Ordering::Relaxed) & IndexSpinlock::MASK;
    }

    /// Since this call borrows the Lock mutably, no actual locking needs to take place --
    /// the mutable borrow statically guarantees no locks exist.
    #[inline(always)]
    pub fn set(&mut self, value: u32) {
        return self
            .lock
            .store(value & IndexSpinlock::MASK, Ordering::Relaxed);
    }

    /// Move the value out of the lock, consuming the lock;
    #[inline(always)]
    pub fn into_inner(self) -> u32 {
        return self.lock.load(Ordering::Relaxed) & IndexSpinlock::MASK;
    }
}

unsafe impl Send for IndexSpinlock {}
unsafe impl Sync for IndexSpinlock {}

pub struct IndexSpinlockGuard<'a> {
    lock: &'a IndexSpinlock,
    value: u32,
}

impl<'a> IndexSpinlockGuard<'a> {
    /// Get the value that was set on the lock at acquisition time.
    #[inline(always)]
    pub fn read(&self) -> u32 {
        return self.value;
    }
    /// Set the value that will be written on release.
    #[inline(always)]
    pub fn write(&mut self, value: u32) {
        return self.value = value & IndexSpinlock::MASK;
    }
}
impl<'a> Drop for IndexSpinlockGuard<'a> {
    fn drop(&mut self) {
        self.lock.lock.store(self.value, Ordering::Release);
    }
}

#[cfg(test)]
mod test;