|
|
|
@ -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<T>
|
|
|
|
|
{
|
|
|
|
|
data: AtomicCell<Option<T>>,
|
|
|
|
|
drop: AtomicBool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct InitInner<T>
|
|
|
|
|
{
|
|
|
|
|
boxed: NonNull<InitData<T>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> InitInner<T>
|
|
|
|
|
{
|
|
|
|
|
#[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<T> Clone for InitInner<T>
|
|
|
|
|
{
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
boxed: unsafe{NonNull::new_unchecked(self.boxed.as_ptr())}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> InitInner<T>
|
|
|
|
|
{
|
|
|
|
|
#[inline(always)] fn should_drop(&self) -> &AtomicBool
|
|
|
|
|
{
|
|
|
|
|
&unsafe {&*self.boxed.as_ptr()}.drop
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)] fn data_ref(&self) -> &InitData<T>
|
|
|
|
|
{
|
|
|
|
|
unsafe{&*self.boxed.as_ptr()}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<T> Send for InitInner<T>{}
|
|
|
|
|
unsafe impl<T> Sync for InitInner<T>{}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for InitInner<T>
|
|
|
|
|
{
|
|
|
|
|
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<T>(InitInner<T>);
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct UninitHalf<T>(InitInner<T>);
|
|
|
|
|
|
|
|
|
|
impl<T> InitHalf<T>
|
|
|
|
|
{
|
|
|
|
|
/// 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<T>
|
|
|
|
|
{
|
|
|
|
|
self.0.data_ref().data.swap(Some(value))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> UninitHalf<T>
|
|
|
|
|
{
|
|
|
|
|
/// 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<T>
|
|
|
|
|
{
|
|
|
|
|
self.0.data_ref().data.take()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a pair of atomic initialser-receivers.
|
|
|
|
|
pub fn shared_uninit<T>() -> (InitHalf<T>, UninitHalf<T>)
|
|
|
|
|
{
|
|
|
|
|
let inner = InitInner::<T>::new();
|
|
|
|
|
|
|
|
|
|
(InitHalf(inner.clone()),
|
|
|
|
|
UninitHalf(inner))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
/// Type to allow for a seperate thread or task to initialise a value.
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct SharedUninit<T>(oneshot::Receiver<T>);
|
|
|
|
@ -12,13 +124,13 @@ pub struct SharedUninit<T>(oneshot::Receiver<T>);
|
|
|
|
|
pub struct SharedInitialiser<T>(oneshot::Sender<T>);
|
|
|
|
|
|
|
|
|
|
impl<'a, T> SharedUninit<T>
|
|
|
|
|
where T: 'a
|
|
|
|
|
where T: 'a
|
|
|
|
|
{
|
|
|
|
|
/// Create an uninit/initialiser pair.
|
|
|
|
|
pub fn pair() -> (SharedInitialiser<T>, Self)
|
|
|
|
|
{
|
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
|
(SharedInitialiser(tx), Self(rx))
|
|
|
|
|
/// Create an uninit/initialiser pair.
|
|
|
|
|
pub fn pair() -> (SharedInitialiser<T>, Self)
|
|
|
|
|
{
|
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
|
(SharedInitialiser(tx), Self(rx))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Try to receive the initialised value.
|
|
|
|
@ -48,7 +160,7 @@ impl<'a, T> SharedInitialiser<T>
|
|
|
|
|
let _ = self.0.send(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
// This was a failure. Just use `tokio::sync::oneshot`...
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|