Skip to content

Commit c4a4f48

Browse files
committed
Use compare_exchange_weak in futex rwlock implementation.
1 parent 1f2c2bb commit c4a4f48

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ impl RwLock {
7575

7676
#[inline]
7777
pub unsafe fn read(&self) {
78-
if !self.try_read() {
78+
let state = self.state.load(Relaxed);
79+
if !read_lockable(state)
80+
|| self
81+
.state
82+
.compare_exchange_weak(state, state + READ_LOCKED, Acquire, Relaxed)
83+
.is_err()
84+
{
7985
self.read_contended();
8086
}
8187
}
@@ -101,7 +107,8 @@ impl RwLock {
101107
loop {
102108
// If we can lock it, lock it.
103109
if read_lockable(state) {
104-
match self.state.compare_exchange(state, state + READ_LOCKED, Acquire, Relaxed) {
110+
match self.state.compare_exchange_weak(state, state + READ_LOCKED, Acquire, Relaxed)
111+
{
105112
Ok(_) => return, // Locked!
106113
Err(s) => {
107114
state = s;
@@ -140,7 +147,7 @@ impl RwLock {
140147

141148
#[inline]
142149
pub unsafe fn write(&self) {
143-
if !self.try_write() {
150+
if self.state.compare_exchange_weak(0, WRITE_LOCKED, Acquire, Relaxed).is_err() {
144151
self.write_contended();
145152
}
146153
}
@@ -165,7 +172,7 @@ impl RwLock {
165172
loop {
166173
// If it's unlocked, we try to lock it.
167174
if unlocked(state) {
168-
match self.state.compare_exchange(
175+
match self.state.compare_exchange_weak(
169176
state,
170177
state | WRITE_LOCKED | other_writers_waiting,
171178
Acquire,

0 commit comments

Comments
 (0)