Skip to content

Commit 482eadd

Browse files
author
Patrick Ribbsaeter
authored
runtime: record time wake when unparking tasks on current thread (#8595)
Fixes #8232. Signed-off-by: Patrick Ribbsaeter <[email protected]>
1 parent 55fbab7 commit 482eadd

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎tokio/src/runtime/driver.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ impl Handle {
8686
self.io.unpark();
8787
}
8888

89+
pub(crate) fn unpark_self(&self) {
90+
#[cfg(feature = "time")]
91+
if let Some(handle) = &self.time {
92+
handle.unpark();
93+
}
94+
}
95+
8996
cfg_io_driver! {
9097
#[track_caller]
9198
pub(crate) fn io(&self) -> &crate::runtime::io::Handle {

‎tokio/src/runtime/scheduler/current_thread/mod.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ impl Schedule for Arc<Handle> {
682682
if let Some(core) = core.as_mut() {
683683
core.push_task(self, task);
684684
}
685+
self.driver.unpark_self();
685686
}
686687
_ => {
687688
// Track that a task was scheduled from **outside** of the runtime.
@@ -748,7 +749,9 @@ impl Wake for Handle {
748749
// If we are already running on the runtime, then it's not required to wake up the
749750
// runtime.
750751
context::with_scheduler(|maybe_cx| match maybe_cx {
751-
Some(CurrentThread(cx)) if Arc::ptr_eq(arc_self, &cx.handle) => {}
752+
Some(CurrentThread(cx)) if Arc::ptr_eq(arc_self, &cx.handle) => {
753+
arc_self.driver.unpark_self();
754+
}
752755
_ => {
753756
arc_self.driver.unpark();
754757
}

‎tokio/tests/time_pause.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,44 @@ fn poll_next(interval: &mut task::Spawn<time::Interval>) -> Poll<Instant> {
331331
fn ms(n: u64) -> Duration {
332332
Duration::from_millis(n)
333333
}
334+
335+
#[cfg(all(feature = "full", unix))]
336+
#[tokio::test(start_paused = true)]
337+
async fn paused_time_join_unix_datagram_and_sleep() {
338+
use tokio::net::UnixDatagram;
339+
340+
let time = Instant::now();
341+
let (sock1, sock2) = UnixDatagram::pair().unwrap();
342+
let mut buf = [0u8; 1];
343+
tokio::join!(
344+
async {
345+
sock1.send(&[1u8]).await.unwrap();
346+
time::sleep(Duration::from_millis(100)).await;
347+
},
348+
async {
349+
sock2.recv(&mut buf).await.unwrap();
350+
assert_eq!(time.elapsed(), Duration::ZERO);
351+
},
352+
);
353+
}
354+
355+
#[cfg(all(feature = "full", unix))]
356+
#[tokio::test(start_paused = true)]
357+
async fn paused_time_spawn_unix_datagram_and_sleep() {
358+
use tokio::net::UnixDatagram;
359+
360+
let time = Instant::now();
361+
let (sock1, sock2) = UnixDatagram::pair().unwrap();
362+
363+
sock1.send(&[1u8]).await.unwrap();
364+
365+
let h = tokio::spawn(async move {
366+
let mut buf = [0u8; 1];
367+
sock2.recv(&mut buf).await.unwrap();
368+
assert_eq!(time.elapsed(), Duration::ZERO);
369+
});
370+
371+
time::sleep(Duration::from_millis(100)).await;
372+
h.await.unwrap();
373+
assert_eq!(time.elapsed(), Duration::from_millis(100));
374+
}

0 commit comments

Comments
 (0)