1
1
use crate :: loom:: cell:: UnsafeCell ;
2
2
use crate :: loom:: future:: AtomicWaker ;
3
3
use crate :: loom:: sync:: atomic:: AtomicUsize ;
4
- use crate :: loom:: sync:: { Arc , Weak } ;
4
+ use crate :: loom:: sync:: Arc ;
5
5
use crate :: park:: thread:: CachedParkThread ;
6
6
use crate :: park:: Park ;
7
7
use crate :: sync:: mpsc:: error:: TryRecvError ;
@@ -11,9 +11,11 @@ use crate::sync::notify::Notify;
11
11
use std:: fmt;
12
12
use std:: mem;
13
13
use std:: process;
14
- use std:: sync:: atomic:: Ordering :: { AcqRel , Acquire , Relaxed , SeqCst } ;
14
+ use std:: sync:: atomic:: Ordering :: { AcqRel , Acquire , Relaxed , Release } ;
15
+ use std:: sync:: Weak ;
15
16
use std:: task:: Poll :: { Pending , Ready } ;
16
17
use std:: task:: { Context , Poll } ;
18
+ use std:: usize;
17
19
18
20
/// Channel sender.
19
21
pub ( crate ) struct Tx < T , S > {
@@ -181,28 +183,31 @@ impl<T, S> TxWeak<T, S> {
181
183
// even though the channel might have been closed in the meantime.
182
184
// Need to check here whether the channel was actually closed.
183
185
184
- let mut tx_count = inner. tx_count . load ( Relaxed ) ;
186
+ let mut tx_count = inner. tx_count . load ( Acquire ) ;
187
+
188
+ if tx_count == 0 {
189
+ // channel is closed
190
+ mem:: drop ( inner) ;
191
+ return None ;
192
+ }
193
+
185
194
loop {
186
- // FIXME Haven't thought the orderings on the CAS through yet
187
195
match inner
188
196
. tx_count
189
- . compare_exchange ( tx_count, tx_count + 1 , SeqCst , SeqCst )
197
+ . compare_exchange ( tx_count, tx_count + 1 , AcqRel , Acquire )
190
198
{
191
199
Ok ( prev_count) => {
192
- if prev_count == 0 {
193
- mem:: drop ( inner) ;
194
- return None ;
195
- }
200
+ assert ! ( prev_count != 0 ) ;
196
201
197
202
return Some ( Tx :: new ( inner) ) ;
198
203
}
199
- Err ( count ) => {
200
- if count == 0 {
204
+ Err ( prev_count ) => {
205
+ if prev_count == 0 {
201
206
mem:: drop ( inner) ;
202
207
return None ;
203
208
}
204
209
205
- tx_count = count ;
210
+ tx_count = prev_count ;
206
211
}
207
212
}
208
213
}
@@ -441,9 +446,6 @@ impl Semaphore for (crate::sync::batch_semaphore::Semaphore, usize) {
441
446
442
447
// ===== impl Semaphore for AtomicUsize =====
443
448
444
- use std:: sync:: atomic:: Ordering :: Release ;
445
- use std:: usize;
446
-
447
449
impl Semaphore for AtomicUsize {
448
450
fn add_permit ( & self ) {
449
451
let prev = self . fetch_sub ( 2 , Release ) ;
0 commit comments