Skip to content

Commit 31519f0

Browse files
author
Stjepan Glavina
committed
Replace AtomicU64 with AtomicUsize
1 parent 65ee297 commit 31519f0

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::marker::PhantomData;
2626
use std::panic::{RefUnwindSafe, UnwindSafe};
2727
use std::pin::Pin;
2828
use std::rc::Rc;
29-
use std::sync::atomic::{AtomicBool, Ordering, AtomicU64, AtomicUsize};
29+
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
3030
use std::sync::{Arc, Mutex, RwLock};
3131
use std::task::{Context, Poll, Waker};
3232

@@ -199,7 +199,7 @@ impl State {
199199
sleepers: Mutex::new(Sleepers {
200200
count: 0,
201201
wakers: Vec::new(),
202-
id_gen: 1,
202+
free_ids: Vec::new(),
203203
}),
204204
}
205205
}
@@ -228,17 +228,19 @@ struct Sleepers {
228228
/// IDs and wakers of sleeping unnotified tickers.
229229
///
230230
/// A sleeping ticker is notified when its waker is missing from this list.
231-
wakers: Vec<(u64, Waker)>,
231+
wakers: Vec<(usize, Waker)>,
232232

233-
/// ID generator for sleepers.
234-
id_gen: u64,
233+
/// Reclaimed IDs.
234+
free_ids: Vec<usize>,
235235
}
236236

237237
impl Sleepers {
238238
/// Inserts a new sleeping ticker.
239-
fn insert(&mut self, waker: &Waker) -> u64 {
240-
let id = self.id_gen;
241-
self.id_gen += 1;
239+
fn insert(&mut self, waker: &Waker) -> usize {
240+
let id = match self.free_ids.pop() {
241+
Some(id) => id,
242+
None => self.count + 1,
243+
};
242244
self.count += 1;
243245
self.wakers.push((id, waker.clone()));
244246
id
@@ -247,7 +249,7 @@ impl Sleepers {
247249
/// Re-inserts a sleeping ticker's waker if it was notified.
248250
///
249251
/// Returns `true` if the ticker was notified.
250-
fn update(&mut self, id: u64, waker: &Waker) -> bool {
252+
fn update(&mut self, id: usize, waker: &Waker) -> bool {
251253
for item in &mut self.wakers {
252254
if item.0 == id {
253255
if !item.1.will_wake(waker) {
@@ -264,8 +266,10 @@ impl Sleepers {
264266
/// Removes a previously inserted sleeping ticker.
265267
///
266268
/// Returns `true` if the ticker was notified.
267-
fn remove(&mut self, id: u64) -> bool {
269+
fn remove(&mut self, id: usize) -> bool {
268270
self.count -= 1;
271+
self.free_ids.push(id);
272+
269273
for i in (0..self.wakers.len()).rev() {
270274
if self.wakers[i].0 == id {
271275
self.wakers.remove(i);
@@ -494,15 +498,15 @@ struct Ticker<'a> {
494498
/// 1) Woken.
495499
/// 2a) Sleeping and unnotified.
496500
/// 2b) Sleeping and notified.
497-
sleeping: AtomicU64,
501+
sleeping: AtomicUsize,
498502
}
499503

500504
impl Ticker<'_> {
501505
/// Creates a ticker.
502506
fn new(state: &State) -> Ticker<'_> {
503507
Ticker {
504508
state,
505-
sleeping: AtomicU64::new(0),
509+
sleeping: AtomicUsize::new(0),
506510
}
507511
}
508512

@@ -514,7 +518,9 @@ impl Ticker<'_> {
514518

515519
match self.sleeping.load(Ordering::SeqCst) {
516520
// Move to sleeping state.
517-
0 => self.sleeping.store(sleepers.insert(waker), Ordering::SeqCst),
521+
0 => self
522+
.sleeping
523+
.store(sleepers.insert(waker), Ordering::SeqCst),
518524

519525
// Already sleeping, check if notified.
520526
id => {

0 commit comments

Comments
 (0)