12#ifndef BOOST_FAIRNESS_SHARED_PRIORITY_MUTEX_HPP
13#define BOOST_FAIRNESS_SHARED_PRIORITY_MUTEX_HPP
47 template<
size_t N = 1>
51 using Thread_cnt_t = uint32_t;
53 struct threadPriority{
54 Thread_cnt_t writers_waiting{};
55 Thread_cnt_t readers_waiting{};
58 template<
size_t S = 1>
61 std::array<std::atomic<uint32_t>, S> writers{};
62 std::array<std::atomic<uint32_t>, S> readers{};
111 internalMutex_.lock(priority);
113 ++priorities_[priority].writers_waiting;
115 ++totalWritersWaiting_;
121 totalCurrentReaders_ == 0 &&
123 find_first_priority_() >= priority
125 --priorities_[priority].writers_waiting;
127 --totalWritersWaiting_;
131 internalMutex_.unlock();
136 internalMutex_.unlock();
140 internalMutex_.lock(priority);
173 internalMutex_.lock();
177 p = find_first_priority_();
181 internalMutex_.unlock();
186 if (totalWritersWaiting_ == 0)
188 allow_all_readers_();
190 internalMutex_.unlock();
192 notify_all_readers_();
199 internalMutex_.unlock();
233 internalMutex_.lock(priority);
236 totalCurrentReaders_ > 0 ||
237 find_first_priority_() < priority)
240 internalMutex_.unlock();
247 internalMutex_.unlock();
283 internalMutex_.lock(priority);
285 ++priorities_[priority].readers_waiting;
289 if (!lockOwned_ && find_first_priority_with_writers_() >= priority)
291 ++totalCurrentReaders_;
293 --priorities_[priority].readers_waiting;
295 internalMutex_.unlock();
300 internalMutex_.unlock();
304 internalMutex_.lock(priority);
337 internalMutex_.lock();
339 --totalCurrentReaders_;
341 p = find_first_priority_();
345 internalMutex_.unlock();
350 if (totalWritersWaiting_ == 0)
352 allow_all_readers_();
354 internalMutex_.unlock();
356 notify_all_readers_();
361 if (totalCurrentReaders_ == 0)
365 internalMutex_.unlock();
372 internalMutex_.unlock();
405 internalMutex_.lock(priority);
407 if (lockOwned_ || find_first_priority_with_writers_() < priority){
409 internalMutex_.unlock();
414 ++totalCurrentReaders_;
416 internalMutex_.unlock();
426 std::array<threadPriority, N> priorities_{};
427 Thread_cnt_t totalCurrentReaders_{};
428 Thread_cnt_t totalWritersWaiting_{};
433 if (priorities_[i].writers_waiting+priorities_[i].readers_waiting > 0)
439 Priority_t find_first_priority_with_writers_()
const {
441 if (priorities_[i].writers_waiting > 0)
447 void notify_all_readers_(){
449 waitingFlags_.readers[i].store(
PROCEED);
461 waitingFlags_.writers[i].store(
WAIT);
462 waitingFlags_.readers[i].store(
WAIT);
464 waitingFlags_.writers[p].store(
PROCEED);
465 waitingFlags_.readers[p].store(
PROCEED);
468 void allow_all_readers_(){
470 waitingFlags_.readers[i].store(
PROCEED);
471 waitingFlags_.writers[i].store(
WAIT);
The shared_priority_mutex is an advanced synchronization mechanism that enhances the traditional shar...
Definition shared_priority_mutex.hpp:49
void lock(Priority_t const priority=0)
Try to acquire the unique ownership of the shared_priority_mutex, blocking the thread if the shared_p...
Definition shared_priority_mutex.hpp:109
void unlock()
Release the shared_priority_mutex from unique ownership. The mutex must be locked by the current th...
Definition shared_priority_mutex.hpp:169
void lock_shared(Priority_t priority=0)
Try to acquire the shared ownership of the shared_priority_mutex, blocking the thread if the shared_p...
Definition shared_priority_mutex.hpp:281
bool try_lock(Priority_t const priority=0)
Try to acquire the unique ownership of the shared_priority_mutex, if successful will return true,...
Definition shared_priority_mutex.hpp:231
bool try_lock_shared(Priority_t priority=0)
Try to acquire the shared ownership of the shared_priority_mutex, if successful will return true,...
Definition shared_priority_mutex.hpp:403
void unlock_shared()
Release the shared_priority_mutex from shared ownership by the calling thread. The mutex shared_prio...
Definition shared_priority_mutex.hpp:333
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_all(T &mem) noexcept
Definition wait_ops.hpp:108
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 PROCEED
Definition priority_mutex_tatas.hpp:25
#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.