Skip to content

Commit

Permalink
Review recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
tglane committed Aug 18, 2024
1 parent 68e5cf1 commit e9c4351
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions tokio/src/runtime/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ struct Inner {
/// Sharded Timer wheels.
wheels: RwLock<ShardedWheel>,

/// Number of entries in the sharded timer wheels.
wheels_len: u32,

/// True if the driver is being shutdown.
pub(super) is_shutdown: AtomicBool,

Expand Down Expand Up @@ -154,6 +157,7 @@ impl Driver {
inner: Inner {
next_wake: AtomicOptionNonZeroU64::new(None),
wheels: RwLock::new(ShardedWheel(wheels.into_boxed_slice())),
wheels_len: shards,
is_shutdown: AtomicBool::new(false),
#[cfg(feature = "test-util")]
did_wake: AtomicBool::new(false),
Expand Down Expand Up @@ -200,10 +204,10 @@ impl Driver {
.inner
.wheels
.write()
.expect("Timer wheel shards poisened");
let expiration_time = (0..wheels_lock.get_shard_size())
.expect("Timer wheel shards poisoned");
let expiration_time = (0..rt_handle.time().inner.get_shard_size())
.filter_map(|id| {
let wheel = wheels_lock.get_sharded_wheel(id);
let wheel = wheels_lock.get_sharded_wheel_mut(id);
wheel.next_expiration_time()
})
.min();
Expand Down Expand Up @@ -325,7 +329,7 @@ impl Handle {
.inner
.wheels
.read()
.expect("Timer wheel shards poisened");
.expect("Timer wheel shards poisoned");
let mut lock = wheels_lock.lock_sharded_wheel(id);

if now < lock.elapsed() {
Expand Down Expand Up @@ -356,7 +360,7 @@ impl Handle {
.inner
.wheels
.read()
.expect("Timer wheel shards poisened");
.expect("Timer wheel shards poisoned");
lock = wheels_lock.lock_sharded_wheel(id);
}
}
Expand Down Expand Up @@ -384,7 +388,7 @@ impl Handle {
.inner
.wheels
.read()
.expect("Timer wheel shards poisened");
.expect("Timer wheel shards poisoned");
let mut lock = wheels_lock.lock_sharded_wheel(entry.as_ref().shard_id());

if entry.as_ref().might_be_registered() {
Expand Down Expand Up @@ -412,7 +416,7 @@ impl Handle {
.inner
.wheels
.read()
.expect("Timer wheel shards poisened");
.expect("Timer wheel shards poisoned");

let mut lock = wheels_lock.lock_sharded_wheel(entry.as_ref().shard_id());

Expand Down Expand Up @@ -481,10 +485,7 @@ impl Inner {

// Gets the number of shards.
fn get_shard_size(&self) -> u32 {
self.wheels
.read()
.expect("Timer wheel shards poisened")
.get_shard_size()
self.wheels_len
}
}

Expand All @@ -508,16 +509,11 @@ impl ShardedWheel {
}

/// Gets a mutable reference to the sharded wheel with the given id.
pub(super) fn get_sharded_wheel(&mut self, shard_id: u32) -> &mut wheel::Wheel {
pub(super) fn get_sharded_wheel_mut(&mut self, shard_id: u32) -> &mut wheel::Wheel {
let index = shard_id % (self.0.len() as u32);
// Safety: This modulo operation ensures that the index is not out of bounds.
unsafe { self.0.get_unchecked_mut(index as usize) }.get_mut()
}

/// Gets the number of shards.
fn get_shard_size(&self) -> u32 {
self.0.len() as u32
}
}

#[cfg(test)]
Expand Down

0 comments on commit e9c4351

Please sign in to comment.