File size: 717 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

/// Structure that ensures that `is_stopped` flag is set to `true` when dropped.
pub struct StoppingGuard {
    is_stopped: Arc<AtomicBool>,
}

impl StoppingGuard {
    /// Creates a new `StopGuard` instance.
    pub fn new() -> Self {
        Self {
            is_stopped: Arc::new(AtomicBool::new(false)),
        }
    }

    pub fn get_is_stopped(&self) -> Arc<AtomicBool> {
        self.is_stopped.clone()
    }
}

impl Default for StoppingGuard {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for StoppingGuard {
    fn drop(&mut self) {
        self.is_stopped
            .store(true, std::sync::atomic::Ordering::Relaxed);
    }
}