lockfree_object_pool/
spin_lock.rs

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
use std::cell::UnsafeCell;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;

pub struct SpinLock<T> {
    data: UnsafeCell<T>,
    lock: AtomicBool,
}

impl<T> SpinLock<T> {
    #[inline]
    pub fn new(data: T) -> Self {
        Self {
            data: UnsafeCell::new(data),
            lock: AtomicBool::new(false),
        }
    }

    #[inline]
    pub fn lock(&self) -> SpinLockGuard<T> {
        self.acquire();
        SpinLockGuard { lock: self }
    }

    #[inline]
    fn acquire(&self) {
        self.exchange(false, true);
    }

    #[inline]
    fn release(&self) {
        self.exchange(true, false);
    }

    #[inline]
    fn exchange(&self, from: bool, to: bool) {
        loop {
            match self
                .lock
                .compare_exchange_weak(from, to, Ordering::SeqCst, Ordering::Relaxed)
            {
                Ok(_) => break,
                Err(_) => {
                    thread::yield_now();
                }
            }
        }
    }
}

unsafe impl<T: Send> Send for SpinLock<T> {} // SAFETY: sending the data is allowed if it's Send
unsafe impl<T: Send> Sync for SpinLock<T> {} // SAFETY: the Mutex manages synchronization so only Send is required

pub struct SpinLockGuard<'a, T> {
    lock: &'a SpinLock<T>,
}

impl<'a, T> DerefMut for SpinLockGuard<'a, T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe {
            // SAFETY: this is the only active guard
            &mut *self.lock.data.get()
        }
    }
}

impl<'a, T> Deref for SpinLockGuard<'a, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe {
            // SAFETY: this is the only active guard
            &*self.lock.data.get()
        }
    }
}

impl<'a, T> Drop for SpinLockGuard<'a, T> {
    #[inline]
    fn drop(&mut self) {
        self.lock.release();
    }
}

unsafe impl<T: Send> Send for SpinLockGuard<'_, T> {} // SAFETY: normal rules apply
unsafe impl<T: Sync> Sync for SpinLockGuard<'_, T> {} // SAFETY: normal rules apply