18 #ifndef __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__ 19 #define __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__ 27 namespace Concurrent {
71 Element(std::unique_ptr<T> iElement,
bool iNew) noexcept :
fElement{std::move(iElement)},
fNew{iNew} {}
106 std::unique_ptr<Element>
store(std::unique_ptr<Element> iElement)
108 iElement->fNew =
true;
110 return std::move(iElement);
124 std::unique_ptr<Element>
load(std::unique_ptr<Element> iElement)
126 iElement->fNew =
false;
128 return std::move(iElement);
135 std::unique_ptr<Element>
__newElement()
const {
return std::make_unique<Element>(std::move(
__newT()),
false); }
198 auto element =
pop();
230 auto element =
pop();
277 template<
class ElementModifier>
288 template<
class ElementModifier>
291 if(iElementModifier(
fPushValue->fElement.get()))
402 template<
class ElementModifier>
403 void update(ElementModifier
const &iElementModifier)
405 iElementModifier(
fSetValue->fElement.get());
413 template<
class ElementModifier>
414 bool updateIf(ElementModifier
const &iElementModifier)
416 if(iElementModifier(
fSetValue->fElement.get()))
436 namespace WithSpinLock {
456 bool iIsEmpty =
false) :
613 #endif // __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__ std::unique_ptr< T > fValue
Definition: Concurrent.h:603
std::unique_ptr< Element > load(std::unique_ptr< Element > iElement)
Loads an element from storage.
Definition: Concurrent.h:124
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:455
std::unique_ptr< T > fElement
Definition: Concurrent.h:73
A simple implementation of a spin lock using the std::atomic_flag which is guaranteed to be atomic an...
Definition: SpinLock.h:32
std::unique_ptr< Element > __newElement() const
Definition: Concurrent.h:135
This is the lock free version of the AtomicValue.
Definition: Concurrent.h:317
bool isEmpty() const
Definition: Concurrent.h:90
void pushValue()
Definition: Concurrent.h:299
SingleElementQueue()
Definition: Concurrent.h:448
Lock acquire()
Definition: SpinLock.h:74
void get(T *oElement)
Copy the value to *oElement.
Definition: Concurrent.h:367
bool fNew
Definition: Concurrent.h:74
SpinLock fSpinLock
Definition: Concurrent.h:538
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:196
This class encapsulates a single atomic value.
Definition: Concurrent.h:547
T const * get()
Definition: Concurrent.h:338
SingleElementStorage(std::unique_ptr< T > iElement, bool iIsEmpty) noexcept
Definition: Concurrent.h:79
T const * last() const
Definition: Concurrent.h:211
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:468
void push(T const &iElement)
Pushes one element in the queue.
Definition: Concurrent.h:517
Definition: Concurrent.h:69
SingleElementQueue()
Definition: Concurrent.h:152
void get(T *oElement)
Returns the "current" value.
Definition: Concurrent.h:578
AtomicValue(std::unique_ptr< T > iValue)
Definition: Concurrent.h:321
void get(T &oElement)
Returns the "current" value.
Definition: Concurrent.h:568
bool updateAndPushIf(ElementModifier const &iElementModifier)
Use this flavor of push to avoid copy.
Definition: Concurrent.h:289
void push(T const &iElement)
Pushes (a copy of) iElement in the queue.
Definition: Concurrent.h:258
AtomicValue(std::unique_ptr< T > iValue)
Definition: Concurrent.h:550
bool fIsEmpty
Definition: Concurrent.h:537
T getCopy()
Definition: Concurrent.h:351
std::unique_ptr< Element > fSetValue
Definition: Concurrent.h:429
~SingleElementStorage()
Definition: Concurrent.h:84
void push(T const *iElement)
Pushes (a copy of) *iElement in the queue.
Definition: Concurrent.h:267
SpinLock fSpinLock
Definition: Concurrent.h:604
void get(T &oElement)
Copy the value to oElement.
Definition: Concurrent.h:359
T const * popOrLast()
Definition: Concurrent.h:228
Element(std::unique_ptr< T > iElement, bool iNew) noexcept
Definition: Concurrent.h:71
void update(ElementModifier const &iElementModifier)
Use this flavor to avoid copy.
Definition: Concurrent.h:403
void set(T const *iValue)
Copy the value to make it accessible to get.
Definition: Concurrent.h:392
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:160
void set(T const &iValue)
Updates the current value with the provided one.
Definition: Concurrent.h:587
AtomicValue(T const &iValue)
Definition: Concurrent.h:552
std::atomic< Element * > fSingleElement
Definition: Concurrent.h:139
std::unique_ptr< T > fSingleElement
Definition: Concurrent.h:536
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:242
std::unique_ptr< Element > fPushValue
Definition: Concurrent.h:308
std::unique_ptr< Element > fPopValue
Definition: Concurrent.h:307
This class implements a queue which has at most 1 element (0 or 1).
Definition: Concurrent.h:445
void set(T const &iValue)
Copy the value to make it accessible to get.
Definition: Concurrent.h:383
bool updateIf(ElementModifier const &iElementModifier)
Use this flavor to avoid copy.
Definition: Concurrent.h:414
std::unique_ptr< Element > fGetValue
Definition: Concurrent.h:428
typename SingleElementStorage< pongasoft::VST::NormalizedState >::Element Element
Definition: Concurrent.h:426
void updateAndPush(ElementModifier const &iElementModifier)
Use this flavor of push to avoid copy.
Definition: Concurrent.h:278
std::unique_ptr< T > __newT() const
Definition: Concurrent.h:132
void last(T &oElement) const
Copy the last value that was popped to oElement.
Definition: Concurrent.h:220
typename SingleElementStorage< pongasoft::VST::NormalizedState >::Element Element
Definition: Concurrent.h:305
void push(T const *iElement)
Pushes one element in the queue.
Definition: Concurrent.h:528
bool pop(T *oElement)
Returns the single element in the queue if there is one.
Definition: Concurrent.h:500
void set(T const *iValue)
Updates the current value with the provided one.
Definition: Concurrent.h:596
This is the lock free version of the SingleElementQueue.
Definition: Concurrent.h:148
bool pop(T &oElement)
Returns the single element in the queue if there is one.
Definition: Concurrent.h:481
bool __isLockFree() const
Used (from test) to make sure that it is a lock free implementation.
Definition: Concurrent.h:99
T get()
Returns the "current" value.
Definition: Concurrent.h:558
This (internal) class stores a single element.
Definition: Concurrent.h:65
std::unique_ptr< Element > store(std::unique_ptr< Element > iElement)
Stores an element in the storage.
Definition: Concurrent.h:106
T * pop()
Definition: Concurrent.h:177