|
| 1 | +use crate::os::xous::ffi::{blocking_scalar, do_yield, scalar}; |
| 2 | +use crate::os::xous::services::ticktimer_server; |
| 3 | +use crate::sync::atomic::{AtomicUsize, 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 | + |
| 19 | +pub type MovableMutex = Mutex; |
| 20 | + |
| 21 | +impl Mutex { |
| 22 | + #[inline] |
| 23 | + #[rustc_const_stable(feature = "const_locks", since = "1.63.0")] |
| 24 | + pub const fn new() -> Mutex { |
| 25 | + Mutex { locked: AtomicUsize::new(0) } |
| 26 | + } |
| 27 | + |
| 28 | + #[inline] |
| 29 | + pub unsafe fn lock(&self) { |
| 30 | + // Try multiple times to acquire the lock without resorting to the ticktimer |
| 31 | + // server. For locks that are held for a short amount of time, this will |
| 32 | + // result in the ticktimer server never getting invoked. The `locked` value |
| 33 | + // will be either 0 or 1. |
| 34 | + for _attempts in 0..3 { |
| 35 | + if unsafe { self.try_lock() } { |
| 36 | + return; |
| 37 | + } |
| 38 | + do_yield(); |
| 39 | + } |
| 40 | + |
| 41 | + // Try one more time to lock. If the lock is released between the previous code and |
| 42 | + // here, then the inner `locked` value will be 1 at the end of this. If it was not |
| 43 | + // locked, then the value will be more than 1, for example if there are multiple other |
| 44 | + // threads waiting on this lock. |
| 45 | + if unsafe { self.try_lock_or_poison() } { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // The lock is now "contended". When the lock is released, a Message will get sent to the |
| 50 | + // ticktimer server to wake it up. Note that this may already have happened, so the actual |
| 51 | + // value of `lock` may be anything (0, 1, 2, ...). |
| 52 | + blocking_scalar( |
| 53 | + ticktimer_server(), |
| 54 | + [6 /* LockMutex */, self as *const Mutex as usize, 0, 0, 0], |
| 55 | + ) |
| 56 | + .expect("failure to send LockMutex command"); |
| 57 | + } |
| 58 | + |
| 59 | + #[inline] |
| 60 | + pub unsafe fn unlock(&self) { |
| 61 | + let prev = self.locked.fetch_sub(1, SeqCst); |
| 62 | + |
| 63 | + // If the previous value was 1, then this was a "fast path" unlock, so no |
| 64 | + // need to involve the Ticktimer server |
| 65 | + if prev == 1 { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // If it was 0, then something has gone seriously wrong and the counter |
| 70 | + // has just wrapped around. |
| 71 | + if prev == 0 { |
| 72 | + panic!("mutex lock count underflowed"); |
| 73 | + } |
| 74 | + |
| 75 | + // Unblock one thread that is waiting on this message. |
| 76 | + scalar(ticktimer_server(), [7 /* UnlockMutex */, self as *const Mutex as usize, 0, 0, 0]) |
| 77 | + .expect("failure to send UnlockMutex command"); |
| 78 | + } |
| 79 | + |
| 80 | + #[inline] |
| 81 | + pub unsafe fn try_lock(&self) -> bool { |
| 82 | + self.locked.compare_exchange(0, 1, SeqCst, SeqCst).is_ok() |
| 83 | + } |
| 84 | + |
| 85 | + #[inline] |
| 86 | + pub unsafe fn try_lock_or_poison(&self) -> bool { |
| 87 | + self.locked.fetch_add(1, SeqCst) == 0 |
| 88 | + } |
| 89 | +} |
0 commit comments