Skip to content

Commit c7a529e

Browse files
yfeldblummeta-codesync[bot]
authored andcommitted
use canonical names for the states in RWSpinLock
Summary: Canonical names are: exclusive, shared, upgrade. Reviewed By: skrueger Differential Revision: D88033292 fbshipit-source-id: 7dc6c3abc0756a4ad07c548c3f1ad6e2a3a44838
1 parent 050652d commit c7a529e

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

third-party/folly/src/folly/synchronization/RWSpinLock.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace folly {
120120
* interfaces.
121121
*/
122122
class RWSpinLock {
123-
enum : int32_t { READER = 4, UPGRADED = 2, WRITER = 1 };
123+
enum : int32_t { SHARED = 4, UPGRADE = 2, EXCLUSIVE = 1 };
124124

125125
public:
126126
constexpr RWSpinLock() : bits_(0) {}
@@ -138,10 +138,10 @@ class RWSpinLock {
138138
}
139139
}
140140

141-
// Writer is responsible for clearing up both the UPGRADED and WRITER bits.
141+
// Writer is responsible for clearing up both the UPGRADE and EXCLUSIVE bits.
142142
void unlock() {
143-
static_assert(READER > WRITER + UPGRADED, "wrong bits!");
144-
bits_.fetch_and(~(WRITER | UPGRADED), std::memory_order_release);
143+
static_assert(SHARED > EXCLUSIVE + UPGRADE, "wrong bits!");
144+
bits_.fetch_and(~(EXCLUSIVE | UPGRADE), std::memory_order_release);
145145
}
146146

147147
// SharedLockable Concept
@@ -154,11 +154,11 @@ class RWSpinLock {
154154
}
155155
}
156156

157-
void unlock_shared() { bits_.fetch_add(-READER, std::memory_order_release); }
157+
void unlock_shared() { bits_.fetch_add(-SHARED, std::memory_order_release); }
158158

159159
// Downgrade the lock from writer status to reader status.
160160
void unlock_and_lock_shared() {
161-
bits_.fetch_add(READER, std::memory_order_acquire);
161+
bits_.fetch_add(SHARED, std::memory_order_acquire);
162162
unlock();
163163
}
164164

@@ -173,7 +173,7 @@ class RWSpinLock {
173173
}
174174

175175
void unlock_upgrade() {
176-
bits_.fetch_add(-UPGRADED, std::memory_order_acq_rel);
176+
bits_.fetch_add(-UPGRADE, std::memory_order_acq_rel);
177177
}
178178

179179
// unlock upgrade and try to acquire write lock
@@ -188,57 +188,57 @@ class RWSpinLock {
188188

189189
// unlock upgrade and read lock atomically
190190
void unlock_upgrade_and_lock_shared() {
191-
bits_.fetch_add(READER - UPGRADED, std::memory_order_acq_rel);
191+
bits_.fetch_add(SHARED - UPGRADE, std::memory_order_acq_rel);
192192
}
193193

194194
// write unlock and upgrade lock atomically
195195
void unlock_and_lock_upgrade() {
196-
// need to do it in two steps here -- as the UPGRADED bit might be OR-ed at
196+
// need to do it in two steps here -- as the UPGRADE bit might be OR-ed at
197197
// the same time when other threads are trying do try_lock_upgrade().
198-
bits_.fetch_or(UPGRADED, std::memory_order_acquire);
199-
bits_.fetch_add(-WRITER, std::memory_order_release);
198+
bits_.fetch_or(UPGRADE, std::memory_order_acquire);
199+
bits_.fetch_add(-EXCLUSIVE, std::memory_order_release);
200200
}
201201

202202
// Attempt to acquire writer permission. Return false if we didn't get it.
203203
bool try_lock() {
204204
int32_t expect = 0;
205205
return bits_.compare_exchange_strong(
206-
expect, WRITER, std::memory_order_acq_rel);
206+
expect, EXCLUSIVE, std::memory_order_acq_rel);
207207
}
208208

209209
// Try to get reader permission on the lock. This can fail if we
210210
// find out someone is a writer or upgrader.
211-
// Setting the UPGRADED bit would allow a writer-to-be to indicate
211+
// Setting the UPGRADE bit would allow a writer-to-be to indicate
212212
// its intention to write and block any new readers while waiting
213213
// for existing readers to finish and release their read locks. This
214214
// helps avoid starving writers (promoted from upgraders).
215215
bool try_lock_shared() {
216216
// fetch_add is considerably (100%) faster than compare_exchange,
217217
// so here we are optimizing for the common (lock success) case.
218-
int32_t value = bits_.fetch_add(READER, std::memory_order_acquire);
219-
if (FOLLY_UNLIKELY(value & (WRITER | UPGRADED))) {
220-
bits_.fetch_add(-READER, std::memory_order_release);
218+
int32_t value = bits_.fetch_add(SHARED, std::memory_order_acquire);
219+
if (FOLLY_UNLIKELY(value & (EXCLUSIVE | UPGRADE))) {
220+
bits_.fetch_add(-SHARED, std::memory_order_release);
221221
return false;
222222
}
223223
return true;
224224
}
225225

226226
// try to unlock upgrade and write lock atomically
227227
bool try_unlock_upgrade_and_lock() {
228-
int32_t expect = UPGRADED;
228+
int32_t expect = UPGRADE;
229229
return bits_.compare_exchange_strong(
230-
expect, WRITER, std::memory_order_acq_rel);
230+
expect, EXCLUSIVE, std::memory_order_acq_rel);
231231
}
232232

233233
// try to acquire an upgradable lock.
234234
bool try_lock_upgrade() {
235-
int32_t value = bits_.fetch_or(UPGRADED, std::memory_order_acquire);
235+
int32_t value = bits_.fetch_or(UPGRADE, std::memory_order_acquire);
236236

237-
// Note: when failed, we cannot flip the UPGRADED bit back,
237+
// Note: when failed, we cannot flip the UPGRADE bit back,
238238
// as in this case there is either another upgrade lock or a write lock.
239239
// If it's a write lock, the bit will get cleared up when that lock's done
240240
// with unlock().
241-
return ((value & (UPGRADED | WRITER)) == 0);
241+
return ((value & (UPGRADE | EXCLUSIVE)) == 0);
242242
}
243243

244244
// mainly for debugging purposes.

0 commit comments

Comments
 (0)