use std::fmt;
use std::sync::{Arc, Condvar, Mutex};
#[derive(Clone)]
pub struct WaitGroup(Arc<WaitGroupInner>);
struct WaitGroupInner {
cond: Condvar,
count: Mutex<i32>,
}
impl WaitGroup {
pub fn new() -> WaitGroup {
WaitGroup(Arc::new(WaitGroupInner {
cond: Condvar::new(),
count: Mutex::new(0),
}))
}
pub fn add(&self, delta: i32) {
let mut count = self.0.count.lock().unwrap();
*count += delta;
assert!(*count >= 0);
self.0.cond.notify_all();
}
pub fn done(&self) {
self.add(-1);
}
pub fn wait(&self) {
let mut count = self.0.count.lock().unwrap();
while *count > 0 {
count = self.0.cond.wait(count).unwrap();
}
}
}
impl fmt::Debug for WaitGroup {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let count = self.0.count.lock().unwrap();
write!(f, "WaitGroup {{ count: {:?} }}", *count)
}
}