From 8659f6b0c3c6095325459e4d8e206b4506a30b8e Mon Sep 17 00:00:00 2001 From: Gavin-Niederman Date: Thu, 4 Jan 2024 14:37:39 -0800 Subject: [PATCH 1/3] fix: sleep in async executor loops --- pros/src/async_runtime/executor.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pros/src/async_runtime/executor.rs b/pros/src/async_runtime/executor.rs index 3d1659cb..df030509 100644 --- a/pros/src/async_runtime/executor.rs +++ b/pros/src/async_runtime/executor.rs @@ -4,13 +4,14 @@ use core::{ pin::Pin, sync::atomic::{AtomicBool, Ordering}, task::{Context, Poll}, + time::Duration, }; use alloc::{collections::VecDeque, sync::Arc}; use async_task::{Runnable, Task}; use waker_fn::waker_fn; -use crate::os_task_local; +use crate::{os_task_local, task::delay}; use super::reactor::Reactor; @@ -82,6 +83,7 @@ impl Executor { } self.tick(); + delay(Duration::from_millis(10)); } } @@ -90,6 +92,7 @@ impl Executor { if !self.tick() { break; } + delay(Duration::from_millis(10)); } } } From f0f3ae67e32c0d3572db223d71f891cce8e37bdf Mon Sep 17 00:00:00 2001 From: Gavin-Niederman Date: Thu, 4 Jan 2024 16:06:25 -0800 Subject: [PATCH 2/3] fix: only sleep when there are no futures to poll --- pros/src/async_runtime/executor.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pros/src/async_runtime/executor.rs b/pros/src/async_runtime/executor.rs index df030509..b3f7fb9d 100644 --- a/pros/src/async_runtime/executor.rs +++ b/pros/src/async_runtime/executor.rs @@ -80,10 +80,13 @@ impl Executor { if let Poll::Ready(output) = Pin::new(&mut task).poll(&mut cx) { return output; } + self.tick(); + // there might be another future to poll, so we continue without sleeping + continue; } - self.tick(); delay(Duration::from_millis(10)); + self.tick(); } } From 5a6e1f91772b99874da8967b3c1c9297fdb66545 Mon Sep 17 00:00:00 2001 From: Gavin-Niederman Date: Thu, 4 Jan 2024 16:41:35 -0800 Subject: [PATCH 3/3] fix: remove broken complete_all executor function --- pros/src/async_runtime/executor.rs | 9 --------- pros/src/async_runtime/mod.rs | 8 -------- pros/src/lib.rs | 4 ---- 3 files changed, 21 deletions(-) diff --git a/pros/src/async_runtime/executor.rs b/pros/src/async_runtime/executor.rs index b3f7fb9d..cef55081 100644 --- a/pros/src/async_runtime/executor.rs +++ b/pros/src/async_runtime/executor.rs @@ -89,13 +89,4 @@ impl Executor { self.tick(); } } - - pub fn complete(&self) { - loop { - if !self.tick() { - break; - } - delay(Duration::from_millis(10)); - } - } } diff --git a/pros/src/async_runtime/mod.rs b/pros/src/async_runtime/mod.rs index 92d7009c..2fec27f4 100644 --- a/pros/src/async_runtime/mod.rs +++ b/pros/src/async_runtime/mod.rs @@ -22,11 +22,3 @@ pub fn spawn(future: impl Future + 'static) -> Task { pub fn block_on(future: F) -> F::Output { executor::EXECUTOR.with(|e| e.block_on(spawn(future))) } - -/// Completes all tasks. -/// Return values can be extracted from the futures by awaiting any [`Task`]s you have not detached. -pub fn complete_all() { - executor::EXECUTOR.with(|e| { - e.complete(); - }) -} diff --git a/pros/src/lib.rs b/pros/src/lib.rs index c442debd..a4ffa42c 100644 --- a/pros/src/lib.rs +++ b/pros/src/lib.rs @@ -185,7 +185,6 @@ macro_rules! __gen_async_exports { .expect("Expected initialize to run before opcontrol") })) .unwrap(); - $crate::async_runtime::complete_all(); } #[doc(hidden)] @@ -197,7 +196,6 @@ macro_rules! __gen_async_exports { .expect("Expected initialize to run before auto") })) .unwrap(); - $crate::async_runtime::complete_all(); } #[doc(hidden)] @@ -209,7 +207,6 @@ macro_rules! __gen_async_exports { .expect("Expected initialize to run before disabled") })) .unwrap(); - $crate::async_runtime::complete_all(); } #[doc(hidden)] @@ -221,7 +218,6 @@ macro_rules! __gen_async_exports { .expect("Expected initialize to run before comp_init") })) .unwrap(); - $crate::async_runtime::complete_all(); } }; }