,
+ unpark: IoUnpark,
}
/// Time state shared which must be protected by a `Mutex`
@@ -132,18 +144,15 @@ struct InnerState {
// ===== impl Driver =====
-impl Driver
-where
- P: Park + 'static,
-{
+impl Driver {
/// Creates a new `Driver` instance that uses `park` to block the current
/// thread and `time_source` to get the current time and convert to ticks.
///
/// Specifying the source of time is useful when testing.
- pub(crate) fn new(park: P, clock: Clock) -> Driver
{
+ pub(crate) fn new(park: IoStack, clock: Clock) -> Driver {
let time_source = TimeSource::new(clock);
- let inner = Inner::new(time_source.clone(), Box::new(park.unpark()));
+ let inner = Inner::new(time_source.clone(), park.unpark());
Driver {
time_source,
@@ -164,7 +173,33 @@ where
self.handle.clone()
}
- fn park_internal(&mut self, limit: Option) -> Result<(), P::Error> {
+ pub(crate) fn unpark(&self) -> TimerUnpark {
+ TimerUnpark::new(self)
+ }
+
+ pub(crate) fn park(&mut self) {
+ self.park_internal(None)
+ }
+
+ pub(crate) fn park_timeout(&mut self, duration: Duration) {
+ self.park_internal(Some(duration))
+ }
+
+ pub(crate) fn shutdown(&mut self) {
+ if self.handle.is_shutdown() {
+ return;
+ }
+
+ self.handle.get().is_shutdown.store(true, Ordering::SeqCst);
+
+ // Advance time forward to the end of time.
+
+ self.handle.process_at_time(u64::MAX);
+
+ self.park.shutdown();
+ }
+
+ fn park_internal(&mut self, limit: Option) {
let mut lock = self.handle.get().state.lock();
assert!(!self.handle.is_shutdown());
@@ -188,32 +223,30 @@ where
duration = std::cmp::min(limit, duration);
}
- self.park_timeout(duration)?;
+ self.park_thread_timeout(duration);
} else {
- self.park.park_timeout(Duration::from_secs(0))?;
+ self.park.park_timeout(Duration::from_secs(0));
}
}
None => {
if let Some(duration) = limit {
- self.park_timeout(duration)?;
+ self.park_thread_timeout(duration);
} else {
- self.park.park()?;
+ self.park.park();
}
}
}
// Process pending timers after waking up
self.handle.process();
-
- Ok(())
}
cfg_test_util! {
- fn park_timeout(&mut self, duration: Duration) -> Result<(), P::Error> {
+ fn park_thread_timeout(&mut self, duration: Duration) {
let clock = &self.time_source.clock;
if clock.is_paused() {
- self.park.park_timeout(Duration::from_secs(0))?;
+ self.park.park_timeout(Duration::from_secs(0));
// If the time driver was woken, then the park completed
// before the "duration" elapsed (usually caused by a
@@ -224,10 +257,8 @@ where
clock.advance(duration);
}
} else {
- self.park.park_timeout(duration)?;
+ self.park.park_timeout(duration);
}
-
- Ok(())
}
fn did_wake(&self) -> bool {
@@ -236,8 +267,8 @@ where
}
cfg_not_test_util! {
- fn park_timeout(&mut self, duration: Duration) -> Result<(), P::Error> {
- self.park.park_timeout(duration)
+ fn park_thread_timeout(&mut self, duration: Duration) {
+ self.park.park_timeout(duration);
}
}
}
@@ -383,58 +414,21 @@ impl Handle {
}
}
-impl Park for Driver
-where
- P: Park + 'static,
-{
- type Unpark = TimerUnpark
;
- type Error = P::Error;
-
- fn unpark(&self) -> Self::Unpark {
- TimerUnpark::new(self)
- }
-
- fn park(&mut self) -> Result<(), Self::Error> {
- self.park_internal(None)
- }
-
- fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
- self.park_internal(Some(duration))
- }
-
- fn shutdown(&mut self) {
- if self.handle.is_shutdown() {
- return;
- }
-
- self.handle.get().is_shutdown.store(true, Ordering::SeqCst);
-
- // Advance time forward to the end of time.
-
- self.handle.process_at_time(u64::MAX);
-
- self.park.shutdown();
- }
-}
-
-impl
Drop for Driver
-where
- P: Park + 'static,
-{
+impl Drop for Driver {
fn drop(&mut self) {
self.shutdown();
}
}
-pub(crate) struct TimerUnpark {
- inner: P::Unpark,
+pub(crate) struct TimerUnpark {
+ inner: IoUnpark,
#[cfg(feature = "test-util")]
did_wake: Arc,
}
-impl TimerUnpark {
- fn new(driver: &Driver
) -> TimerUnpark
{
+impl TimerUnpark {
+ fn new(driver: &Driver) -> TimerUnpark {
TimerUnpark {
inner: driver.park.unpark(),
@@ -442,10 +436,8 @@ impl TimerUnpark {
did_wake: driver.did_wake.clone(),
}
}
-}
-impl Unpark for TimerUnpark {
- fn unpark(&self) {
+ pub(crate) fn unpark(&self) {
#[cfg(feature = "test-util")]
self.did_wake.store(true, Ordering::SeqCst);
@@ -456,7 +448,7 @@ impl Unpark for TimerUnpark {
// ===== impl Inner =====
impl Inner {
- pub(self) fn new(time_source: TimeSource, unpark: Box) -> Self {
+ pub(self) fn new(time_source: TimeSource, unpark: IoUnpark) -> Self {
Inner {
state: Mutex::new(InnerState {
time_source,
diff --git a/tokio/src/runtime/time/tests/mod.rs b/tokio/src/runtime/time/tests/mod.rs
index ff69c8407fa..31ed198e2f7 100644
--- a/tokio/src/runtime/time/tests/mod.rs
+++ b/tokio/src/runtime/time/tests/mod.rs
@@ -5,25 +5,13 @@ use std::{task::Context, time::Duration};
#[cfg(not(loom))]
use futures::task::noop_waker_ref;
+use crate::loom::sync::atomic::{AtomicBool, Ordering};
use crate::loom::sync::Arc;
use crate::loom::thread;
-use crate::{
- loom::sync::atomic::{AtomicBool, Ordering},
- park::Unpark,
-};
+use crate::runtime::driver::IoUnpark;
use super::{Handle, TimerEntry};
-struct MockUnpark {}
-impl Unpark for MockUnpark {
- fn unpark(&self) {}
-}
-impl MockUnpark {
- fn mock() -> Box {
- Box::new(Self {})
- }
-}
-
fn block_on(f: impl std::future::Future