fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
spinlock_priority_mutex_tatas.hpp
Go to the documentation of this file.
1
13#ifndef BOOST_FAIRNESS_SPINLOCK_PRIORITY_MUTEX_TATAS_HPP
14#define BOOST_FAIRNESS_SPINLOCK_PRIORITY_MUTEX_TATAS_HPP
15#include <atomic>
16#include <array>
19
20namespace boost::fairness{
21
27 template<size_t N = 1>
28 requires (N >= 1 && N <= BOOST_FAIRNESS_MAXIMUM_PRIORITY)
29 class spinlock_priority_mutex{
30
31 using Thread_cnt_t = uint32_t;
32
33 public:
34
36 spinlock_priority_mutex() = default;
37
39 spinlock_priority_mutex(const spinlock_priority_mutex&) = delete;
40
42 spinlock_priority_mutex& operator=(const spinlock_priority_mutex&) = delete;
43
45 spinlock_priority_mutex(spinlock_priority_mutex&&) = delete;
46
48 spinlock_priority_mutex& operator=(spinlock_priority_mutex&&) = delete;
49
51 ~spinlock_priority_mutex() = default;
52
68 void lock(Priority_t const priority = 0){
69
70 bool localLockOwned = lockOwned_.test(std::memory_order_relaxed);
71
72 Priority_t localCurrentPriority = currentPriority_.load(std::memory_order_relaxed);
73
74 waiters_[priority].fetch_add(1, std::memory_order_relaxed);
75
76 for (;;){
77
78 if (!localLockOwned & (localCurrentPriority >= priority)){
79 if (!lockOwned_.test_and_set(std::memory_order_acquire))
80 break;
81 }
82 detail::spin_wait(lockOwned_, true);
83 localLockOwned = lockOwned_.test(std::memory_order_relaxed);
84 localCurrentPriority = currentPriority_.load(std::memory_order_relaxed);
85
86 }
87
88 waiters_[priority].fetch_sub(1, std::memory_order_relaxed);
89
90 }
91
105 void unlock(){
106 currentPriority_.store(find_first_priority_(), std::memory_order_relaxed);
107 lockOwned_.clear(std::memory_order_release);
108 }
109
126 [[nodiscard]] bool try_lock(Priority_t const priority = 0){
127 return (currentPriority_.load(std::memory_order_relaxed) >= priority && !lockOwned_.test_and_set(std::memory_order_acquire));
128 }
129
130 private:
131 alignas(BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE) std::array<std::atomic<Thread_cnt_t>, N> waiters_{};
132 alignas(BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE) std::atomic<Priority_t> currentPriority_{BOOST_FAIRNESS_MAXIMUM_PRIORITY};
133 alignas(BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE) std::atomic_flag lockOwned_{};
134
135 Priority_t find_first_priority_(){
136 for (Priority_t i = 0; i < N; ++i){
137 if (waiters_[i] > 0)
138 return i;
139 }
141 }
142 };
143}
144#endif // BOOST_FAIRNESS_SPINLOCK_PRIORITY_MUTEX_TATAS_HPP
void lock(Priority_t const priority=0)
Try to acquire the unique ownership of the spinlock_priority_mutex, blocking the thread if the spinlo...
Definition spinlock_priority_mutex_tatas.hpp:68
bool try_lock(Priority_t const priority=0)
Try to acquire the unique ownership of the spinlock_priority_mutex, if successful will return true,...
Definition spinlock_priority_mutex_tatas.hpp:126
void unlock()
Release the spinlock_priority_mutex from unique ownership.
Definition spinlock_priority_mutex_tatas.hpp:105
#define BOOST_FAIRNESS_HARDWARE_DESTRUCTIVE_SIZE
Size to be aligned to avoid false sharing
Definition config.hpp:85
void spin_wait() noexcept
Definition wait_ops.hpp:56
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 wait operations used by the mutexes.