use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone, Default)]
pub(super) struct StreamCaptureCancellation {
cancelled: Arc<AtomicBool>,
stop: Arc<AtomicBool>,
}
impl StreamCaptureCancellation {
pub(super) fn cancel(&self) {
self.cancelled.store(true, Ordering::Release);
self.stop.store(true, Ordering::Release);
}
pub(super) fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::Acquire)
}
#[cfg_attr(not(feature = "stream-broadcast"), allow(dead_code))]
pub(super) fn flag(&self) -> Arc<AtomicBool> {
Arc::clone(&self.stop)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn worker_stop_does_not_mark_preparation_as_externally_cancelled() {
let cancellation = StreamCaptureCancellation::default();
cancellation.flag().store(true, Ordering::Release);
assert!(!cancellation.is_cancelled());
}
#[test]
fn external_cancellation_also_stops_the_capture_worker() {
let cancellation = StreamCaptureCancellation::default();
let stop = cancellation.flag();
cancellation.cancel();
assert!(cancellation.is_cancelled());
assert!(stop.load(Ordering::Acquire));
}
}