Files
khadhroony-solana-project/crates/ksp-worker-api/src/stop.rs

45 lines
1.3 KiB
Rust

// file: crates/ksp-worker-api/src/stop.rs
// version: 2
/// Runtime-neutral cloneable token carrying cooperative Worker stop intent.
#[derive(Clone)]
pub struct WorkerStopToken {
requested: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
impl crate::WorkerStopToken {
/// Creates a token with no stop request.
#[must_use]
pub fn new() -> Self {
return Self { requested: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)) };
}
/// Requests stop and returns `true` only for the first request shared by all clones.
#[must_use]
pub fn request_stop(&self) -> bool {
return !self.requested.swap(true, std::sync::atomic::Ordering::AcqRel);
}
/// Reports whether stop has been requested through any clone.
#[must_use]
pub fn is_stop_requested(&self) -> bool {
return self.requested.load(std::sync::atomic::Ordering::Acquire);
}
}
impl std::default::Default for crate::WorkerStopToken {
fn default() -> Self {
return Self::new();
}
}
impl std::fmt::Debug for crate::WorkerStopToken {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
return formatter.debug_struct("WorkerStopToken").field("stop_requested", &self.is_stop_requested()).finish();
}
}
#[cfg(test)]
#[path = "../unit_tests/stop.rs"]
mod tests;