static ORDER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
fn next() -> u64 {
ORDER.fetch_add(1, std::sync::atomic::Ordering::AcqRel)
}
#[derive(Clone, Debug)]
pub struct ExecutionLease {
pub(super) id: uuid::Uuid,
live: std::sync::Arc<std::sync::atomic::AtomicBool>,
minted: u64,
closed: std::sync::Arc<std::sync::atomic::AtomicU64>,
}
impl PartialEq for ExecutionLease {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for ExecutionLease {}
impl std::hash::Hash for ExecutionLease {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl ExecutionLease {
#[must_use]
pub fn mint() -> Self {
Self {
id: uuid::Uuid::new_v4(),
live: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)),
minted: next(),
closed: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0)),
}
}
#[must_use]
pub fn is_live(&self) -> bool {
self.live.load(std::sync::atomic::Ordering::Acquire)
}
pub fn close(&self) {
let _ = self.closed.compare_exchange(
0,
next(),
std::sync::atomic::Ordering::AcqRel,
std::sync::atomic::Ordering::Acquire,
);
self.live.store(false, std::sync::atomic::Ordering::Release);
}
#[must_use]
pub fn replaced(&self, ended: &Self) -> bool {
let closed = ended.closed.load(std::sync::atomic::Ordering::Acquire);
closed != 0 && self.is_live() && self.minted > closed
}
}