Skip to content

Commit 350787b

Browse files
committed
renamed ticket to phase in RwLockReadFuture
1 parent 157ca1f commit 350787b

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

futures-util/src/lock/rwlock.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<T: ?Sized> RwLock<T> {
162162
pub fn read(&self) -> RwLockReadFuture<'_, T> {
163163
RwLockReadFuture {
164164
rwlock: Some(self),
165-
ticket: None,
165+
phase: None,
166166
wait_key: WAIT_KEY_NONE,
167167
}
168168
}
@@ -236,27 +236,12 @@ impl<T: ?Sized> RwLock<T> {
236236
}
237237
}
238238

239-
#[derive(Debug)]
240-
enum Ticket {
241-
Read(usize),
242-
Write(usize),
243-
}
244-
245-
impl Ticket {
246-
fn value(&self) -> usize {
247-
match self {
248-
Ticket::Read(value) => *value,
249-
Ticket::Write(value) => *value,
250-
}
251-
}
252-
}
253-
254239
/// A future which resolves when the target read access lock has been successfully
255240
/// acquired.
256241
pub struct RwLockReadFuture<'a, T: ?Sized> {
257242
// `None` indicates that the mutex was successfully acquired.
258243
rwlock: Option<&'a RwLock<T>>,
259-
ticket: Option<Ticket>,
244+
phase: Option<usize>,
260245
wait_key: usize,
261246
}
262247

@@ -265,7 +250,7 @@ impl<T: ?Sized> fmt::Debug for RwLockReadFuture<'_, T> {
265250
f.debug_struct("RwLockReadFuture")
266251
.field("was_acquired", &self.rwlock.is_none())
267252
.field("rwlock", &self.rwlock)
268-
.field("ticket", &self.ticket)
253+
.field("phase", &self.phase)
269254
.field(
270255
"wait_key",
271256
&(if self.wait_key == WAIT_KEY_NONE {
@@ -292,19 +277,16 @@ impl<'a, T: ?Sized> Future for RwLockReadFuture<'a, T> {
292277
.rwlock
293278
.expect("polled RwLockReadFuture after completion");
294279

295-
// The ticket is defined by the write bits stored within the read-in count
296-
let ticket = self
297-
.ticket
298-
.get_or_insert_with(|| Ticket::Read(rwlock.atomic.reserve_reader()))
299-
.value();
280+
// The phase is defined by the write bits stored within the read-in count
281+
let phase = *self.phase.get_or_insert_with(|| rwlock.atomic.reserve_reader());
300282

301-
// Safe to create guard when either there are no writers (ticket == 0) or if
283+
// Safe to create guard when either there are no writers (phase == 0) or if
302284
// at least one of the two write bits change.
303285
// Writers always wait until the current reader phase completes before acquiring
304286
// the lock; thus the PHASE bit both maintains the read-write condition and
305287
// prevents deadlock in the case that this line isn't reached before a writer sets
306288
// the ONE_WRITER bit.
307-
if ticket == 0 || ticket != rwlock.atomic.phase() {
289+
if phase == 0 || phase != rwlock.atomic.phase() {
308290
if self.wait_key != WAIT_KEY_NONE {
309291
rwlock.readers.remove(self.wait_key);
310292
}
@@ -329,6 +311,12 @@ impl<T: ?Sized> Drop for RwLockReadFuture<'_, T> {
329311
}
330312
}
331313

314+
#[derive(Debug)]
315+
enum Ticket {
316+
Read(usize),
317+
Write(usize),
318+
}
319+
332320
/// A future which resolves when the target write access lock has been successfully
333321
/// acquired.
334322
pub struct RwLockWriteFuture<'a, T: ?Sized> {

0 commit comments

Comments
 (0)