@@ -75,7 +75,13 @@ impl RwLock {
75
75
76
76
#[ inline]
77
77
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
+ {
79
85
self . read_contended ( ) ;
80
86
}
81
87
}
@@ -101,7 +107,8 @@ impl RwLock {
101
107
loop {
102
108
// If we can lock it, lock it.
103
109
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
+ {
105
112
Ok ( _) => return , // Locked!
106
113
Err ( s) => {
107
114
state = s;
@@ -140,7 +147,7 @@ impl RwLock {
140
147
141
148
#[ inline]
142
149
pub unsafe fn write ( & self ) {
143
- if ! self . try_write ( ) {
150
+ if self . state . compare_exchange_weak ( 0 , WRITE_LOCKED , Acquire , Relaxed ) . is_err ( ) {
144
151
self . write_contended ( ) ;
145
152
}
146
153
}
@@ -165,7 +172,7 @@ impl RwLock {
165
172
loop {
166
173
// If it's unlocked, we try to lock it.
167
174
if unlocked ( state) {
168
- match self . state . compare_exchange (
175
+ match self . state . compare_exchange_weak (
169
176
state,
170
177
state | WRITE_LOCKED | other_writers_waiting,
171
178
Acquire ,
0 commit comments