Function cooptex::retry_loop[][src]

pub fn retry_loop<'m, T, F: FnMut() -> Result<T, Retry<'m>>>(f: F) -> T
Expand description

Helper function for implementing the behavior of dropping held MutexGuards 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.

fn use_locks(a: &mut usize, b: &mut usize) -> Result<usize, ()> {
  *a += 1;
  *b += 1;
  Ok(*a + *b)

use cooptex::*;
let a = CoopMutex::new(42);
let b = CoopMutex::new(43);

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));