use std::fmt;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
use crate::table::{Slot, Table};
use crate::{OrbitalId, Primitive, Result};
pub struct Orbital<T: Primitive> {
slot: NonNull<Slot>,
id: OrbitalId,
table: Arc<Table>,
_type: PhantomData<T>,
}
unsafe impl<T: Primitive> Send for Orbital<T> {}
unsafe impl<T: Primitive> Sync for Orbital<T> {}
impl<T: Primitive> Clone for Orbital<T> {
fn clone(&self) -> Self {
Self {
slot: self.slot,
id: self.id,
table: Arc::clone(&self.table),
_type: PhantomData,
}
}
}
impl<T: Primitive> fmt::Debug for Orbital<T> {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
f.debug_tuple("Orbital").field(&self.id).finish()
}
}
impl<T: Primitive> Orbital<T> {
pub(crate) fn new(
slot: NonNull<Slot>,
id: OrbitalId,
table: Arc<Table>,
) -> Self {
Self { slot, id, table, _type: PhantomData }
}
#[inline]
fn slot(&self) -> &Slot {
unsafe { self.slot.as_ref() }
}
#[inline]
fn write<R>(
&self,
operation: impl FnOnce(&AtomicU64) -> R,
) -> R {
let slot = self.slot();
let result = operation(&slot.value);
slot.wake.changed();
result
}
pub fn id(&self) -> OrbitalId {
self.id
}
#[inline]
pub fn load(&self) -> T {
T::from_word(self.slot().value.load(Ordering::Acquire))
}
#[inline]
pub fn store(
&self,
value: T,
) {
self.write(|word| word.store(value.into_word(), Ordering::SeqCst));
}
#[inline]
pub fn swap(
&self,
value: T,
) -> T {
T::from_word(
self.write(|word| word.swap(value.into_word(), Ordering::SeqCst)),
)
}
#[inline]
pub fn compare_exchange(
&self,
current: T,
new: T,
) -> Result<T, T> {
let slot = self.slot();
let exchanged = slot.value.compare_exchange(
current.into_word(),
new.into_word(),
Ordering::SeqCst,
Ordering::SeqCst,
);
if exchanged.is_ok() {
slot.wake.changed();
}
exchanged.map(T::from_word).map_err(T::from_word)
}
pub fn wait(
&self,
current: T,
) -> Result<T> {
let slot = self.slot();
let current = current.into_word();
loop {
let now = slot.value.load(Ordering::SeqCst);
if now != current {
return Ok(T::from_word(now));
}
slot.wake
.wait_if(|| slot.value.load(Ordering::SeqCst) == current)?;
}
}
pub fn wait_until(
&self,
current: T,
interrupt: &AtomicBool,
) -> Result<Option<T>> {
let slot = self.slot();
let current = current.into_word();
loop {
if interrupt.load(Ordering::SeqCst) {
return Ok(None);
}
let now = slot.value.load(Ordering::SeqCst);
if now != current {
return Ok(Some(T::from_word(now)));
}
slot.wake.wait_if(|| {
slot.value.load(Ordering::SeqCst) == current
&& !interrupt.load(Ordering::SeqCst)
})?;
}
}
pub fn notify(&self) {
self.slot().wake.notify();
}
}
macro_rules! integer {
($type:ty, $atomic:ty) => {
impl Orbital<$type> {
#[inline]
fn integer<R>(
&self,
operation: impl FnOnce(&$atomic) -> R,
) -> R {
self.write(|word| unsafe {
operation(<$atomic>::from_ptr(word.as_ptr().cast()))
})
}
#[inline]
pub fn fetch_add(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_add(value, Ordering::SeqCst))
}
#[inline]
pub fn fetch_sub(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_sub(value, Ordering::SeqCst))
}
#[inline]
pub fn fetch_and(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_and(value, Ordering::SeqCst))
}
#[inline]
pub fn fetch_or(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_or(value, Ordering::SeqCst))
}
#[inline]
pub fn fetch_xor(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_xor(value, Ordering::SeqCst))
}
#[inline]
pub fn fetch_max(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_max(value, Ordering::SeqCst))
}
#[inline]
pub fn fetch_min(
&self,
value: $type,
) -> $type {
self.integer(|word| word.fetch_min(value, Ordering::SeqCst))
}
}
};
}
integer!(i64, AtomicI64);
integer!(u64, AtomicU64);
impl Orbital<f64> {
pub fn fetch_add(
&self,
value: f64,
) -> f64 {
f64::from_bits(self.write(|word| {
word.update(Ordering::SeqCst, Ordering::SeqCst, |bits| {
(f64::from_bits(bits) + value).to_bits()
})
}))
}
}
impl Orbital<bool> {
#[inline]
pub fn fetch_and(
&self,
value: bool,
) -> bool {
self.write(|word| word.fetch_and(u64::from(value), Ordering::SeqCst))
!= 0
}
#[inline]
pub fn fetch_or(
&self,
value: bool,
) -> bool {
self.write(|word| word.fetch_or(u64::from(value), Ordering::SeqCst))
!= 0
}
#[inline]
pub fn fetch_xor(
&self,
value: bool,
) -> bool {
self.write(|word| word.fetch_xor(u64::from(value), Ordering::SeqCst))
!= 0
}
}