use solana_svm_type_overrides::sync::{Condvar, Mutex};
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct LoadingTaskCookie(u64);
impl LoadingTaskCookie {
fn new() -> Self {
Self(0)
}
fn update(&mut self) {
let LoadingTaskCookie(cookie) = self;
*cookie = cookie.wrapping_add(1);
}
}
#[derive(Debug, Default)]
pub struct LoadingTaskWaiter {
cookie: Mutex<LoadingTaskCookie>,
cond: Condvar,
}
impl LoadingTaskWaiter {
pub fn new() -> Self {
Self {
cookie: Mutex::new(LoadingTaskCookie::new()),
cond: Condvar::new(),
}
}
pub fn cookie(&self) -> LoadingTaskCookie {
*self.cookie.lock().unwrap()
}
pub fn notify(&self) {
let mut cookie = self.cookie.lock().unwrap();
cookie.update();
self.cond.notify_all();
}
pub fn wait(&self, cookie: LoadingTaskCookie) -> LoadingTaskCookie {
let cookie_guard = self.cookie.lock().unwrap();
*self
.cond
.wait_while(cookie_guard, |current_cookie| *current_cookie == cookie)
.unwrap()
}
}