-
Notifications
You must be signed in to change notification settings - Fork 159
/
bt2_locks.cpp
36 lines (30 loc) · 920 Bytes
/
bt2_locks.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
#include "bt2_locks.h"
void mcs_lock::lock(mcs_node &node) {
node.next = nullptr;
mcs_node *pred = q.exchange(&node, std::memory_order_acq_rel);
if (pred) {
node.unlocked = false;
pred->next.store(&node, std::memory_order_release);
spin_while_eq(node.unlocked, false);
}
node.unlocked.load(std::memory_order_acquire);
}
void mcs_lock::unlock(mcs_node &node) {
if (!node.next.load(std::memory_order_acquire)) {
mcs_node *node_ptr = &node;
if (q.compare_exchange_strong(node_ptr,
(mcs_node *)nullptr,
std::memory_order_release))
return;
spin_while_eq(node.next, (mcs_node *)nullptr);
}
node.next.load(std::memory_order_acquire)->unlocked.store(true, std::memory_order_release);
}
void spin_lock::lock() {
cpu_backoff backoff;
while (flag.test_and_set(std::memory_order_acquire))
backoff.pause();
}
void spin_lock::unlock() {
flag.clear(std::memory_order_release);
}