blob: e9d9e121f9d5d9cba120fc488f6e942ba801159e (
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#pragma once
#ifndef RW_SPIN_LOCK_INL_H_
#error "Direct inclusion of this file is not allowed, include rw_spin_lock.h"
// For the sake of sane code completion.
#include "rw_spin_lock.h"
#endif
#undef RW_SPIN_LOCK_INL_H_
#include "spin_wait.h"
namespace NYT::NThreading {
////////////////////////////////////////////////////////////////////////////////
inline void TReaderWriterSpinLock::AcquireReader() noexcept
{
if (TryAcquireReader()) {
return;
}
AcquireReaderSlow();
}
inline void TReaderWriterSpinLock::AcquireReaderForkFriendly() noexcept
{
if (TryAcquireReaderForkFriendly()) {
return;
}
AcquireReaderForkFriendlySlow();
}
inline void TReaderWriterSpinLock::ReleaseReader() noexcept
{
auto prevValue = Value_.fetch_sub(ReaderDelta, std::memory_order_release);
Y_ASSERT((prevValue & ~WriterMask) != 0);
}
inline void TReaderWriterSpinLock::AcquireWriter() noexcept
{
if (TryAcquireWriter()) {
return;
}
AcquireWriterSlow();
}
inline void TReaderWriterSpinLock::ReleaseWriter() noexcept
{
auto prevValue = Value_.fetch_and(~WriterMask, std::memory_order_release);
Y_ASSERT(prevValue & WriterMask);
}
inline bool TReaderWriterSpinLock::IsLocked() const noexcept
{
return Value_.load() != UnlockedValue;
}
inline bool TReaderWriterSpinLock::IsLockedByReader() const noexcept
{
return Value_.load() >= ReaderDelta;
}
inline bool TReaderWriterSpinLock::IsLockedByWriter() const noexcept
{
return (Value_.load() & WriterMask) != 0;
}
inline bool TReaderWriterSpinLock::TryAcquireReader() noexcept
{
auto oldValue = Value_.fetch_add(ReaderDelta, std::memory_order_acquire);
if ((oldValue & WriterMask) != 0) {
Value_.fetch_sub(ReaderDelta, std::memory_order_relaxed);
return false;
}
return true;
}
inline bool TReaderWriterSpinLock::TryAndTryAcquireReader() noexcept
{
auto oldValue = Value_.load(std::memory_order_relaxed);
if ((oldValue & WriterMask) != 0) {
return false;
}
return TryAcquireReader();
}
inline bool TReaderWriterSpinLock::TryAcquireReaderForkFriendly() noexcept
{
auto oldValue = Value_.load(std::memory_order_relaxed);
if ((oldValue & WriterMask) != 0) {
return false;
}
auto newValue = oldValue + ReaderDelta;
return Value_.compare_exchange_weak(oldValue, newValue, std::memory_order_acquire);
}
inline bool TReaderWriterSpinLock::TryAcquireWriter() noexcept
{
auto expected = UnlockedValue;
return Value_.compare_exchange_weak(expected, WriterMask, std::memory_order_acquire);
}
inline bool TReaderWriterSpinLock::TryAndTryAcquireWriter() noexcept
{
auto oldValue = Value_.load(std::memory_order_relaxed);
if (oldValue != UnlockedValue) {
return false;
}
return TryAcquireWriter();
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
void TReaderSpinlockTraits<T>::Acquire(T* spinlock)
{
spinlock->AcquireReader();
}
template <class T>
void TReaderSpinlockTraits<T>::Release(T* spinlock)
{
spinlock->ReleaseReader();
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
void TForkFriendlyReaderSpinlockTraits<T>::Acquire(T* spinlock)
{
spinlock->AcquireReaderForkFriendly();
}
template <class T>
void TForkFriendlyReaderSpinlockTraits<T>::Release(T* spinlock)
{
spinlock->ReleaseReader();
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
void TWriterSpinlockTraits<T>::Acquire(T* spinlock)
{
spinlock->AcquireWriter();
}
template <class T>
void TWriterSpinlockTraits<T>::Release(T* spinlock)
{
spinlock->ReleaseWriter();
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
auto ReaderGuard(const T& lock)
{
return TReaderGuard<T>(lock);
}
template <class T>
auto ReaderGuard(const T* lock)
{
return TReaderGuard<T>(lock);
}
template <class T>
auto ForkFriendlyReaderGuard(const T& lock)
{
return TGuard<T, TForkFriendlyReaderSpinlockTraits<T>>(lock);
}
template <class T>
auto ForkFriendlyReaderGuard(const T* lock)
{
return TGuard<T, TForkFriendlyReaderSpinlockTraits<T>>(lock);
}
template <class T>
auto WriterGuard(const T& lock)
{
return TWriterGuard<T>(lock);
}
template <class T>
auto WriterGuard(const T* lock)
{
return TWriterGuard<T>(lock);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NThreading
|