photon_ring/shutdown.rs
1// Copyright 2026 Photon Ring Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! A shared shutdown signal for coordinating graceful termination of
5//! consumer loops.
6
7use alloc::sync::Arc;
8use core::sync::atomic::{AtomicBool, Ordering};
9
10/// A shared shutdown signal for coordinating graceful termination.
11///
12/// ```
13/// use photon_ring::Shutdown;
14///
15/// let shutdown = Shutdown::new();
16/// let flag = shutdown.clone();
17///
18/// // In consumer thread:
19/// // while !flag.is_shutdown() {
20/// // match sub.try_recv() {
21/// // Ok(v) => { /* process */ }
22/// // Err(_) => core::hint::spin_loop(),
23/// // }
24/// // }
25///
26/// // In main thread:
27/// shutdown.trigger();
28/// assert!(flag.is_shutdown());
29/// ```
30#[derive(Clone)]
31pub struct Shutdown {
32 flag: Arc<AtomicBool>,
33}
34
35impl Shutdown {
36 /// Create a new shutdown signal (not yet triggered).
37 pub fn new() -> Self {
38 Shutdown {
39 flag: Arc::new(AtomicBool::new(false)),
40 }
41 }
42
43 /// Trigger the shutdown signal. All clones will observe `is_shutdown() == true`.
44 pub fn trigger(&self) {
45 self.flag.store(true, Ordering::Release);
46 }
47
48 /// Returns `true` if [`trigger`](Self::trigger) has been called on any clone.
49 pub fn is_shutdown(&self) -> bool {
50 self.flag.load(Ordering::Acquire)
51 }
52}
53
54impl Default for Shutdown {
55 fn default() -> Self {
56 Self::new()
57 }
58}