17 #ifndef CPP_MATE_SHARED_STATE_HPP
18 #define CPP_MATE_SHARED_STATE_HPP
22 #include <condition_variable>
24 #if __cplusplus >= 201703L
25 #include <shared_mutex>
43 template<
typename... A,
typename = std::enable_if_t<std::is_constructible_v<T, A...>>>
44 inline SharedState(A&&... args): _state(std::forward<A>(args)...) {}
50 inline void view(std::function<
void(
const T&)> block)
const {
51 LockRead lock(_mutex);
61 inline R
view(std::function<R(
const T&)> block)
const {
62 LockRead lock(_mutex);
70 inline void modify(std::function<
void(T&)> block) {
71 LockWriteGuard lock(_mutex);
84 #if __cplusplus >= 201703L
85 using Mutex = std::shared_mutex;
86 using LockRead = std::shared_lock<Mutex>;
87 using LockWriteGuard = std::unique_lock<Mutex>;
88 using ConditionVariable = std::condition_variable_any;
91 using Mutex = std::mutex;
92 using LockRead = std::lock_guard<Mutex>;
93 using LockWriteGuard = std::lock_guard<Mutex>;
94 using ConditionVariable = std::condition_variable;
97 using LockWrite = std::unique_lock<Mutex>;
100 mutable Mutex _mutex;
101 ConditionVariable _notify;
107 template <
typename T>
116 void access(std::function<
void(T&)> block) {
117 return block(_parent._state);
126 inline R
extract(std::function<R(T&)> block) {
127 return block(_parent._state);
135 block(_parent._state);
136 _parent._notify.notify_one();
146 R result = block(_parent._state);
147 _parent._notify.notify_one();
156 block(_parent._state);
157 _parent._notify.notify_all();
167 R result = block(_parent._state);
168 _parent._notify.notify_all();
178 _parent._notify.wait(_lock, [&pred,
this] {
return pred(_parent._state); });
187 _lock(_parent._mutex)
Represents possible actions for modification of the state.
Definition: SharedState.hpp:109
R extract(std::function< R(T &)> block)
Provides read and write access to state within the block and returns a value.
Definition: SharedState.hpp:126
friend class SharedState
Definition: SharedState.hpp:183
void notifyAll(std::function< void(T &)> block)
Provides read and write access to state within the block and notifies all threads waiting for state c...
Definition: SharedState.hpp:155
Action & when(std::function< bool(T &)> pred)
Waits till state will satisfy predicate condition.
Definition: SharedState.hpp:177
R notifyAll(std::function< R(T &)> block)
Provides read and write access to state within the block and notifies all threads waiting for state c...
Definition: SharedState.hpp:166
void notifyOne(std::function< void(T &)> block)
Provides read and write access to state within the block and notifies one thread waiting for state ch...
Definition: SharedState.hpp:134
void access(std::function< void(T &)> block)
Provides read and write access to state within the block.
Definition: SharedState.hpp:116
R notifyOne(std::function< R(T &)> block)
Provides read and write access to state within the block and notifies one thread waiting for state ch...
Definition: SharedState.hpp:145
Represents shared state wrapper for thread pool tasks.
Definition: SharedState.hpp:35
R view(std::function< R(const T &)> block) const
Provides read access to state within a block and return value.
Definition: SharedState.hpp:61
void modify(std::function< void(T &)> block)
Provides read and write access to state within a block.
Definition: SharedState.hpp:70
SharedState(A &&... args)
Constructor.
Definition: SharedState.hpp:44
Action modify()
Provides read and write actions with state.
Definition: SharedState.hpp:79
void view(std::function< void(const T &)> block) const
Provides read access to state within a block.
Definition: SharedState.hpp:50
Definition: BinaryData.hpp:28