Skip to content

Commit 6873487

Browse files
committed
Remove has_blocked
Because we will usually have at least one blocked task when we notify, so adding an atomic boolean will probably just add additinal overhead.
1 parent 385ae68 commit 6873487

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

src/sync/condvar.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::pin::Pin;
2-
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
2+
use std::sync::atomic::{AtomicUsize, Ordering};
33
use std::sync::Arc;
44
use std::time::Duration;
55

@@ -61,7 +61,6 @@ impl WaitTimeoutResult {
6161
/// ```
6262
#[derive(Debug)]
6363
pub struct Condvar {
64-
has_blocked: AtomicBool,
6564
blocked: std::sync::Mutex<Slab<WaitEntry>>,
6665
}
6766

@@ -89,7 +88,6 @@ impl Condvar {
8988
/// ```
9089
pub fn new() -> Self {
9190
Condvar {
92-
has_blocked: AtomicBool::new(false),
9391
blocked: std::sync::Mutex::new(Slab::new()),
9492
}
9593
}
@@ -275,10 +273,8 @@ impl Condvar {
275273
/// # }) }
276274
/// ```
277275
pub fn notify_one(&self) {
278-
if self.has_blocked.load(Ordering::Acquire) {
279-
let blocked = self.blocked.lock().unwrap();
280-
notify(blocked, false);
281-
}
276+
let blocked = self.blocked.lock().unwrap();
277+
notify(blocked, false);
282278
}
283279

284280
/// Wakes up all blocked tasks on this condvar.
@@ -314,10 +310,8 @@ impl Condvar {
314310
/// # }) }
315311
/// ```
316312
pub fn notify_all(&self) {
317-
if self.has_blocked.load(Ordering::Acquire) {
318-
let blocked = self.blocked.lock().unwrap();
319-
notify(blocked, true);
320-
}
313+
let blocked = self.blocked.lock().unwrap();
314+
notify(blocked, true);
321315
}
322316
}
323317

@@ -355,9 +349,6 @@ impl<'a, 'b, T> Future for AwaitNotify<'a, 'b, T> {
355349
waker: Some(w),
356350
}));
357351

358-
if blocked.len() == 1 {
359-
self.cond.has_blocked.store(true, Ordering::Relaxed);
360-
}
361352
// the guard is dropped when we return, which frees the lock
362353
Poll::Pending
363354
}
@@ -378,9 +369,7 @@ impl<'a, 'b, T> Drop for AwaitNotify<'a, 'b, T> {
378369
let mut blocked = self.cond.blocked.lock().unwrap();
379370
blocked.remove(key);
380371

381-
if blocked.is_empty() {
382-
self.cond.has_blocked.store(false, Ordering::Relaxed);
383-
} else if self.state.load(Ordering::Acquire) == NOTIFIED_ONCE {
372+
if !blocked.is_empty() && self.state.load(Ordering::Acquire) == NOTIFIED_ONCE {
384373
// we got a notification form notify_once but didn't handle it,
385374
// so send it to a different task
386375
notify(blocked, false);

0 commit comments

Comments
 (0)