zebra_chain/
shutdown.rs

1//! Shutdown related code.
2//!
3//! A global flag indicates when the application is shutting down so actions can be taken
4//! at different parts of the codebase.
5
6use std::sync::atomic::{AtomicBool, Ordering};
7
8/// A flag to indicate if Zebra is shutting down.
9///
10/// Initialized to `false` at startup.
11pub static IS_SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);
12
13/// Returns true if the application is shutting down.
14///
15/// Returns false otherwise.
16pub fn is_shutting_down() -> bool {
17    // ## Correctness:
18    //
19    // Since we're shutting down, and this is a one-time operation,
20    // performance is not important. So we use the strongest memory
21    // ordering.
22    // https://doc.rust-lang.org/nomicon/atomics.html#sequentially-consistent
23    IS_SHUTTING_DOWN.load(Ordering::SeqCst)
24}
25
26/// Sets the Zebra shutdown flag to `true`.
27pub fn set_shutting_down() {
28    IS_SHUTTING_DOWN.store(true, Ordering::SeqCst);
29}