|
| 1 | +use crate::os::xous::ffi::{blocking_scalar, do_yield, scalar}; |
| 2 | +use crate::os::xous::services::ticktimer_server; |
| 3 | +use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed, Ordering::SeqCst}; |
| 4 | + |
| 5 | +pub struct Mutex { |
| 6 | + /// The "locked" value indicates how many threads are waiting on this |
| 7 | + /// Mutex. Possible values are: |
| 8 | + /// 0: The lock is unlocked |
| 9 | + /// 1: The lock is locked and uncontended |
| 10 | + /// >=2: The lock is locked and contended |
| 11 | + /// |
| 12 | + /// A lock is "contended" when there is more than one thread waiting |
| 13 | + /// for a lock, or it is locked for long periods of time. Rather than |
| 14 | + /// spinning, these locks send a Message to the ticktimer server |
| 15 | + /// requesting that they be woken up when a lock is unlocked. |
| 16 | + locked: AtomicUsize, |
| 17 | + |
| 18 | + /// Whether this Mutex ever was contended, and therefore made a trip |
| 19 | + /// to the ticktimer server. If this was never set, then we were never |
| 20 | + /// on the slow path and can skip deregistering the mutex. |
| 21 | + contended: AtomicBool, |
| 22 | +} |
| 23 | + |
| 24 | +impl Mutex { |
| 25 | + #[inline] |
| 26 | + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] |
| 27 | + pub const fn new() -> Mutex { |
| 28 | + Mutex { locked: AtomicUsize::new(0), contended: AtomicBool::new(false) } |
| 29 | + } |
| 30 | + |
| 31 | + fn index(&self) -> usize { |
| 32 | + self as *const Mutex as usize |
| 33 | + } |
| 34 | + |
| 35 | + #[inline] |
| 36 | + pub unsafe fn lock(&self) { |
| 37 | + // Try multiple times to acquire the lock without resorting to the ticktimer |
| 38 | + // server. For locks that are held for a short amount of time, this will |
| 39 | + // result in the ticktimer server never getting invoked. The `locked` value |
| 40 | + // will be either 0 or 1. |
| 41 | + for _attempts in 0..3 { |
| 42 | + if unsafe { self.try_lock() } { |
| 43 | + return; |
| 44 | + } |
| 45 | + do_yield(); |
| 46 | + } |
| 47 | + |
| 48 | + // Try one more time to lock. If the lock is released between the previous code and |
| 49 | + // here, then the inner `locked` value will be 1 at the end of this. If it was not |
| 50 | + // locked, then the value will be more than 1, for example if there are multiple other |
| 51 | + // threads waiting on this lock. |
| 52 | + if unsafe { self.try_lock_or_poison() } { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // When this mutex is dropped, we will need to deregister it with the server. |
| 57 | + self.contended.store(true, Relaxed); |
| 58 | + |
| 59 | + // The lock is now "contended". When the lock is released, a Message will get sent to the |
| 60 | + // ticktimer server to wake it up. Note that this may already have happened, so the actual |
| 61 | + // value of `lock` may be anything (0, 1, 2, ...). |
| 62 | + blocking_scalar( |
| 63 | + ticktimer_server(), |
| 64 | + crate::os::xous::services::TicktimerScalar::LockMutex(self.index()).into(), |
| 65 | + ) |
| 66 | + .expect("failure to send LockMutex command"); |
| 67 | + } |
| 68 | + |
| 69 | + #[inline] |
| 70 | + pub unsafe fn unlock(&self) { |
| 71 | + let prev = self.locked.fetch_sub(1, SeqCst); |
| 72 | + |
| 73 | + // If the previous value was 1, then this was a "fast path" unlock, so no |
| 74 | + // need to involve the Ticktimer server |
| 75 | + if prev == 1 { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // If it was 0, then something has gone seriously wrong and the counter |
| 80 | + // has just wrapped around. |
| 81 | + if prev == 0 { |
| 82 | + panic!("mutex lock count underflowed"); |
| 83 | + } |
| 84 | + |
| 85 | + // Unblock one thread that is waiting on this message. |
| 86 | + scalar( |
| 87 | + ticktimer_server(), |
| 88 | + crate::os::xous::services::TicktimerScalar::UnlockMutex(self.index()).into(), |
| 89 | + ) |
| 90 | + .expect("failure to send UnlockMutex command"); |
| 91 | + } |
| 92 | + |
| 93 | + #[inline] |
| 94 | + pub unsafe fn try_lock(&self) -> bool { |
| 95 | + self.locked.compare_exchange(0, 1, SeqCst, SeqCst).is_ok() |
| 96 | + } |
| 97 | + |
| 98 | + #[inline] |
| 99 | + pub unsafe fn try_lock_or_poison(&self) -> bool { |
| 100 | + self.locked.fetch_add(1, SeqCst) == 0 |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl Drop for Mutex { |
| 105 | + fn drop(&mut self) { |
| 106 | + // If there was Mutex contention, then we involved the ticktimer. Free |
| 107 | + // the resources associated with this Mutex as it is deallocated. |
| 108 | + if self.contended.load(Relaxed) { |
| 109 | + scalar( |
| 110 | + ticktimer_server(), |
| 111 | + crate::os::xous::services::TicktimerScalar::FreeMutex(self.index()).into(), |
| 112 | + ) |
| 113 | + .ok(); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments