Skip to content

Commit 8339381

Browse files
committed
Use is_ or has_ prefix for pure -> bool functions.
1 parent c4a4f48 commit 8339381

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

Diff for: library/std/src/sys/unix/locks/futex_rwlock.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,33 @@ const MAX_READERS: i32 = MASK - 1;
2727
const READERS_WAITING: i32 = 1 << 30;
2828
const WRITERS_WAITING: i32 = 1 << 31;
2929

30-
fn unlocked(state: i32) -> bool {
30+
fn is_unlocked(state: i32) -> bool {
3131
state & MASK == 0
3232
}
3333

34-
fn write_locked(state: i32) -> bool {
34+
fn is_write_locked(state: i32) -> bool {
3535
state & MASK == WRITE_LOCKED
3636
}
3737

38-
fn readers_waiting(state: i32) -> bool {
38+
fn has_readers_waiting(state: i32) -> bool {
3939
state & READERS_WAITING != 0
4040
}
4141

42-
fn writers_waiting(state: i32) -> bool {
42+
fn has_writers_waiting(state: i32) -> bool {
4343
state & WRITERS_WAITING != 0
4444
}
4545

46-
fn read_lockable(state: i32) -> bool {
46+
fn is_read_lockable(state: i32) -> bool {
4747
// This also returns false if the counter could overflow if we tried to read lock it.
4848
//
4949
// We don't allow read-locking if there's readers waiting, even if the lock is unlocked
5050
// and there's no writers waiting. The only situation when this happens is after unlocking,
5151
// at which point the unlocking thread might be waking up writers, which have priority over readers.
5252
// The unlocking thread will clear the readers waiting bit and wake up readers, if necssary.
53-
state & MASK < MAX_READERS && !readers_waiting(state) && !writers_waiting(state)
53+
state & MASK < MAX_READERS && !has_readers_waiting(state) && !has_writers_waiting(state)
5454
}
5555

56-
fn reached_max_readers(state: i32) -> bool {
56+
fn has_reached_max_readers(state: i32) -> bool {
5757
state & MASK == MAX_READERS
5858
}
5959

@@ -69,14 +69,14 @@ impl RwLock {
6969
#[inline]
7070
pub unsafe fn try_read(&self) -> bool {
7171
self.state
72-
.fetch_update(Acquire, Relaxed, |s| read_lockable(s).then(|| s + READ_LOCKED))
72+
.fetch_update(Acquire, Relaxed, |s| is_read_lockable(s).then(|| s + READ_LOCKED))
7373
.is_ok()
7474
}
7575

7676
#[inline]
7777
pub unsafe fn read(&self) {
7878
let state = self.state.load(Relaxed);
79-
if !read_lockable(state)
79+
if !is_read_lockable(state)
8080
|| self
8181
.state
8282
.compare_exchange_weak(state, state + READ_LOCKED, Acquire, Relaxed)
@@ -92,10 +92,10 @@ impl RwLock {
9292

9393
// It's impossible for a reader to be waiting on a read-locked RwLock,
9494
// except if there is also a writer waiting.
95-
debug_assert!(!readers_waiting(state) || writers_waiting(state));
95+
debug_assert!(!has_readers_waiting(state) || has_writers_waiting(state));
9696

9797
// Wake up a writer if we were the last reader and there's a writer waiting.
98-
if unlocked(state) && writers_waiting(state) {
98+
if is_unlocked(state) && has_writers_waiting(state) {
9999
self.wake_writer_or_readers(state);
100100
}
101101
}
@@ -106,7 +106,7 @@ impl RwLock {
106106

107107
loop {
108108
// If we can lock it, lock it.
109-
if read_lockable(state) {
109+
if is_read_lockable(state) {
110110
match self.state.compare_exchange_weak(state, state + READ_LOCKED, Acquire, Relaxed)
111111
{
112112
Ok(_) => return, // Locked!
@@ -118,12 +118,12 @@ impl RwLock {
118118
}
119119

120120
// Check for overflow.
121-
if reached_max_readers(state) {
121+
if has_reached_max_readers(state) {
122122
panic!("too many active read locks on RwLock");
123123
}
124124

125125
// Make sure the readers waiting bit is set before we go to sleep.
126-
if !readers_waiting(state) {
126+
if !has_readers_waiting(state) {
127127
if let Err(s) =
128128
self.state.compare_exchange(state, state | READERS_WAITING, Relaxed, Relaxed)
129129
{
@@ -142,7 +142,9 @@ impl RwLock {
142142

143143
#[inline]
144144
pub unsafe fn try_write(&self) -> bool {
145-
self.state.fetch_update(Acquire, Relaxed, |s| unlocked(s).then(|| s + WRITE_LOCKED)).is_ok()
145+
self.state
146+
.fetch_update(Acquire, Relaxed, |s| is_unlocked(s).then(|| s + WRITE_LOCKED))
147+
.is_ok()
146148
}
147149

148150
#[inline]
@@ -156,9 +158,9 @@ impl RwLock {
156158
pub unsafe fn write_unlock(&self) {
157159
let state = self.state.fetch_sub(WRITE_LOCKED, Release) - WRITE_LOCKED;
158160

159-
debug_assert!(unlocked(state));
161+
debug_assert!(is_unlocked(state));
160162

161-
if writers_waiting(state) || readers_waiting(state) {
163+
if has_writers_waiting(state) || has_readers_waiting(state) {
162164
self.wake_writer_or_readers(state);
163165
}
164166
}
@@ -171,7 +173,7 @@ impl RwLock {
171173

172174
loop {
173175
// If it's unlocked, we try to lock it.
174-
if unlocked(state) {
176+
if is_unlocked(state) {
175177
match self.state.compare_exchange_weak(
176178
state,
177179
state | WRITE_LOCKED | other_writers_waiting,
@@ -187,7 +189,7 @@ impl RwLock {
187189
}
188190

189191
// Set the waiting bit indicating that we're waiting on it.
190-
if !writers_waiting(state) {
192+
if !has_writers_waiting(state) {
191193
if let Err(s) =
192194
self.state.compare_exchange(state, state | WRITERS_WAITING, Relaxed, Relaxed)
193195
{
@@ -207,7 +209,7 @@ impl RwLock {
207209
// Don't go to sleep if the lock has become available,
208210
// or if the writers waiting bit is no longer set.
209211
let s = self.state.load(Relaxed);
210-
if unlocked(state) || !writers_waiting(s) {
212+
if is_unlocked(state) || !has_writers_waiting(s) {
211213
state = s;
212214
continue;
213215
}
@@ -226,7 +228,7 @@ impl RwLock {
226228
/// back to waking up readers if there was no writer to wake up.
227229
#[cold]
228230
fn wake_writer_or_readers(&self, mut state: i32) {
229-
assert!(unlocked(state));
231+
assert!(is_unlocked(state));
230232

231233
// The readers waiting bit might be turned on at any point now,
232234
// since readers will block when there's anything waiting.
@@ -299,13 +301,13 @@ impl RwLock {
299301

300302
fn spin_write(&self) -> i32 {
301303
// Stop spinning when it's unlocked or when there's waiting writers, to keep things somewhat fair.
302-
self.spin_until(|state| unlocked(state) || writers_waiting(state))
304+
self.spin_until(|state| is_unlocked(state) || has_writers_waiting(state))
303305
}
304306

305307
fn spin_read(&self) -> i32 {
306308
// Stop spinning when it's unlocked or read locked, or when there's waiting threads.
307309
self.spin_until(|state| {
308-
!write_locked(state) || readers_waiting(state) || writers_waiting(state)
310+
!is_write_locked(state) || has_readers_waiting(state) || has_writers_waiting(state)
309311
})
310312
}
311313
}

0 commit comments

Comments
 (0)