fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
priority_mutex_tatas.hpp
Go to the documentation of this file.
1
13#ifndef BOOST_FAIRNESS_PRIORITY_MUTEX_TATAS_HPP
14#define BOOST_FAIRNESS_PRIORITY_MUTEX_TATAS_HPP
15#include <atomic>
16#include <array>
21
22namespace boost::fairness{
23
24 #define WAIT 0
25 #define PROCEED 1
26
48 template<size_t N = 1>
49 requires (N >= 1 && N <= BOOST_FAIRNESS_MAXIMUM_PRIORITY)
50 class priority_mutex{
51
52 using Thread_cnt_t = uint32_t;
53
54 public:
55
57 priority_mutex() = default;
58
60 priority_mutex(const priority_mutex&) = delete;
61
63 priority_mutex& operator=(const priority_mutex&) = delete;
64
66 priority_mutex(priority_mutex&&) = delete;
67
69 priority_mutex& operator=(priority_mutex&&) = delete;
70
72 ~priority_mutex() = default;
73
114 void lock(Priority_t const priority = 0){
115
116 internalMutex_.lock(priority);
117
118 ++waiters_[priority];
119
120 for (;;){
121
122 if (
123 !lockOwned_ &&
124 find_first_priority_() >= priority
125 ){
126
127 --waiters_[priority];
128
129 lockOwned_ = true;
130
131 internalMutex_.unlock();
132
133 return;
134 }
135
136 internalMutex_.unlock();
137
138 detail::wait(waitingFlag_[priority], detail::wait_flag);
139
140 internalMutex_.lock(priority);
141
142 }
143 }
144
183 void unlock(){
184
185 Priority_t p;
186
187 internalMutex_.lock();
188
189 lockOwned_ = false;
190
191 p = find_first_priority_();
192
194 internalMutex_.unlock();
195 return;
196 }
197
198 waitingFlag_.reset_(p);
199
200 internalMutex_.unlock();
201
202 // we use an internal spinlock based on tatas just to avoid thundering herd here instead of using tatas directly
203 detail::notify_one(waitingFlag_[p]);
204 }
205
253 [[nodiscard]] bool try_lock(Priority_t const priority = 0){
254 internalMutex_.lock(priority);
255
256 if (lockOwned_ ||
257 find_first_priority_() < priority){
258
259 internalMutex_.unlock();
260
261 return false;
262 }
263
264 lockOwned_ = true;
265
266 internalMutex_.unlock();
267
268 return true;
269 }
270
271 private:
272
274
275 detail::WaitPool<N> waitingFlag_{};
276
277 std::array<Thread_cnt_t, N> waiters_{};
278
279 bool lockOwned_{};
280
281 Priority_t find_first_priority_(){
282 for (Priority_t i = 0; i < N; ++i){
283 if (waiters_[i] > 0)
284 return i;
285 }
287 }
288
289 };
290
291 #undef WAIT
292 #undef PROCEED
293
294}
295#endif // BOOST_FAIRNESS_PRIORITY_MUTEX_TATAS_HPP
Definition wait_pool.hpp:45
bool try_lock(Priority_t const priority=0)
Try to acquire the unique ownership of the priority_mutex with a designated priority....
Definition priority_mutex_tatas.hpp:253
The spinlock_priority_mutex is an advanced synchronization mechanism that enhances the traditional mu...
Definition spinlock_priority_mutex_cpl.hpp:31
#define BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE
Size to be aligned to avoid false sharing
Definition config.hpp:85
void wait(T &mem, K const expected) noexcept
Definition wait_ops.hpp:69
void notify_one(T &mem) noexcept
Definition wait_ops.hpp:103
Definition acquisition_modes.hpp:16
uint8_t Priority_t
Definition priority_t.hpp:17
Alias the type Priority_t. Priority_t is the type of priorities that are used by the priority_mutexes...
#define BOOST_FAIRNESS_MAXIMUM_PRIORITY
Priorities are indexes in an array, that means that if I define a priority_mutex<BOOST_FAIRNESS_MAXIM...
Definition priority_t.hpp:24
This file contains the implementation of the spinlock_priority_mutex based on a scalable list base al...
This file contains the implementation of the wait operations used by the mutexes.
This file contains the implementation of a wait pool.