bastion_executor/
sleepers.rs1use std::sync::atomic::{AtomicBool, Ordering};
6use std::sync::{Condvar, Mutex};
7
8#[derive(Debug)]
13#[allow(clippy::mutex_atomic)]
14pub struct Sleepers {
15 sleep: Mutex<usize>,
17
18 wake: Condvar,
20
21 notified: AtomicBool,
23}
24
25#[allow(clippy::mutex_atomic)]
26impl Default for Sleepers {
27 fn default() -> Self {
29 Self {
30 sleep: Mutex::new(0),
31 wake: Condvar::new(),
32 notified: AtomicBool::new(false),
33 }
34 }
35}
36
37#[allow(clippy::mutex_atomic)]
38impl Sleepers {
39 pub fn new() -> Self {
41 Self::default()
42 }
43
44 pub fn wait(&self) {
46 let mut sleep = self.sleep.lock().unwrap();
47
48 if !self.notified.swap(false, Ordering::SeqCst) {
49 *sleep += 1;
50 std::mem::drop(self.wake.wait(sleep).unwrap());
51 }
52 }
53
54 pub fn notify_one(&self) {
56 if !self.notified.load(Ordering::SeqCst) {
57 let mut sleep = self.sleep.lock().unwrap();
58
59 if *sleep > 0 {
60 *sleep -= 1;
61 self.wake.notify_one();
62 } else {
63 self.notified.store(true, Ordering::SeqCst);
64 }
65 }
66 }
67}