19#ifndef __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__
20#define __PONGASOFT_UTILS_CONCURRENT_CONCURRENT_H__
30template<
typename M,
typename T>
35template<
typename M,
typename T>
37 std::invocable<M, T*> &&
38 std::convertible_to<std::invoke_result_t<M, T*>,
bool>;
84 Element(std::unique_ptr<T> iElement,
bool iNew) noexcept :
fElement{std::move(iElement)},
fNew{iNew} {}
119 std::unique_ptr<Element>
store(std::unique_ptr<Element> iElement)
121 iElement->fNew =
true;
123 return std::move(iElement);
137 std::unique_ptr<Element>
load(std::unique_ptr<Element> iElement)
139 iElement->fNew =
false;
141 return std::move(iElement);
148 std::unique_ptr<Element>
__newElement()
const {
return std::make_unique<Element>(std::move(
__newT()),
false); }
211 auto element =
pop();
243 auto element =
pop();
290 template<ElementModifier<T> Modifier>
293 std::invoke(iElementModifier,
fPushValue->fElement.get());
301 template<ElementPredicate<T> Modifier>
304 if(std::invoke(iElementModifier,
fPushValue->fElement.get()))
415 template<ElementModifier<T> Modifier>
416 void update(Modifier
const &iElementModifier)
418 std::invoke(iElementModifier,
fSetValue->fElement.get());
426 template<ElementPredicate<T> Modifier>
429 if(std::invoke(iElementModifier,
fSetValue->fElement.get()))
469 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:334
void update(Modifier const &iElementModifier)
Use this flavor to avoid copy.
Definition Concurrent.h:416
void set(T const *iValue)
Copy the value to make it accessible to get.
Definition Concurrent.h:405
SingleElementStorage< T >::Element Element
Definition Concurrent.h:439
void get(T *oElement)
Copy the value to *oElement.
Definition Concurrent.h:380
T getCopy()
Definition Concurrent.h:364
T const * get()
Definition Concurrent.h:351
void get(T &oElement)
Copy the value to oElement.
Definition Concurrent.h:372
std::unique_ptr< Element > fGetValue
Definition Concurrent.h:441
bool updateIf(Modifier const &iElementModifier)
Use this flavor to avoid copy.
Definition Concurrent.h:427
std::unique_ptr< Element > fSetValue
Definition Concurrent.h:442
void set(T const &iValue)
Copy the value to make it accessible to get.
Definition Concurrent.h:396
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:209
SingleElementQueue()
Definition Concurrent.h:165
void last(T &oElement) const
Copy the last value that was popped to oElement.
Definition Concurrent.h:233
T const * popOrLast()
Definition Concurrent.h:241
SingleElementStorage< T >::Element Element
Definition Concurrent.h:318
bool updateAndPushIf(Modifier const &iElementModifier)
Use this flavor of push to avoid copy.
Definition Concurrent.h:302
void pushValue()
Definition Concurrent.h:312
T * pop()
Definition Concurrent.h:190
void push(T const &iElement)
Pushes (a copy of) iElement in the queue.
Definition Concurrent.h:271
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:173
std::unique_ptr< Element > fPopValue
Definition Concurrent.h:320
T const * last() const
Definition Concurrent.h:224
std::unique_ptr< Element > fPushValue
Definition Concurrent.h:321
void push(T const *iElement)
Pushes (a copy of) *iElement in the queue.
Definition Concurrent.h:280
void updateAndPush(Modifier const &iElementModifier)
Use this flavor of push to avoid copy.
Definition Concurrent.h:291
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:255
SingleElementStorage(std::unique_ptr< T > iElement, bool iIsEmpty) noexcept
Definition Concurrent.h:92
std::unique_ptr< Element > load(std::unique_ptr< Element > iElement)
Loads an element from storage.
Definition Concurrent.h:137
bool __isLockFree() const
Used (from test) to make sure that it is a lock free implementation.
Definition Concurrent.h:112
~SingleElementStorage()
Definition Concurrent.h:97
std::unique_ptr< Element > store(std::unique_ptr< Element > iElement)
Stores an element in the storage.
Definition Concurrent.h:119
bool isEmpty() const
Definition Concurrent.h:103
std::unique_ptr< T > __newT() const
Definition Concurrent.h:145
std::unique_ptr< Element > __newElement() const
Definition Concurrent.h:148
std::atomic< Element * > fSingleElement
Definition Concurrent.h:152
SpinLock fSpinLock
Definition Concurrent.h:617
AtomicValue(std::unique_ptr< T > iValue)
Definition Concurrent.h:563
void set(T const *iValue)
Updates the current value with the provided one.
Definition Concurrent.h:609
std::unique_ptr< T > fValue
Definition Concurrent.h:616
void get(T *oElement)
Returns the "current" value.
Definition Concurrent.h:591
void get(T &oElement)
Returns the "current" value.
Definition Concurrent.h:581
AtomicValue(T const &iValue)
Definition Concurrent.h:565
void set(T const &iValue)
Updates the current value with the provided one.
Definition Concurrent.h:600
T get()
Returns the "current" value.
Definition Concurrent.h:571
bool pop(T &oElement)
Returns the single element in the queue if there is one.
Definition Concurrent.h:494
SpinLock fSpinLock
Definition Concurrent.h:551
std::unique_ptr< T > fSingleElement
Definition Concurrent.h:549
SingleElementQueue()
Definition Concurrent.h:461
void push(T const &iElement)
Pushes one element in the queue.
Definition Concurrent.h:530
bool pop(T *oElement)
Returns the single element in the queue if there is one.
Definition Concurrent.h:513
bool fIsEmpty
Definition Concurrent.h:550
void push(T const *iElement)
Pushes one element in the queue.
Definition Concurrent.h:541
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:468
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:481
Concept for updating an element. We do not explicitly restrict the return type which is unused.
Definition Concurrent.h:31
Concept for updating an element.
Definition Concurrent.h:36
Definition Concurrent.h:68
The purpose of this namespace is to emphasize the fact that the implementation is using a spinlock.
Definition Concurrent.h:449
Definition Concurrent.h:40
Definition CircularBuffer.h:27
Definition Concurrent.h:83
Element(std::unique_ptr< T > iElement, bool iNew) noexcept
Definition Concurrent.h:84
std::unique_ptr< T > fElement
Definition Concurrent.h:86
bool fNew
Definition Concurrent.h:87