Skip to main content

solana_program_runtime/
loading_task.rs

1use solana_svm_type_overrides::sync::{Condvar, Mutex};
2
3#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
4pub struct LoadingTaskCookie(u64);
5
6impl LoadingTaskCookie {
7    fn new() -> Self {
8        Self(0)
9    }
10
11    fn update(&mut self) {
12        let LoadingTaskCookie(cookie) = self;
13        *cookie = cookie.wrapping_add(1);
14    }
15}
16
17/// Suspends the thread in case no cooprative loading task was assigned
18#[derive(Debug, Default)]
19pub struct LoadingTaskWaiter {
20    cookie: Mutex<LoadingTaskCookie>,
21    cond: Condvar,
22}
23
24impl LoadingTaskWaiter {
25    pub fn new() -> Self {
26        Self {
27            cookie: Mutex::new(LoadingTaskCookie::new()),
28            cond: Condvar::new(),
29        }
30    }
31
32    pub fn cookie(&self) -> LoadingTaskCookie {
33        *self.cookie.lock().unwrap()
34    }
35
36    pub fn notify(&self) {
37        let mut cookie = self.cookie.lock().unwrap();
38        cookie.update();
39        self.cond.notify_all();
40    }
41
42    pub fn wait(&self, cookie: LoadingTaskCookie) -> LoadingTaskCookie {
43        let cookie_guard = self.cookie.lock().unwrap();
44        *self
45            .cond
46            .wait_while(cookie_guard, |current_cookie| *current_cookie == cookie)
47            .unwrap()
48    }
49}