AtomicTripleBuffer

Struct AtomicTripleBuffer 

Source
pub struct AtomicTripleBuffer<T> { /* private fields */ }
Expand description

A container holding three instances of T that can be used as a queue.

The three instances correspond to three buffers: front buffer, back buffer, and pending buffer. When the back buffer is written, it can be “committed” to swap it with the pending buffer. The next time an attempt is made to read the front buffer, it will be swapped with any pending buffer before reading.

The front buffer can be locked by one thread at a time and the back buffer can be locked by another thread. This restriction ensures only one thread has access to each buffer at time, making it safe to access the AtomicTripleBuffer from multiple threads without T being Sync.

Locking and swapping are both accomplished by adjusting a single lock-free flag holding the indexes of the buffers. No copying is done when swapping buffers.

Implementations§

Source§

impl<T> AtomicTripleBuffer<T>

Source

pub fn new(init: T) -> Self
where T: Clone,

Creates a new AtomicTripleBuffer with all buffers set to init.

Source

pub fn front_buffer(&self) -> Result<TBFrontGuard<'_, T>, TBLockError>

Tries to lock the front buffer for access. Returns TBLockError::AlreadyLocked if the front buffer is already locked by another thread. If the pending buffer is locked, this method will atomically swap the pending and front buffers and unlock the pending buffer at the same time the front buffer is locked.

Source

pub fn back_buffers(&self) -> Result<TBBackGuard<'_, T>, TBLockError>

Tries to lock the back buffer for access. Returns TBLockError::AlreadyLocked if the back buffer is already locked by another thread. Also allows access to the pending buffer if a frame is not pending.

Trait Implementations§

Source§

impl<T: Debug> Debug for AtomicTripleBuffer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for AtomicTripleBuffer<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Send> Send for AtomicTripleBuffer<T>

Source§

impl<T: Send> Sync for AtomicTripleBuffer<T>

Auto Trait Implementations§

§

impl<T> !Freeze for AtomicTripleBuffer<T>

§

impl<T> !RefUnwindSafe for AtomicTripleBuffer<T>

§

impl<T> Unpin for AtomicTripleBuffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for AtomicTripleBuffer<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.