Function cooptex::retry_loop [−][src]
Expand description
Helper function for implementing the behavior of dropping held MutexGuard
s when a
CoopMutex::lock
call returns Retry
.
You should use the early return operator ?
to raise any Retry
errors. While the
std::ops::Try
trait is unstable, we can’t allow using the early return operator for
returning as normal user code would. We recommend having one function that acquires all
relevant locks, and another that uses them.
use cooptex::*;
let a = CoopMutex::new(42);
let b = CoopMutex::new(43);
fn use_locks(a: &mut usize, b: &mut usize) -> Result<usize, ()> {
*a += 1;
*b += 1;
Ok(*a + *b)
}
let result = retry_loop(|| {
let mut a_lock = a.lock()?.unwrap();
let mut b_lock = b.lock()?.unwrap();
Ok(use_locks(&mut a_lock, &mut b_lock))
});
assert_eq!(result, Ok(87));