chromedriver_api/
manager.rs

1use crate::prelude::*;
2
3/// The tasks manager
4#[derive(Debug, Clone)]
5pub struct TaskManager {
6    state: Arc<Mutex<bool>>, // true — занят, false — свободен
7    notify: Arc<Notify>,
8}
9
10impl TaskManager {
11    /// Creates a new task manager
12    pub fn new() -> Self {
13        Self {
14            state: Arc::new(Mutex::new(false)),
15            notify: Arc::new(Notify::new()),
16        }
17    }
18
19    /// Locking tasks execxuting
20    pub async fn lock(&self) {
21        loop {
22            {
23                let mut locked = self.state.lock().await;
24                if !*locked {
25                    *locked = true;
26                    return;
27                }
28            }
29
30            self.notify.notified().await;
31        }
32    }
33
34    /// Unlocking tasks execxuting
35    pub async fn unlock(&self) {
36        {
37            let mut locked = self.state.lock().await;
38            *locked = false;
39        }
40
41        self.notify.notify_waiters();
42    }
43}