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>
impl<T> AtomicTripleBuffer<T>
Sourcepub fn new(init: T) -> Selfwhere
T: Clone,
pub fn new(init: T) -> Selfwhere
T: Clone,
Creates a new AtomicTripleBuffer with all buffers set to init.
Sourcepub fn front_buffer(&self) -> Result<TBFrontGuard<'_, T>, TBLockError>
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.
Sourcepub fn back_buffers(&self) -> Result<TBBackGuard<'_, T>, TBLockError>
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.