use std::{cell::UnsafeCell, fmt};
pub(crate) struct State(UnsafeCell<usize>);
#[derive(Copy, Clone)]
pub(crate) struct Snapshot(usize);
type UpdateResult = Result<Snapshot, Snapshot>;
const RUNNING: usize = 0b0001;
const COMPLETE: usize = 0b0010;
const LIFECYCLE_MASK: usize = 0b11;
const NOTIFIED: usize = 0b100;
#[allow(clippy::unusual_byte_groupings)] const JOIN_INTEREST: usize = 0b1_000;
#[allow(clippy::unusual_byte_groupings)] const JOIN_WAKER: usize = 0b10_000;
const STATE_MASK: usize = LIFECYCLE_MASK | NOTIFIED | JOIN_INTEREST | JOIN_WAKER;
const REF_COUNT_MASK: usize = !STATE_MASK;
const REF_COUNT_SHIFT: usize = REF_COUNT_MASK.count_zeros() as usize;
const REF_ONE: usize = 1 << REF_COUNT_SHIFT;
const INITIAL_STATE: usize = (REF_ONE * 2) | JOIN_INTEREST | NOTIFIED;
#[must_use]
pub(super) enum TransitionToIdle {
Ok,
OkNotified,
}
#[must_use]
pub(super) enum TransitionToNotified {
DoNothing,
Submit,
}
impl State {
pub(crate) fn new() -> Self {
State(UnsafeCell::new(INITIAL_STATE))
}
pub(crate) fn load(&self) -> Snapshot {
Snapshot(unsafe { *self.0.get() })
}
pub(crate) fn store(&self, val: Snapshot) {
unsafe {
*self.0.get() = *val;
}
}
pub(super) fn transition_to_running(&self) {
let mut snapshot = self.load();
debug_assert!(snapshot.is_notified());
debug_assert!(snapshot.is_idle());
snapshot.set_running();
snapshot.unset_notified();
self.store(snapshot);
}
pub(super) fn transition_to_idle(&self) -> TransitionToIdle {
let mut snapshot = self.load();
debug_assert!(snapshot.is_running());
snapshot.unset_running();
let action = if snapshot.is_notified() {
TransitionToIdle::OkNotified
} else {
TransitionToIdle::Ok
};
self.store(snapshot);
action
}
pub(super) fn transition_to_complete(&self) -> Snapshot {
const DELTA: usize = RUNNING | COMPLETE;
let mut val = self.load();
debug_assert!(val.is_running());
debug_assert!(!val.is_complete());
val.xor(DELTA);
self.store(val);
val
}
pub(super) fn transition_to_notified(&self) -> TransitionToNotified {
let mut snapshot = self.load();
let action = if snapshot.is_running() {
snapshot.set_notified();
TransitionToNotified::DoNothing
} else if snapshot.is_complete() || snapshot.is_notified() {
TransitionToNotified::DoNothing
} else {
snapshot.set_notified();
TransitionToNotified::Submit
};
self.store(snapshot);
action
}
pub(super) fn drop_join_handle_fast(&self) -> Result<(), ()> {
if *self.load() == INITIAL_STATE {
self.store(Snapshot((INITIAL_STATE - REF_ONE) & !JOIN_INTEREST));
trace!("MONOIO DEBUG[State]: drop_join_handle_fast");
Ok(())
} else {
Err(())
}
}
pub(super) fn unset_join_interested(&self) -> UpdateResult {
self.fetch_update(|curr| {
assert!(curr.is_join_interested());
if curr.is_complete() {
return None;
}
let mut next = curr;
next.unset_join_interested();
Some(next)
})
}
pub(super) fn set_join_waker(&self) -> UpdateResult {
self.fetch_update(|curr| {
assert!(curr.is_join_interested());
assert!(!curr.has_join_waker());
if curr.is_complete() {
return None;
}
let mut next = curr;
next.set_join_waker();
Some(next)
})
}
pub(super) fn unset_waker(&self) -> UpdateResult {
self.fetch_update(|curr| {
assert!(curr.is_join_interested());
assert!(curr.has_join_waker());
if curr.is_complete() {
return None;
}
let mut next = curr;
next.unset_join_waker();
Some(next)
})
}
pub(crate) fn ref_inc(&self) {
use std::process;
let mut val = self.load();
let prev = *val;
val.add(REF_ONE);
self.store(val);
trace!(
"MONOIO DEBUG[State]: ref_inc {}, ptr: {:p}",
val.ref_count(),
self
);
if prev > isize::MAX as usize {
process::abort();
}
}
pub(crate) fn ref_dec(&self) -> bool {
let mut val = self.load();
debug_assert!(val.ref_count() >= 1);
val.sub(REF_ONE);
self.store(val);
trace!(
"MONOIO DEBUG[State]: ref_dec {}, ptr: {:p}",
val.ref_count(),
self
);
val.ref_count() == 0
}
fn fetch_update<F>(&self, mut f: F) -> Result<Snapshot, Snapshot>
where
F: FnMut(Snapshot) -> Option<Snapshot>,
{
let curr = self.load();
let next = f(curr);
let next = match next {
Some(next) => next,
None => return Err(curr),
};
self.store(next);
Ok(next)
}
}
impl std::ops::Deref for Snapshot {
type Target = usize;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Snapshot {
pub(super) fn is_idle(self) -> bool {
self.0 & (RUNNING | COMPLETE) == 0
}
pub(super) fn is_notified(self) -> bool {
self.0 & NOTIFIED == NOTIFIED
}
fn unset_notified(&mut self) {
self.0 &= !NOTIFIED
}
fn set_notified(&mut self) {
self.0 |= NOTIFIED
}
pub(super) fn is_running(self) -> bool {
self.0 & RUNNING == RUNNING
}
fn set_running(&mut self) {
self.0 |= RUNNING;
}
fn unset_running(&mut self) {
self.0 &= !RUNNING;
}
pub(super) fn is_complete(self) -> bool {
self.0 & COMPLETE == COMPLETE
}
pub(super) fn is_join_interested(self) -> bool {
self.0 & JOIN_INTEREST == JOIN_INTEREST
}
fn unset_join_interested(&mut self) {
self.0 &= !JOIN_INTEREST
}
pub(super) fn has_join_waker(self) -> bool {
self.0 & JOIN_WAKER == JOIN_WAKER
}
fn set_join_waker(&mut self) {
self.0 |= JOIN_WAKER;
}
fn unset_join_waker(&mut self) {
self.0 &= !JOIN_WAKER
}
pub(super) fn ref_count(self) -> usize {
(self.0 & REF_COUNT_MASK) >> REF_COUNT_SHIFT
}
}
impl Snapshot {
fn xor(&mut self, other: usize) {
self.0 ^= other;
}
fn add(&mut self, other: usize) {
self.0 += other
}
fn sub(&mut self, other: usize) {
self.0 -= other
}
}
impl fmt::Debug for State {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let snapshot = self.load();
snapshot.fmt(fmt)
}
}
impl fmt::Debug for Snapshot {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Snapshot")
.field("is_running", &self.is_running())
.field("is_complete", &self.is_complete())
.field("is_notified", &self.is_notified())
.field("is_join_interested", &self.is_join_interested())
.field("has_join_waker", &self.has_join_waker())
.field("ref_count", &self.ref_count())
.finish()
}
}