blob: 3fbe8a6441b74e16779b6da1a48509b145b9e43a (
plain) (
blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#pragma once
#include "spin_wait.h"
#include <atomic>
class TSpinLockBase {
protected:
TSpinLockBase() = default;
// These were unearthed in IGNIETFERRO-1105
// Need to get rid of them separately
TSpinLockBase(const TSpinLockBase& other)
: Val_(other.Val_.load())
{
}
TSpinLockBase& operator=(const TSpinLockBase& other)
{
Val_.store(other.Val_);
return *this;
}
public:
inline bool IsLocked() const noexcept {
return Val_.load();
}
inline bool TryAcquire() noexcept {
intptr_t zero = 0;
return Val_.compare_exchange_strong(zero, 1);
}
inline bool try_lock() noexcept {
return TryAcquire();
}
protected:
std::atomic<intptr_t> Val_{0};
};
static inline void SpinLockPause() {
#if defined(__GNUC__)
#if defined(_i386_) || defined(_x86_64_)
__asm __volatile("pause");
#elif defined(_arm64_)
__asm __volatile("yield" ::
: "memory");
#endif
#endif
}
/*
* You should almost always use TAdaptiveLock instead of TSpinLock
*/
class TSpinLock: public TSpinLockBase {
public:
using TSpinLockBase::TSpinLockBase;
inline void Release() noexcept {
Val_.store(0, std::memory_order_release);
}
inline void Acquire() noexcept {
intptr_t zero = 0;
if (Val_.compare_exchange_strong(zero, 1)) {
return;
}
do {
SpinLockPause();
zero = 0;
} while (Val_.load(std::memory_order_acquire) != 0 ||
!Val_.compare_exchange_strong(zero, 1));
}
inline void unlock() noexcept {
Release();
}
inline void lock() noexcept {
Acquire();
}
};
class TAdaptiveLock: public TSpinLockBase {
public:
using TSpinLockBase::TSpinLockBase;
void Release() noexcept {
Val_.store(0, std::memory_order_release);
}
void Acquire() noexcept {
intptr_t zero = 0;
if (Val_.compare_exchange_strong(zero, 1)) {
return;
}
TSpinWait sw;
for (;;) {
zero = 0;
if (Val_.load(std::memory_order_acquire) == 0 &&
Val_.compare_exchange_strong(zero, 1)) {
break;
}
sw.Sleep();
}
}
inline void unlock() noexcept {
Release();
}
inline void lock() noexcept {
Acquire();
}
};
|