14#ifndef BOOST_FAIRNESS_SLIM_PRIORITY_MUTEX_HPP
15#define BOOST_FAIRNESS_SLIM_PRIORITY_MUTEX_HPP
30 template<
bool>
struct Range;
37 template<
size_t N = 1,
typename = Range<true> >
38 struct slim_priority_mutex{};
59 class slim_priority_mutex<N, Range<(1 <= N && N <= BOOST_FAIRNESS_SPM64B_SUPPORTED_PRIORITIES)>>{
61 using Thread_cnt_t = uint32_t;
66 slim_priority_mutex() =
default;
69 slim_priority_mutex(
const slim_priority_mutex&) =
delete;
72 slim_priority_mutex& operator=(
const slim_priority_mutex&) =
delete;
75 slim_priority_mutex(slim_priority_mutex&&) =
delete;
78 slim_priority_mutex& operator=(slim_priority_mutex&&) =
delete;
81 ~slim_priority_mutex() =
default;
122 control_block_64b_t localCtrl = ctrl_.load();
127 if (localCtrl.priority_[priority] == uint8_t(-1)){
128 localCtrl = ctrl_.load();
131 if (ctrl_.compare_exchange_weak(localCtrl, localCtrl.increasePriority(priority))){
137 localCtrl = ctrl_.load();
138 if (!localCtrl.isOwned_() && priority <= localCtrl.owned_ && ctrl_.compare_exchange_strong(localCtrl, localCtrl.setOwned())){
141 waitingFlags_[priority].wait(
false);
144 localCtrl = ctrl_.load();
147 if (ctrl_.compare_exchange_weak(localCtrl, localCtrl.decreasePriority(priority))){
193 control_block_64b_t localCtrl;
196 localCtrl = ctrl_.load();
197 localFirstPriority = find_first_priority_(localCtrl);
198 if (localFirstPriority < N){
199 waitingFlags_[localFirstPriority].test_and_set();
200 waitingFlags_[localFirstPriority].notify_one();
202 if (ctrl_.compare_exchange_weak(localCtrl, localCtrl.setPriority(localFirstPriority)))
249 control_block_64b_t localCtrl = ctrl_.load();
250 return !localCtrl.isOwned_() && priority <= localCtrl.owned_ && ctrl_.compare_exchange_strong(localCtrl, localCtrl.setOwned());
254 std::array<std::atomic_flag, N> waitingFlags_{};
255 std::atomic<control_block_64b_t> ctrl_;
258 std::memset(&waitingFlags_, 0b00000000, N);
261 Priority_t find_first_priority_(control_block_64b_t
const& ctrl){
263 if (ctrl.priority_[i] > 0)
271#ifdef BOOST_FAIRNESS_HAS_DWCAS
294 using Thread_cnt_t = uint32_t;
299 slim_priority_mutex() =
default;
302 slim_priority_mutex(
const slim_priority_mutex&) =
delete;
305 slim_priority_mutex& operator=(
const slim_priority_mutex&) =
delete;
308 slim_priority_mutex(slim_priority_mutex&&) =
delete;
311 slim_priority_mutex& operator=(slim_priority_mutex&&) =
delete;
314 ~slim_priority_mutex() =
default;
355 control_block_128b_t localCtrl = ctrl_.load();
360 if (localCtrl.priority_[priority] == uint8_t(-1)){
361 localCtrl = ctrl_.load();
364 if (ctrl_.compare_exchange_weak(localCtrl, localCtrl.increasePriority(priority))){
370 localCtrl = ctrl_.load();
371 if (!localCtrl.isOwned_() && priority <= localCtrl.owned_ && ctrl_.compare_exchange_strong(localCtrl, localCtrl.setOwned())){
374 waitingFlags_[priority].wait(
false);
377 localCtrl = ctrl_.load();
380 if (ctrl_.compare_exchange_weak(localCtrl, localCtrl.decreasePriority(priority))){
426 control_block_128b_t localCtrl;
429 localCtrl = ctrl_.load();
430 localFirstPriority = find_first_priority_(localCtrl);
431 if (localFirstPriority < N){
432 waitingFlags_[localFirstPriority].test_and_set();
433 waitingFlags_[localFirstPriority].notify_one();
435 if (ctrl_.compare_exchange_weak(localCtrl, localCtrl.setPriority(localFirstPriority)))
481 [[nodiscard]]
bool try_lock(
Priority_t const priority = 0){
482 control_block_128b_t localCtrl = ctrl_.load();
483 return !localCtrl.isOwned_() && priority <= localCtrl.owned_ && ctrl_.compare_exchange_strong(localCtrl, localCtrl.setOwned());
487 std::array<boost::atomic_flag, N> waitingFlags_{};
488 boost::atomic<control_block_128b_t> ctrl_;
491 std::memset(&waitingFlags_, 0b00000000, N);
494 Priority_t find_first_priority_(control_block_128b_t
const& ctrl){
496 if (ctrl.priority_[i] > 0)
499 return BOOST_FAIRNESS_SPM128B_SUPPORTED_PRIORITIES;
void lock(Priority_t const priority=0)
Lock the slim_priority_mutex. If another thread has already locked the mutex or other threads are wai...
Definition slim_priority_mutex.hpp:120
bool try_lock(Priority_t const priority=0)
Try to acquire the unique ownership of the slim_priority_mutex. Returns immediately....
Definition slim_priority_mutex.hpp:248
void unlock()
Unlocks the mutex. The mutex must be locked by the current thread of execution, otherwise,...
Definition slim_priority_mutex.hpp:191
This file contains configurations about boost and 128bit cpu support. TODO.
This file contains the definition of the control_block_t. TODO.
#define BOOST_FAIRNESS_SPM64B_SUPPORTED_PRIORITIES
Definition control_block_t.hpp:134
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...