Jamba  3.0.2
pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T > Class Template Reference

#include <Concurrent.h>

Inherited by pongasoft::Utils::Concurrent::LockFree::AtomicValue< T >, and pongasoft::Utils::Concurrent::LockFree::SingleElementQueue< T >.

Classes

struct  Element
 

Public Member Functions

bool __isLockFree () const
 
bool isEmpty () const
 
 SingleElementStorage (std::unique_ptr< T > iElement, bool iIsEmpty) noexcept
 
 ~SingleElementStorage ()
 

Protected Member Functions

std::unique_ptr< Element__newElement () const
 
std::unique_ptr< T > __newT () const
 
std::unique_ptr< Elementload (std::unique_ptr< Element > iElement)
 
std::unique_ptr< Elementstore (std::unique_ptr< Element > iElement)
 

Private Attributes

std::atomic< Element * > fSingleElement
 

Detailed Description

template<typename T>
class pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >

This (internal) class stores a single element. This implementation is NOT fully thread safe. It is only thread safe as long as load is called by a single thread and store is called by another single thread as well (if load and store are called by the same thread it is obviously thread safe since it would be mono thread...)

Todo:
the implementation somehow assumes that T is a real type, not a primitive.. maybe there is a way to write a primitive version (if that becomes a necessity)

Constructor & Destructor Documentation

◆ SingleElementStorage()

template<typename T>
pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::SingleElementStorage ( std::unique_ptr< T >  iElement,
bool  iIsEmpty 
)
inlinenoexcept

◆ ~SingleElementStorage()

Member Function Documentation

◆ __isLockFree()

template<typename T>
bool pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::__isLockFree ( ) const
inline

Used (from test) to make sure that it is a lock free implementation. Relies on an atomic pointer and in general the implementation should be lock free but we make sure here.

◆ __newElement()

template<typename T>
std::unique_ptr<Element> pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::__newElement ( ) const
inlineprotected

◆ __newT()

template<typename T>
std::unique_ptr<T> pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::__newT ( ) const
inlineprotected

◆ isEmpty()

template<typename T>
bool pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::isEmpty ( ) const
inline

◆ load()

template<typename T>
std::unique_ptr<Element> pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::load ( std::unique_ptr< Element iElement)
inlineprotected

Loads an element from storage. In order to avoid copy and memory allocation, the api actually takes an element to replace the one that is returned. The correct usage pattern should be:

if(!isEmpty()) myPtr = std::move(load(std::move(myPtr)));

That way you are guaranteed that what load returns will have a fNew flag set to true...

Returns
element with flag fNew set to true if there was a new element, false otherwise

◆ store()

template<typename T>
std::unique_ptr<Element> pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::store ( std::unique_ptr< Element iElement)
inlineprotected

Stores an element in the storage. Replaces the current one if there is one. In order to avoid copy and memory allocation, the storage assumes ownership of the element provided and as result returns the one it replaces.

Member Data Documentation

◆ fSingleElement

template<typename T>
std::atomic<Element *> pongasoft::Utils::Concurrent::LockFree::SingleElementStorage< T >::fSingleElement
private

The documentation for this class was generated from the following file: