fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
recursive_priority_mutex.hpp
Go to the documentation of this file.
1
14#ifndef BOOST_FAIRNESS_RECURSIVE_PRIORITY_MUTEX_HPP
15#define BOOST_FAIRNESS_RECURSIVE_PRIORITY_MUTEX_HPP
16#include <thread>
17#include <atomic>
18#include <array>
22
23namespace boost::fairness{
24
25 #define WAIT 0
26 #define PROCEED 1
27
48 template<size_t N = 1>
49 requires (N >= 1 && N <= BOOST_FAIRNESS_MAXIMUM_PRIORITY)
51
52 using Thread_cnt_t = uint32_t;
53
54 public:
55
57 recursive_priority_mutex() = default;
58
61
63 recursive_priority_mutex& operator=(const recursive_priority_mutex&) = delete;
64
67
70
72 ~recursive_priority_mutex() = default;
73
115 void lock(Priority_t const priority = 0){
116
117 internalMutex_.lock(priority);
118
119 ++waiters_[priority];
120
121 for(;;){
122 if (
123 lock_owned_by_me_() ||
124 (find_first_priority_() >= priority &&
125 lock_not_owned_())
126 ){
127
128 owner_ = std::this_thread::get_id();
129
130 --waiters_[priority];
131
132 ++recursionCounter_;
133
134 internalMutex_.unlock();
135
136 return;
137 }
138
139 internalMutex_.unlock();
140
141 detail::wait(waitingFlag_[priority], WAIT);
142
143 internalMutex_.lock(priority);
144 }
145 }
146
184 void unlock(){
185
186 Priority_t p;
187
188 internalMutex_.lock();
189
190 p = find_first_priority_();
191
192 --recursionCounter_;
193
194 if (recursionCounter_ != 0)
195 {
196 internalMutex_.unlock();
197
198 return;
199 }
200
201 owner_ = std::thread::id();
202
204
205 internalMutex_.unlock();
206
207 return;
208 }
209
210 waitingFlag_.reset_(p);
211
212 internalMutex_.unlock();
213
214 detail::notify_one(waitingFlag_[p]);
215 }
216
264 [[nodiscard]] bool try_lock(Priority_t const priority = 0){
265
266 internalMutex_.lock(priority);
267
268 if (lock_owned_by_me_() ||
269 (find_first_priority_() >= priority && lock_not_owned_())){
270
271 owner_ = std::this_thread::get_id();
272
273 ++recursionCounter_;
274
275 internalMutex_.unlock();
276
277 return true;
278 }
279
280 internalMutex_.unlock();
281
282 return false;
283 }
284
285 private:
286
288
289 detail::WaitPool<N> waitingFlag_{};
290
291 std::array<Thread_cnt_t, N> waiters_{};
292
293 std::thread::id owner_{};
294
295 uint32_t recursionCounter_{};
296
297 bool lock_not_owned_(){
298 return std::thread::id() == owner_;
299 }
300
301 bool lock_owned_by_me_(){
302 return owner_ == std::this_thread::get_id();
303 }
304
305 Priority_t find_first_priority_(){
306 for (Priority_t i = 0; i < N; ++i){
307 if (waiters_[i] > 0)
308 return i;
309 }
311 }
312 };
313
314 #undef WAIT
315 #undef PROCEED
316}
317#endif // BOOST_FAIRNESS_RECURSIVE_PRIORITY_MUTEX_HPP
Definition wait_pool.hpp:45
The recursive_priority_mutex is an advanced synchronization mechanism that enhances the traditional m...
Definition recursive_priority_mutex.hpp:50
bool try_lock(Priority_t const priority=0)
Try to acquire the unique ownership of the recursive_priority_mutex with a designated priority....
Definition recursive_priority_mutex.hpp:264
void lock(Priority_t const priority=0)
Acquire the recursive_priority_mutex with a designated priority. If another thread has already obtain...
Definition recursive_priority_mutex.hpp:115
void unlock()
Unlock the recursive_priority_mutex when its ownership level is 1, signifying that there was precisel...
Definition recursive_priority_mutex.hpp:184
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
#define WAIT
Definition priority_mutex_tatas.hpp:24
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.