#![allow(dead_code)]
use std::sync::Condvar;
use std::sync::Mutex;
use std::thread;
pub struct TestSync {
mutex: Mutex<u32>,
condvar: Condvar,
}
impl TestSync {
pub fn new() -> TestSync {
TestSync {
mutex: Mutex::new(0),
condvar: Condvar::new(),
}
}
pub fn take(&self, wait_for: u32) {
let mut guard = self.mutex.lock().expect("mutex poisoned");
loop {
if *guard == wait_for {
trace!(
"take {} from thread {}",
wait_for,
thread::current().name().unwrap_or("?")
);
*guard += 1;
self.condvar.notify_all();
return;
}
assert!(
wait_for > *guard,
"wait_for = {}, *guard = {}",
wait_for,
*guard
);
guard = self.condvar.wait(guard).expect("wait");
}
}
}