fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
coherent_priority_lock.hpp
Go to the documentation of this file.
1
14#ifndef BOOST_FAIRNESS_COHERENT_PRIORITY_LOCK_HPP
15#define BOOST_FAIRNESS_COHERENT_PRIORITY_LOCK_HPP
16
17#include <atomic>
23
24
26
27 enum class WaitMechanism{
28 Spin,
29 Wait
30 };
31
32 template<WaitMechanism W = WaitMechanism::Spin>
34
35 public:
36
38
39 void initialize(Request* const firstTail){
40
41 firstTail->state_ = GRANTED;
42
43 tail_.store(firstTail);
44
45 head_.store(firstTail);
46
47 }
48
49 void requestLock(Thread* const requester){
50 requester->watch_ = tail_.exchange(requester->request_, std::memory_order_acquire);
51 requester->watch_->watcher_ = requester;
52 for(;;){
53 if (requester->watch_->state_.load(std::memory_order_acquire) == GRANTED){
54 return;
55 }
56 if constexpr (W == WaitMechanism::Wait)
57 wait(requester->watch_->state_, PENDING);
58 else
59 spin_wait(requester->watch_->state_, PENDING);
60 }
61 }
62
63 void grantLock(Thread* const requester) {
64 Priority_t localHighestPriority{BOOST_FAIRNESS_MAXIMUM_PRIORITY};
65 Thread* currentThread;
66 Request* localHighestPriorityReq;
67
68 requester->request_->thread_ = requester->watch_->thread_;
69
70 if (requester->request_->thread_ != nullptr)
71 requester->request_->thread_->request_ = (requester->request_);
72 else
73 head_.store(requester->request_);
74
75 localHighestPriorityReq = head_.load();
76 currentThread = head_.load()->watcher_;
77
78 while (currentThread != nullptr){
79 if (currentThread->priority_ < localHighestPriority){
80 localHighestPriority = currentThread->priority_;
81 localHighestPriorityReq = currentThread->watch_;
82 }
83 currentThread = currentThread->request_->watcher_;
84 }
85
86 localHighestPriorityReq->state_.store(GRANTED, std::memory_order_release);
87
88 if constexpr (W == WaitMechanism::Wait)
89 notify_one(localHighestPriorityReq->state_);
90
91 requester->request_ = requester->watch_;
92 requester->request_->thread_ = requester;
93
94 }
95
96 private:
97 std::atomic<Request*> tail_{nullptr};
98 std::atomic<Request*> head_{nullptr};
99
100 };
101
102 #undef PENDING
103 #undef GRANTED
104}
105#endif // BOOST_FAIRNESS_COHERENT_PRIORITY_LOCK_HPP
Definition coherent_priority_lock.hpp:33
void requestLock(Thread *const requester)
Definition coherent_priority_lock.hpp:49
void grantLock(Thread *const requester)
Definition coherent_priority_lock.hpp:63
void initialize(Request *const firstTail)
Definition coherent_priority_lock.hpp:39
This file contains configurations about boost and 128bit cpu support. TODO.
Definition coherent_priority_lock.hpp:25
void wait(T &mem, K const expected) noexcept
Definition wait_ops.hpp:69
WaitMechanism
Definition coherent_priority_lock.hpp:27
void spin_wait() noexcept
Definition wait_ops.hpp:56
void notify_one(T &mem) noexcept
Definition wait_ops.hpp:103
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 a static very fast and simple request pool.
#define PENDING
Definition request_pool.hpp:20
#define GRANTED
Definition request_pool.hpp:21
Definition request_pool.hpp:25
std::atomic< uint32_t > state_
Definition request_pool.hpp:28
Thread * watcher_
Definition request_pool.hpp:26
Thread * thread_
Definition request_pool.hpp:27
Definition thread_pool.hpp:24
Request * request_
Definition thread_pool.hpp:40
Request * watch_
Definition thread_pool.hpp:39
Priority_t priority_
Definition thread_pool.hpp:42
This file contains the implementation of a pool of thread structs.
This file contains the implementation of the wait operations used by the mutexes.