-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDekkersAlgorithm.cpp
47 lines (41 loc) · 1.39 KB
/
DekkersAlgorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "DekkersAlgorithm.h"
void DekkersAlgorithm::lock(int Slot) {
WantsToEnter[Slot] = true;
const int Other = 1 - Slot;
while (WantsToEnter[Other]) {
if (Turn != Slot) {
WantsToEnter[Slot] = false;
while (Turn != Slot) {
// busy wait until it is our turn
}
WantsToEnter[Slot] = true;
}
}
}
void DekkersAlgorithm::unlock(int Slot) {
const int Other = 1 - Slot;
Turn = Other;
WantsToEnter[Slot] = false;
}
/// @see "Anthony Williams: Implementing Dekker's algorithm with Fences"
void DekkersAlgorithmFences::lock(int Slot) {
WantsToEnter[Slot].store(true, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_seq_cst);
const auto Other = 1 - Slot;
while (WantsToEnter[Other].load(std::memory_order_relaxed)) {
if (Turn.load(std::memory_order_relaxed) != Slot) {
WantsToEnter[Slot].store(false, std::memory_order_relaxed);
while (Turn.load(std::memory_order_relaxed) != Slot) {
}
WantsToEnter[Slot].store(true, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_seq_cst);
}
}
std::atomic_thread_fence(std::memory_order_acquire);
}
void DekkersAlgorithmFences::unlock(int Slot) {
const auto Other = 1 - Slot;
Turn.store(Other, std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
WantsToEnter[Slot].store(false, std::memory_order_relaxed);
}