19#ifndef __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__
20#define __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__
72 Element(std::unique_ptr<T> iElement,
bool iNew) noexcept :
fElement{std::move(iElement)},
fNew{iNew} {}
107 std::unique_ptr<Element>
store(std::unique_ptr<Element> iElement)
109 iElement->fNew =
true;
111 return std::move(iElement);
125 std::unique_ptr<Element>
load(std::unique_ptr<Element> iElement)
127 iElement->fNew =
false;
129 return std::move(iElement);
136 std::unique_ptr<Element>
__newElement()
const {
return std::make_unique<Element>(std::move(
__newT()),
false); }
199 auto element =
pop();
231 auto element =
pop();
278 template<
class ElementModifier>
289 template<
class ElementModifier>
292 if(iElementModifier(
fPushValue->fElement.get()))
403 template<
class ElementModifier>
404 void update(ElementModifier
const &iElementModifier)
406 iElementModifier(
fSetValue->fElement.get());
414 template<
class ElementModifier>
415 bool updateIf(ElementModifier
const &iElementModifier)
417 if(iElementModifier(
fSetValue->fElement.get()))
457 bool iIsEmpty =
false) :
A simple implementation of a spin lock using the std::atomic_flag which is guaranteed to be atomic an...
Definition SpinLock.h:34
AtomicValue(std::unique_ptr< T > iValue)
Definition Concurrent.h:322
void set(T const *iValue)
Copy the value to make it accessible to get.
Definition Concurrent.h:393
void get(T *oElement)
Copy the value to *oElement.
Definition Concurrent.h:368
T getCopy()
Definition Concurrent.h:352
T const * get()
Definition Concurrent.h:339
bool updateIf(ElementModifier const &iElementModifier)
Use this flavor to avoid copy.
Definition Concurrent.h:415
void get(T &oElement)
Copy the value to oElement.
Definition Concurrent.h:360
typename SingleElementStorage< T >::Element Element
Definition Concurrent.h:427
std::unique_ptr< Element > fGetValue
Definition Concurrent.h:429
void update(ElementModifier const &iElementModifier)
Use this flavor to avoid copy.
Definition Concurrent.h:404
std::unique_ptr< Element > fSetValue
Definition Concurrent.h:430
void set(T const &iValue)
Copy the value to make it accessible to get.
Definition Concurrent.h:384
bool pop(T &oElement)
Copy the popped value to oElement and return true when there is a new value otherwise do nothing and ...
Definition Concurrent.h:197
SingleElementQueue()
Definition Concurrent.h:153
void last(T &oElement) const
Copy the last value that was popped to oElement.
Definition Concurrent.h:221
T const * popOrLast()
Definition Concurrent.h:229
void updateAndPush(ElementModifier const &iElementModifier)
Use this flavor of push to avoid copy.
Definition Concurrent.h:279
void pushValue()
Definition Concurrent.h:300
T * pop()
Definition Concurrent.h:178
void push(T const &iElement)
Pushes (a copy of) iElement in the queue.
Definition Concurrent.h:259
typename SingleElementStorage< T >::Element Element
Definition Concurrent.h:306
SingleElementQueue(std::unique_ptr< T > iElement, bool iIsEmpty=false)
This constructor should be used if T does not provide an empty constructor.
Definition Concurrent.h:161
std::unique_ptr< Element > fPopValue
Definition Concurrent.h:308
bool updateAndPushIf(ElementModifier const &iElementModifier)
Use this flavor of push to avoid copy.
Definition Concurrent.h:290
T const * last() const
Definition Concurrent.h:212
std::unique_ptr< Element > fPushValue
Definition Concurrent.h:309
void push(T const *iElement)
Pushes (a copy of) *iElement in the queue.
Definition Concurrent.h:268
void popOrLast(T &oElement)
Copy either the new value (if there is one) or the last value that was popped to oElement.
Definition Concurrent.h:243
SingleElementStorage(std::unique_ptr< T > iElement, bool iIsEmpty) noexcept
Definition Concurrent.h:80
std::unique_ptr< Element > load(std::unique_ptr< Element > iElement)
Loads an element from storage.
Definition Concurrent.h:125
bool __isLockFree() const
Used (from test) to make sure that it is a lock free implementation.
Definition Concurrent.h:100
~SingleElementStorage()
Definition Concurrent.h:85
std::unique_ptr< Element > store(std::unique_ptr< Element > iElement)
Stores an element in the storage.
Definition Concurrent.h:107
bool isEmpty() const
Definition Concurrent.h:91
std::unique_ptr< T > __newT() const
Definition Concurrent.h:133
std::unique_ptr< Element > __newElement() const
Definition Concurrent.h:136
std::atomic< Element * > fSingleElement
Definition Concurrent.h:140
SpinLock fSpinLock
Definition Concurrent.h:605
AtomicValue(std::unique_ptr< T > iValue)
Definition Concurrent.h:551
void set(T const *iValue)
Updates the current value with the provided one.
Definition Concurrent.h:597
std::unique_ptr< T > fValue
Definition Concurrent.h:604
void get(T *oElement)
Returns the "current" value.
Definition Concurrent.h:579
void get(T &oElement)
Returns the "current" value.
Definition Concurrent.h:569
AtomicValue(T const &iValue)
Definition Concurrent.h:553
void set(T const &iValue)
Updates the current value with the provided one.
Definition Concurrent.h:588
T get()
Returns the "current" value.
Definition Concurrent.h:559
bool pop(T &oElement)
Returns the single element in the queue if there is one.
Definition Concurrent.h:482
SpinLock fSpinLock
Definition Concurrent.h:539
std::unique_ptr< T > fSingleElement
Definition Concurrent.h:537
SingleElementQueue()
Definition Concurrent.h:449
void push(T const &iElement)
Pushes one element in the queue.
Definition Concurrent.h:518
bool pop(T *oElement)
Returns the single element in the queue if there is one.
Definition Concurrent.h:501
bool fIsEmpty
Definition Concurrent.h:538
void push(T const *iElement)
Pushes one element in the queue.
Definition Concurrent.h:529
SingleElementQueue(std::unique_ptr< T > iFirstElement, bool iIsEmpty=false)
This constructor can be used to add one element to the queue right away or when there is no empty con...
Definition Concurrent.h:456
bool isEmpty() const
Note that although this api is thread safe, it will only report the state of the queue at the moment ...
Definition Concurrent.h:469
Definition Concurrent.h:56
The purpose of this namespace is to emphasize the fact that the implementation is using a spinlock.
Definition Concurrent.h:437
Definition Concurrent.h:28
Definition CircularBuffer.h:26
Definition Concurrent.h:71
Element(std::unique_ptr< T > iElement, bool iNew) noexcept
Definition Concurrent.h:72
std::unique_ptr< T > fElement
Definition Concurrent.h:74
bool fNew
Definition Concurrent.h:75