use std::sync::atomic::{AtomicUsize, Ordering};
const SET: usize = 1;
const UNSET: usize = 0;
#[derive(Debug)]
pub struct OnSignal {
state: AtomicUsize,
}
impl Default for OnSignal {
fn default() -> Self {
Self::new()
}
}
impl OnSignal {
#[must_use]
pub fn new() -> Self {
Self {
state: AtomicUsize::new(0),
}
}
#[inline]
pub fn turn_off(&self) -> bool {
self.state
.compare_exchange(SET, UNSET, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
}
#[inline]
pub fn turn_on(&self) -> bool {
self.state
.compare_exchange(UNSET, SET, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
}
#[inline]
pub fn probe(&self) -> bool {
self.state.load(Ordering::Acquire) == SET
}
}
const SLEEPY: usize = 2;
const SLEEPING: usize = 3;
#[derive(Debug)]
pub struct ActivitySignal {
state: AtomicUsize,
}
impl Default for ActivitySignal {
fn default() -> Self {
Self::new()
}
}
impl ActivitySignal {
#[must_use]
pub fn new() -> Self {
Self {
state: AtomicUsize::new(0),
}
}
#[inline]
pub fn wake_up(&self) -> bool {
self.state
.compare_exchange(SET, UNSET, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
}
#[inline]
pub fn make_asleep(&self) -> bool {
self.state
.compare_exchange(SLEEPY, SLEEPING, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
}
#[inline]
pub fn make_sleepy(&self) -> bool {
self.state
.compare_exchange(UNSET, SLEEPY, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
}
#[inline]
pub fn become_paused(&self) -> bool {
self.state
.compare_exchange(SLEEPING, SET, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
}
#[inline]
pub fn probe_sleepy(&self) -> bool {
self.state.load(Ordering::Acquire) == SLEEPY
}
#[inline]
pub fn probe_asleep(&self) -> bool {
self.state.load(Ordering::Acquire) == SLEEPING
}
#[inline]
pub fn probe_paused(&self) -> bool {
self.state.load(Ordering::Acquire) == SET
}
}