Skip to content

Commit 45e044f

Browse files
committed
Relax even more
1 parent 3208bb5 commit 45e044f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

futures-util/src/lock/bilock.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::pin::Pin;
1010
#[cfg(feature = "bilock")]
1111
use core::ptr;
1212
use core::sync::atomic::AtomicU8;
13-
use core::sync::atomic::Ordering::{Acquire, AcqRel};
13+
use core::sync::atomic::Ordering::{Acquire, AcqRel, Relaxed, Release};
1414
#[cfg(feature = "bilock")]
1515
use futures_core::future::Future;
1616
use futures_core::task::{Context, Poll, Waker};
@@ -160,10 +160,12 @@ impl<T> BiLock<T> {
160160
/// task.
161161
pub fn poll_lock(&mut self, cx: &mut Context<'_>) -> Poll<BiLockGuard<'_, T>> {
162162
assert_ne!(self.token, TOKEN_LOCK);
163-
self.token = self.arc.token.swap(self.token, if self.token == TOKEN_WAKE { AcqRel } else { Acquire });
163+
let arc_token = &self.arc.token;
164+
self.token = arc_token.swap(self.token, if self.token == TOKEN_WAKE { Release } else { Relaxed });
164165
match self.token {
165166
TOKEN_NULL => { }
166167
TOKEN_WAKE => {
168+
arc_token.load(Acquire);
167169
let our_waker = cx.waker();
168170
// SAFETY: we own the wake token, so we have exclusive access to this field.
169171
// The mutable reference we create goes out of scope before we give up the
@@ -179,17 +181,19 @@ impl<T> BiLock<T> {
179181
}
180182
}
181183
TOKEN_LOCK => {
184+
arc_token.load(Acquire);
182185
return Poll::Ready(BiLockGuard { bilock: self });
183186
}
184187
_ => unreachable!(),
185188
}
186189
// We only need Release if we previously stored our waker
187-
self.token = self.arc.token.swap(self.token, if self.token == TOKEN_WAKE { AcqRel } else { Acquire });
190+
self.token = arc_token.swap(self.token, if self.token == TOKEN_WAKE { Release } else { Relaxed });
188191
match self.token {
189192
TOKEN_NULL => {
190193
return Poll::Pending;
191194
}
192195
TOKEN_WAKE => {
196+
arc_token.load(Acquire);
193197
let our_waker = cx.waker();
194198
// SAFETY: we own the wake token, so we have exclusive access to this field.
195199
// The mutable reference we create goes out of scope before we give up the
@@ -205,17 +209,19 @@ impl<T> BiLock<T> {
205209
}
206210
}
207211
TOKEN_LOCK => {
212+
arc_token.load(Acquire);
208213
return Poll::Ready(BiLockGuard { bilock: self });
209214
}
210215
_ => unreachable!(),
211216
}
212-
self.token = self.arc.token.swap(self.token, AcqRel);
217+
self.token = arc_token.swap(self.token, Release);
213218
match self.token {
214219
TOKEN_NULL => {
215-
return Poll::Pending;
220+
Poll::Pending
216221
}
217222
TOKEN_LOCK => {
218-
return Poll::Ready(BiLockGuard { bilock: self });
223+
arc_token.load(Acquire);
224+
Poll::Ready(BiLockGuard { bilock: self })
219225
}
220226
_ => unreachable!(),
221227
}
@@ -255,10 +261,12 @@ impl<T> BiLock<T> {
255261

256262
fn unlock(&mut self) {
257263
assert_eq!(self.token, TOKEN_LOCK);
258-
self.token = self.arc.token.swap(self.token, AcqRel);
264+
let arc_token = self.arc.token;
265+
self.token = self.arc.token.swap(self.token, Release);
259266
match self.token {
260267
TOKEN_NULL => {} // lock uncontended
261268
TOKEN_WAKE => {
269+
arc_token.load(Acquire);
262270
// SAFETY: we own the wake token, so we have exclusive access to this field.
263271
if let Some((left, wake)) = unsafe { &mut *self.arc.waker.get() } {
264272
if self.left != *left {

0 commit comments

Comments
 (0)