[][src]Struct concread::ebrcell::EbrCell

pub struct EbrCell<T: Clone + Send + 'static> { /* fields omitted */ }

A concurrently readable cell.

This structure behaves in a similar manner to a RwLock<Box<T>>. However unlike a read-write lock, writes and parallel reads can be performed simultaneously. This means writes do not block reads or reads do not block writes.

To achieve this a form of "copy-on-write" (or for Rust, clone on write) is used. As a write transaction begins, we clone the existing data to a new location that is capable of being mutated.

Readers are guaranteed that the content of the EbrCell will live as long as the read transaction is open, and will be consistent for the duration of the transaction. There can be an "unlimited" number of readers in parallel accessing different generations of data of the EbrCell.

Data that is copied is garbage collected using the crossbeam-epoch library.

Writers are serialised and are guaranteed they have exclusive write access to the structure.

Examples

use concread::ebrcell::EbrCell;

let data: i64 = 0;
let ebrcell = EbrCell::new(data);

// Begin a read transaction
let read_txn = ebrcell.read();
assert_eq!(*read_txn, 0);
{
    // Now create a write, and commit it.
    let mut write_txn = ebrcell.write();
    *write_txn = 1;
    // Commit the change
    write_txn.commit();
}
// Show the previous generation still reads '0'
assert_eq!(*read_txn, 0);
let new_read_txn = ebrcell.read();
// And a new read transaction has '1'
assert_eq!(*new_read_txn, 1);

Implementations

impl<T> EbrCell<T> where
    T: Clone + Send + 'static, 
[src]

pub fn new(data: T) -> Self[src]

Create a new EbrCell storing type T. T must implement Clone.

pub fn write(&self) -> EbrCellWriteTxn<T>[src]

Begin a write transaction, returning a write guard.

pub fn try_write(&self) -> Option<EbrCellWriteTxn<T>>[src]

Attempt to begin a write transaction. If it's already held, None is returned.

pub fn read(&self) -> EbrCellReadTxn<T>[src]

Begin a read transaction. The returned [`EbrCellReadTxn'] guarantees the data lives long enough via crossbeam's Epoch type. When this is dropped the data may be freed at some point in the future.

Trait Implementations

impl<T: Debug + Clone + Send + 'static> Debug for EbrCell<T>[src]

impl<T> Drop for EbrCell<T> where
    T: Clone + Send + 'static, 
[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for EbrCell<T>

impl<T> Send for EbrCell<T> where
    T: Sync

impl<T> Sync for EbrCell<T> where
    T: Sync

impl<T> Unpin for EbrCell<T>

impl<T> UnwindSafe for EbrCell<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.