Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

fix: sleep in async executor when there are no futures left to poll #26

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions pros/src/async_runtime/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -79,17 +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;
doinkythederp marked this conversation as resolved.
Show resolved Hide resolved
}

delay(Duration::from_millis(10));
self.tick();
}
}

pub fn complete(&self) {
loop {
if !self.tick() {
break;
}
}
}
}
8 changes: 0 additions & 8 deletions pros/src/async_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,3 @@ pub fn spawn<T>(future: impl Future<Output = T> + 'static) -> Task<T> {
pub fn block_on<F: Future + 'static>(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();
})
}
4 changes: 0 additions & 4 deletions pros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ macro_rules! __gen_async_exports {
.expect("Expected initialize to run before opcontrol")
}))
.unwrap();
$crate::async_runtime::complete_all();
}

#[doc(hidden)]
Expand All @@ -197,7 +196,6 @@ macro_rules! __gen_async_exports {
.expect("Expected initialize to run before auto")
}))
.unwrap();
$crate::async_runtime::complete_all();
}

#[doc(hidden)]
Expand All @@ -209,7 +207,6 @@ macro_rules! __gen_async_exports {
.expect("Expected initialize to run before disabled")
}))
.unwrap();
$crate::async_runtime::complete_all();
}

#[doc(hidden)]
Expand All @@ -221,7 +218,6 @@ macro_rules! __gen_async_exports {
.expect("Expected initialize to run before comp_init")
}))
.unwrap();
$crate::async_runtime::complete_all();
}
};
}
Expand Down
Loading