Last active
August 29, 2015 14:05
-
-
Save toroidal-code/9c12d53a0f2d4e631cdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate sync; | |
use sync::mutex::{Mutex,Guard}; | |
use std::task; | |
fn lock<'a>(l0 : &'a Mutex, l1 : &'a Mutex) -> (Guard<'a>, Guard<'a>) { | |
loop { | |
't0: for _ in range(0u, 1u) { | |
let u0 = l0.lock(); | |
let u1 = match l1.try_lock() { | |
None => { | |
drop(u0); | |
break 't0; | |
} | |
Some(g) => g | |
}; | |
return (u0, u1); | |
} | |
task::deschedule(); | |
't1: for _ in range(0u, 1u) { | |
let u1 = l1.lock(); | |
let u0 = match l1.try_lock() { | |
None => { | |
drop(u1); | |
break 't1; | |
} | |
Some(g) => g | |
}; | |
return (u0, u1); | |
} | |
task::deschedule(); | |
} | |
} | |
fn main() { | |
let a = Mutex::new(); | |
let b = Mutex::new(); | |
let (g0, g1) = lock(&a, &b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment