|
|
@ -96,11 +96,17 @@ impl<T, S: MutexSupport> Mutex<T, S>
|
|
|
|
impl<T: ?Sized, S: MutexSupport> Mutex<T, S>
|
|
|
|
impl<T: ?Sized, S: MutexSupport> Mutex<T, S>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fn obtain_lock(&self) {
|
|
|
|
fn obtain_lock(&self) {
|
|
|
|
while self.lock.compare_and_swap(false, true, Ordering::Acquire) != false {
|
|
|
|
while true {
|
|
|
|
|
|
|
|
match self.lock.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
|
|
|
|
|
|
|
|
Ok(X) => break,
|
|
|
|
|
|
|
|
Err(X) => {
|
|
|
|
// Wait until the lock looks unlocked before retrying
|
|
|
|
// Wait until the lock looks unlocked before retrying
|
|
|
|
while self.lock.load(Ordering::Relaxed) {
|
|
|
|
while self.lock.load(Ordering::Relaxed) {
|
|
|
|
self.support.cpu_relax();
|
|
|
|
self.support.cpu_relax();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -144,13 +150,13 @@ impl<T: ?Sized, S: MutexSupport> Mutex<T, S>
|
|
|
|
/// a guard within Some.
|
|
|
|
/// a guard within Some.
|
|
|
|
pub fn try_lock(&self) -> Option<MutexGuard<T, S>> {
|
|
|
|
pub fn try_lock(&self) -> Option<MutexGuard<T, S>> {
|
|
|
|
let support_guard = S::before_lock();
|
|
|
|
let support_guard = S::before_lock();
|
|
|
|
if self.lock.compare_and_swap(false, true, Ordering::Acquire) == false {
|
|
|
|
match self.lock.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
|
|
|
|
|
|
|
|
Ok(X) =>
|
|
|
|
Some(MutexGuard {
|
|
|
|
Some(MutexGuard {
|
|
|
|
mutex: self,
|
|
|
|
mutex: self,
|
|
|
|
support_guard,
|
|
|
|
support_guard,
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
} else {
|
|
|
|
Err(X) => None,
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|