Add misc bounds and impls for `TMCell`

next
Sayan Nandan 2 years ago
parent 548ed53e16
commit 6fcf87c040
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -27,9 +27,7 @@
use core::{fmt, mem, ops::Deref, sync::atomic::Ordering}; use core::{fmt, mem, ops::Deref, sync::atomic::Ordering};
use crossbeam_epoch::{Atomic as CBAtomic, CompareExchangeError, Pointable, Pointer}; use crossbeam_epoch::{Atomic as CBAtomic, CompareExchangeError, Pointable, Pointer};
// re-export here because we have some future plans ;) (@ohsayan) // re-export here because we have some future plans ;) (@ohsayan)
pub use crossbeam_epoch::{ pub use crossbeam_epoch::{pin as cpin, unprotected as upin, Guard, Owned, Shared};
pin as pin_current, unprotected as pin_unprotected, Guard, Owned, Shared,
};
pub(super) const ORD_RLX: Ordering = Ordering::Relaxed; pub(super) const ORD_RLX: Ordering = Ordering::Relaxed;
pub(super) const ORD_ACQ: Ordering = Ordering::Acquire; pub(super) const ORD_ACQ: Ordering = Ordering::Acquire;

@ -24,12 +24,13 @@
* *
*/ */
use super::atm::{pin_unprotected, Atomic, Guard, Owned, Shared, ORD_REL}; use super::atm::{upin, Atomic, Guard, Owned, Shared, ORD_REL};
use core::ops::Deref; use core::ops::Deref;
use parking_lot::{Mutex, MutexGuard}; use parking_lot::{Mutex, MutexGuard};
use std::marker::PhantomData; use std::marker::PhantomData;
/// A [`TMCell`] provides atomic reads and serialized writes; the `static` is a CB hack /// A [`TMCell`] provides atomic reads and serialized writes; the `static` is a CB hack
#[derive(Debug)]
pub struct TMCell<T: 'static> { pub struct TMCell<T: 'static> {
a: Atomic<T>, a: Atomic<T>,
g: Mutex<()>, g: Mutex<()>,
@ -65,13 +66,17 @@ impl<T> Drop for TMCell<T> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
// UNSAFE(@ohsayan): Sole owner with mutable access // UNSAFE(@ohsayan): Sole owner with mutable access
let g = pin_unprotected(); let g = upin();
let shptr = self.a.ld_rlx(&g); let shptr = self.a.ld_rlx(&g);
g.defer_destroy(shptr); g.defer_destroy(shptr);
} }
} }
} }
unsafe impl<T: Send> Send for TMCell<T> {}
unsafe impl<T: Sync> Sync for TMCell<T> {}
#[derive(Debug)]
pub struct TMCellReadTxn<'a, 'g, T: 'static> { pub struct TMCellReadTxn<'a, 'g, T: 'static> {
d: &'g T, d: &'g T,
_m: PhantomData<&'a TMCell<T>>, _m: PhantomData<&'a TMCell<T>>,
@ -88,6 +93,13 @@ impl<'a, 'g, T> TMCellReadTxn<'a, 'g, T> {
} }
} }
impl<'a, 'g, T: Clone> TMCellReadTxn<'a, 'g, T> {
#[inline(always)]
pub fn read_copied(&self) -> T {
self.read().clone()
}
}
impl<'a, 'g, T: Copy> TMCellReadTxn<'a, 'g, T> { impl<'a, 'g, T: Copy> TMCellReadTxn<'a, 'g, T> {
fn read_copy(&self) -> T { fn read_copy(&self) -> T {
*self.d *self.d
@ -101,6 +113,10 @@ impl<'a, 'g, T> Deref for TMCellReadTxn<'a, 'g, T> {
} }
} }
unsafe impl<'a, 'g, T: Send> Send for TMCellReadTxn<'a, 'g, T> {}
unsafe impl<'a, 'g, T: Sync> Sync for TMCellReadTxn<'a, 'g, T> {}
#[derive(Debug)]
pub struct TMCellWriteTxn<'a, 'g, T: 'static> { pub struct TMCellWriteTxn<'a, 'g, T: 'static> {
d: &'g T, d: &'g T,
a: &'a Atomic<T>, a: &'a Atomic<T>,
@ -134,6 +150,13 @@ impl<'a, 'g, T> TMCellWriteTxn<'a, 'g, T> {
} }
} }
impl<'a, 'g, T: Clone> TMCellWriteTxn<'a, 'g, T> {
#[inline(always)]
pub fn read_copied(&self) -> T {
self.read().clone()
}
}
impl<'a, 'g, T: Copy> TMCellWriteTxn<'a, 'g, T> { impl<'a, 'g, T: Copy> TMCellWriteTxn<'a, 'g, T> {
fn read_copy(&self) -> T { fn read_copy(&self) -> T {
*self.d *self.d
@ -146,3 +169,5 @@ impl<'a, 'g, T> Deref for TMCellWriteTxn<'a, 'g, T> {
self.d self.d
} }
} }
unsafe impl<'a, 'g, T: Sync> Sync for TMCellWriteTxn<'a, 'g, T> {}

Loading…
Cancel
Save