Skip to content

Commit ae4653f

Browse files
committed
sync: use WakeList in batch_semaphore
This commit updates the internal semaphore implementation (`batch_semaphore.rs`) to use the new `WakeList` type added in PR #4055.
1 parent 0d703b3 commit ae4653f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

tokio/src/sync/batch_semaphore.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::loom::cell::UnsafeCell;
1919
use crate::loom::sync::atomic::AtomicUsize;
2020
use crate::loom::sync::{Mutex, MutexGuard};
2121
use crate::util::linked_list::{self, LinkedList};
22+
use crate::util::WakeList;
2223

2324
use std::future::Future;
2425
use std::marker::PhantomPinned;
@@ -239,12 +240,12 @@ impl Semaphore {
239240
/// If `rem` exceeds the number of permits needed by the wait list, the
240241
/// remainder are assigned back to the semaphore.
241242
fn add_permits_locked(&self, mut rem: usize, waiters: MutexGuard<'_, Waitlist>) {
242-
let mut wakers: [Option<Waker>; 8] = Default::default();
243+
let mut wakers = WakeList::new();
243244
let mut lock = Some(waiters);
244245
let mut is_empty = false;
245246
while rem > 0 {
246247
let mut waiters = lock.take().unwrap_or_else(|| self.waiters.lock());
247-
'inner: for slot in &mut wakers[..] {
248+
'inner: while wakers.can_push() {
248249
// Was the waiter assigned enough permits to wake it?
249250
match waiters.queue.last() {
250251
Some(waiter) => {
@@ -260,7 +261,11 @@ impl Semaphore {
260261
}
261262
};
262263
let mut waiter = waiters.queue.pop_back().unwrap();
263-
*slot = unsafe { waiter.as_mut().waker.with_mut(|waker| (*waker).take()) };
264+
if let Some(waker) =
265+
unsafe { waiter.as_mut().waker.with_mut(|waker| (*waker).take()) }
266+
{
267+
wakers.push(waker);
268+
}
264269
}
265270

266271
if rem > 0 && is_empty {
@@ -283,10 +288,7 @@ impl Semaphore {
283288

284289
drop(waiters); // release the lock
285290

286-
wakers
287-
.iter_mut()
288-
.filter_map(Option::take)
289-
.for_each(Waker::wake);
291+
wakers.wake_all();
290292
}
291293

292294
assert_eq!(rem, 0);

0 commit comments

Comments
 (0)