|
| 1 | +use crate::sync::atomic::{ |
| 2 | + AtomicI32, |
| 3 | + Ordering::{Acquire, Relaxed, Release}, |
| 4 | +}; |
| 5 | +use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all}; |
| 6 | +use crate::time::Duration; |
| 7 | + |
| 8 | +pub type MovableMutex = Mutex; |
| 9 | +pub type MovableCondvar = Condvar; |
| 10 | + |
| 11 | +pub struct Mutex { |
| 12 | + /// 0: unlocked |
| 13 | + /// 1: locked, no other threads waiting |
| 14 | + /// 2: locked, and other threads waiting (contended) |
| 15 | + futex: AtomicI32, |
| 16 | +} |
| 17 | + |
| 18 | +impl Mutex { |
| 19 | + #[inline] |
| 20 | + pub const fn new() -> Self { |
| 21 | + Self { futex: AtomicI32::new(0) } |
| 22 | + } |
| 23 | + |
| 24 | + #[inline] |
| 25 | + pub unsafe fn init(&mut self) {} |
| 26 | + |
| 27 | + #[inline] |
| 28 | + pub unsafe fn destroy(&self) {} |
| 29 | + |
| 30 | + #[inline] |
| 31 | + pub unsafe fn try_lock(&self) -> bool { |
| 32 | + self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_ok() |
| 33 | + } |
| 34 | + |
| 35 | + #[inline] |
| 36 | + pub unsafe fn lock(&self) { |
| 37 | + if self.futex.compare_exchange(0, 1, Acquire, Relaxed).is_err() { |
| 38 | + self.lock_contended(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + #[cold] |
| 43 | + fn lock_contended(&self) { |
| 44 | + // Spin first to speed things up if the lock is released quickly. |
| 45 | + let mut state = self.spin(); |
| 46 | + |
| 47 | + // If it's unlocked now, attempt to take the lock |
| 48 | + // without marking it as contended. |
| 49 | + if state == 0 { |
| 50 | + match self.futex.compare_exchange(0, 1, Acquire, Relaxed) { |
| 51 | + Ok(_) => return, // Locked! |
| 52 | + Err(s) => state = s, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + loop { |
| 57 | + // Put the lock in contended state. |
| 58 | + // We avoid an unnecessary write if it as already set to 2, |
| 59 | + // to be friendlier for the caches. |
| 60 | + if state != 2 && self.futex.swap(2, Acquire) == 0 { |
| 61 | + // We changed it from 0 to 2, so we just succesfully locked it. |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Wait for the futex to change state, assuming it is still 2. |
| 66 | + futex_wait(&self.futex, 2, None); |
| 67 | + |
| 68 | + // Spin again after waking up. |
| 69 | + state = self.spin(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fn spin(&self) -> i32 { |
| 74 | + let mut spin = 100; |
| 75 | + loop { |
| 76 | + // We only use `load` (and not `swap` or `compare_exchange`) |
| 77 | + // while spinning, to be easier on the caches. |
| 78 | + let state = self.futex.load(Relaxed); |
| 79 | + |
| 80 | + // We stop spinning when the mutex is unlocked (0), |
| 81 | + // but also when it's contended (2). |
| 82 | + if state != 1 || spin == 0 { |
| 83 | + return state; |
| 84 | + } |
| 85 | + |
| 86 | + crate::hint::spin_loop(); |
| 87 | + spin -= 1; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + #[inline] |
| 92 | + pub unsafe fn unlock(&self) { |
| 93 | + if self.futex.swap(0, Release) == 2 { |
| 94 | + // We only wake up one thread. When that thread locks the mutex, it |
| 95 | + // will mark the mutex as contended (2) (see lock_contended above), |
| 96 | + // which makes sure that any other waiting threads will also be |
| 97 | + // woken up eventually. |
| 98 | + self.wake(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + #[cold] |
| 103 | + fn wake(&self) { |
| 104 | + futex_wake(&self.futex); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +pub struct Condvar { |
| 109 | + // The value of this atomic is simply incremented on every notification. |
| 110 | + // This is used by `.wait()` to not miss any notifications after |
| 111 | + // unlocking the mutex and before waiting for notifications. |
| 112 | + futex: AtomicI32, |
| 113 | +} |
| 114 | + |
| 115 | +impl Condvar { |
| 116 | + #[inline] |
| 117 | + pub const fn new() -> Self { |
| 118 | + Self { futex: AtomicI32::new(0) } |
| 119 | + } |
| 120 | + |
| 121 | + #[inline] |
| 122 | + pub unsafe fn init(&mut self) {} |
| 123 | + |
| 124 | + #[inline] |
| 125 | + pub unsafe fn destroy(&self) {} |
| 126 | + |
| 127 | + // All the memory orderings here are `Relaxed`, |
| 128 | + // because synchronization is done by unlocking and locking the mutex. |
| 129 | + |
| 130 | + pub unsafe fn notify_one(&self) { |
| 131 | + self.futex.fetch_add(1, Relaxed); |
| 132 | + futex_wake(&self.futex); |
| 133 | + } |
| 134 | + |
| 135 | + pub unsafe fn notify_all(&self) { |
| 136 | + self.futex.fetch_add(1, Relaxed); |
| 137 | + futex_wake_all(&self.futex); |
| 138 | + } |
| 139 | + |
| 140 | + pub unsafe fn wait(&self, mutex: &Mutex) { |
| 141 | + self.wait_optional_timeout(mutex, None); |
| 142 | + } |
| 143 | + |
| 144 | + pub unsafe fn wait_timeout(&self, mutex: &Mutex, timeout: Duration) -> bool { |
| 145 | + self.wait_optional_timeout(mutex, Some(timeout)) |
| 146 | + } |
| 147 | + |
| 148 | + unsafe fn wait_optional_timeout(&self, mutex: &Mutex, timeout: Option<Duration>) -> bool { |
| 149 | + // Examine the notification counter _before_ we unlock the mutex. |
| 150 | + let futex_value = self.futex.load(Relaxed); |
| 151 | + |
| 152 | + // Unlock the mutex before going to sleep. |
| 153 | + mutex.unlock(); |
| 154 | + |
| 155 | + // Wait, but only if there hasn't been any |
| 156 | + // notification since we unlocked the mutex. |
| 157 | + let r = futex_wait(&self.futex, futex_value, timeout); |
| 158 | + |
| 159 | + // Lock the mutex again. |
| 160 | + mutex.lock(); |
| 161 | + |
| 162 | + r |
| 163 | + } |
| 164 | +} |
0 commit comments