diff --git a/src/ext/sync.rs b/src/ext/sync.rs index a6e8016..d2e8cc2 100644 --- a/src/ext/sync.rs +++ b/src/ext/sync.rs @@ -1,8 +1,120 @@ //! Sync utils use super::*; -use tokio::sync::oneshot; -use futures::prelude::*; +use std::{ + ptr::NonNull, + sync::atomic::{AtomicBool, Ordering}, +}; +use std::ops::Drop; +use crossbeam_utils::atomic::AtomicCell; +struct InitData +{ + data: AtomicCell>, + drop: AtomicBool, +} + +#[derive(Debug)] +struct InitInner +{ + boxed: NonNull>, +} + +impl InitInner +{ + #[inline] pub fn new() -> Self + { + let from = Box::new(InitData { + data: AtomicCell::new(None), + drop: false.into(), + }); + Self { + boxed: unsafe{NonNull::new_unchecked(Box::into_raw(from))}, + } + } +} + +impl Clone for InitInner +{ + fn clone(&self) -> Self { + Self { + boxed: unsafe{NonNull::new_unchecked(self.boxed.as_ptr())} + } + } +} + +impl InitInner +{ + #[inline(always)] fn should_drop(&self) -> &AtomicBool + { + &unsafe {&*self.boxed.as_ptr()}.drop + } + + #[inline(always)] fn data_ref(&self) -> &InitData + { + unsafe{&*self.boxed.as_ptr()} + } +} + +unsafe impl Send for InitInner{} +unsafe impl Sync for InitInner{} + +impl Drop for InitInner +{ + fn drop(&mut self) { + if let Err(true) = self.should_drop().compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) { + unsafe { + drop(Box::from_raw(self.boxed.as_ptr())) + } + } + } +} + +#[derive(Debug)] +pub struct InitHalf(InitInner); +#[derive(Debug)] +pub struct UninitHalf(InitInner); + +impl InitHalf +{ + /// Set the value + pub fn set(&mut self, value: T) + { + self.0.data_ref().data.store(Some(value)); + } + /// Set the value, returning the previous one if it was set + pub fn swap(&mut self, value: T) -> Option + { + self.0.data_ref().data.swap(Some(value)) + } +} + +impl UninitHalf +{ + /// Take the initialised value + /// + /// # Panics + /// If the value hasn't yet been initialised. + pub fn take(&mut self) -> T + { + self.0.data_ref().data.take().unwrap() + } + /// Take the value if it is set, if not, returns `None`. + pub fn try_take(&mut self) -> Option + { + self.0.data_ref().data.take() + } +} + +/// Create a pair of atomic initialser-receivers. +pub fn shared_uninit() -> (InitHalf, UninitHalf) +{ + let inner = InitInner::::new(); + + (InitHalf(inner.clone()), + UninitHalf(inner)) +} + +/* /// Type to allow for a seperate thread or task to initialise a value. #[derive(Debug)] pub struct SharedUninit(oneshot::Receiver); @@ -12,13 +124,13 @@ pub struct SharedUninit(oneshot::Receiver); pub struct SharedInitialiser(oneshot::Sender); impl<'a, T> SharedUninit - where T: 'a +where T: 'a { - /// Create an uninit/initialiser pair. - pub fn pair() -> (SharedInitialiser, Self) - { - let (tx, rx) = oneshot::channel(); - (SharedInitialiser(tx), Self(rx)) +/// Create an uninit/initialiser pair. +pub fn pair() -> (SharedInitialiser, Self) +{ +let (tx, rx) = oneshot::channel(); +(SharedInitialiser(tx), Self(rx)) } /// Try to receive the initialised value. @@ -48,7 +160,7 @@ impl<'a, T> SharedInitialiser let _ = self.0.send(value); } } - +*/ // This was a failure. Just use `tokio::sync::oneshot`... /*