-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_locks.hpp
52 lines (46 loc) · 1.5 KB
/
custom_locks.hpp
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
48
49
50
51
#ifndef CUSTOM_LOCKS_HPP
#define CUSTOM_LOCKS_HPP
#include <atomic>
struct spin_mutex {
private:
std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
constexpr auto lock() noexcept -> void {
while (flag.test_and_set(std::memory_order_acquire)) { flag.wait(true, std::memory_order_relaxed); }
}
constexpr auto unlock() noexcept -> void {
flag.clear(std::memory_order_release);
flag.notify_one();
}
constexpr auto try_lock() noexcept -> bool {
return !flag.test_and_set(std::memory_order_acquire);
}
};
struct ticket_mutex {
private:
alignas(std::hardware_destructive_interference_size) std::atomic<int> in{0};
alignas(std::hardware_destructive_interference_size) std::atomic<int> out{0};
public:
constexpr auto lock() noexcept -> void {
auto const my = in.fetch_add(1, std::memory_order_acquire);
while (true) {
auto const now = out.load(std::memory_order_acquire);
if (now == my) return;
out.wait(now, std::memory_order_relaxed);
}
}
constexpr auto unlock() noexcept -> void {
out.fetch_add(1, std::memory_order_release);
out.notify_all();
}
constexpr auto try_lock() noexcept -> bool {
auto const my = in.load(std::memory_order_acquire);
auto const now = out.load(std::memory_order_acquire);
if (now == my) {
in.fetch_add(1, std::memory_order_acquire);
return true;
}
return false;
}
};
#endif