@@ -27,33 +27,33 @@ const MAX_READERS: i32 = MASK - 1;
27
27
const READERS_WAITING : i32 = 1 << 30 ;
28
28
const WRITERS_WAITING : i32 = 1 << 31 ;
29
29
30
- fn unlocked ( state : i32 ) -> bool {
30
+ fn is_unlocked ( state : i32 ) -> bool {
31
31
state & MASK == 0
32
32
}
33
33
34
- fn write_locked ( state : i32 ) -> bool {
34
+ fn is_write_locked ( state : i32 ) -> bool {
35
35
state & MASK == WRITE_LOCKED
36
36
}
37
37
38
- fn readers_waiting ( state : i32 ) -> bool {
38
+ fn has_readers_waiting ( state : i32 ) -> bool {
39
39
state & READERS_WAITING != 0
40
40
}
41
41
42
- fn writers_waiting ( state : i32 ) -> bool {
42
+ fn has_writers_waiting ( state : i32 ) -> bool {
43
43
state & WRITERS_WAITING != 0
44
44
}
45
45
46
- fn read_lockable ( state : i32 ) -> bool {
46
+ fn is_read_lockable ( state : i32 ) -> bool {
47
47
// This also returns false if the counter could overflow if we tried to read lock it.
48
48
//
49
49
// We don't allow read-locking if there's readers waiting, even if the lock is unlocked
50
50
// and there's no writers waiting. The only situation when this happens is after unlocking,
51
51
// at which point the unlocking thread might be waking up writers, which have priority over readers.
52
52
// 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)
54
54
}
55
55
56
- fn reached_max_readers ( state : i32 ) -> bool {
56
+ fn has_reached_max_readers ( state : i32 ) -> bool {
57
57
state & MASK == MAX_READERS
58
58
}
59
59
@@ -69,14 +69,14 @@ impl RwLock {
69
69
#[ inline]
70
70
pub unsafe fn try_read ( & self ) -> bool {
71
71
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 ) )
73
73
. is_ok ( )
74
74
}
75
75
76
76
#[ inline]
77
77
pub unsafe fn read ( & self ) {
78
78
let state = self . state . load ( Relaxed ) ;
79
- if !read_lockable ( state)
79
+ if !is_read_lockable ( state)
80
80
|| self
81
81
. state
82
82
. compare_exchange_weak ( state, state + READ_LOCKED , Acquire , Relaxed )
@@ -92,10 +92,10 @@ impl RwLock {
92
92
93
93
// It's impossible for a reader to be waiting on a read-locked RwLock,
94
94
// 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) ) ;
96
96
97
97
// 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) {
99
99
self . wake_writer_or_readers ( state) ;
100
100
}
101
101
}
@@ -106,7 +106,7 @@ impl RwLock {
106
106
107
107
loop {
108
108
// If we can lock it, lock it.
109
- if read_lockable ( state) {
109
+ if is_read_lockable ( state) {
110
110
match self . state . compare_exchange_weak ( state, state + READ_LOCKED , Acquire , Relaxed )
111
111
{
112
112
Ok ( _) => return , // Locked!
@@ -118,12 +118,12 @@ impl RwLock {
118
118
}
119
119
120
120
// Check for overflow.
121
- if reached_max_readers ( state) {
121
+ if has_reached_max_readers ( state) {
122
122
panic ! ( "too many active read locks on RwLock" ) ;
123
123
}
124
124
125
125
// Make sure the readers waiting bit is set before we go to sleep.
126
- if !readers_waiting ( state) {
126
+ if !has_readers_waiting ( state) {
127
127
if let Err ( s) =
128
128
self . state . compare_exchange ( state, state | READERS_WAITING , Relaxed , Relaxed )
129
129
{
@@ -142,7 +142,9 @@ impl RwLock {
142
142
143
143
#[ inline]
144
144
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 ( )
146
148
}
147
149
148
150
#[ inline]
@@ -156,9 +158,9 @@ impl RwLock {
156
158
pub unsafe fn write_unlock ( & self ) {
157
159
let state = self . state . fetch_sub ( WRITE_LOCKED , Release ) - WRITE_LOCKED ;
158
160
159
- debug_assert ! ( unlocked ( state) ) ;
161
+ debug_assert ! ( is_unlocked ( state) ) ;
160
162
161
- if writers_waiting ( state) || readers_waiting ( state) {
163
+ if has_writers_waiting ( state) || has_readers_waiting ( state) {
162
164
self . wake_writer_or_readers ( state) ;
163
165
}
164
166
}
@@ -171,7 +173,7 @@ impl RwLock {
171
173
172
174
loop {
173
175
// If it's unlocked, we try to lock it.
174
- if unlocked ( state) {
176
+ if is_unlocked ( state) {
175
177
match self . state . compare_exchange_weak (
176
178
state,
177
179
state | WRITE_LOCKED | other_writers_waiting,
@@ -187,7 +189,7 @@ impl RwLock {
187
189
}
188
190
189
191
// Set the waiting bit indicating that we're waiting on it.
190
- if !writers_waiting ( state) {
192
+ if !has_writers_waiting ( state) {
191
193
if let Err ( s) =
192
194
self . state . compare_exchange ( state, state | WRITERS_WAITING , Relaxed , Relaxed )
193
195
{
@@ -207,7 +209,7 @@ impl RwLock {
207
209
// Don't go to sleep if the lock has become available,
208
210
// or if the writers waiting bit is no longer set.
209
211
let s = self . state . load ( Relaxed ) ;
210
- if unlocked ( state) || !writers_waiting ( s) {
212
+ if is_unlocked ( state) || !has_writers_waiting ( s) {
211
213
state = s;
212
214
continue ;
213
215
}
@@ -226,7 +228,7 @@ impl RwLock {
226
228
/// back to waking up readers if there was no writer to wake up.
227
229
#[ cold]
228
230
fn wake_writer_or_readers ( & self , mut state : i32 ) {
229
- assert ! ( unlocked ( state) ) ;
231
+ assert ! ( is_unlocked ( state) ) ;
230
232
231
233
// The readers waiting bit might be turned on at any point now,
232
234
// since readers will block when there's anything waiting.
@@ -299,13 +301,13 @@ impl RwLock {
299
301
300
302
fn spin_write ( & self ) -> i32 {
301
303
// 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) )
303
305
}
304
306
305
307
fn spin_read ( & self ) -> i32 {
306
308
// Stop spinning when it's unlocked or read locked, or when there's waiting threads.
307
309
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)
309
311
} )
310
312
}
311
313
}
0 commit comments