Skip to content
Open
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
- uses: actions/checkout@master

- uses: ./.github/actions/install-rust
with:
toolchain: 1.89.0

- name: Install wasmtime
uses: bytecodealliance/actions/wasmtime/setup@v1
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/block_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use std::task::{Context, Poll, Waker};
/// Start the event loop. Blocks until the future
pub fn block_on<F>(fut: F) -> F::Output
where
F: Future + 'static,
F::Output: 'static,
F: Future,
{
// Construct the reactor
let reactor = Reactor::new();
Expand All @@ -19,7 +18,11 @@ where
}

// Spawn the task onto the reactor.
let root_task = reactor.spawn(fut);
// Safety: The execution loop below, concluding with pulling the Ready out
// of the root_task, ensures that it does not outlive the Future or its
// output.
#[allow(unsafe_code)]
let root_task = unsafe { reactor.spawn_unchecked(fut) };

loop {
match reactor.pop_ready_list() {
Expand Down
27 changes: 23 additions & 4 deletions src/runtime/reactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,17 @@ impl Reactor {
ready
}

/// Spawn a `Task` on the `Reactor`.
pub fn spawn<F, T>(&self, fut: F) -> Task<T>
/// We need an unchecked spawn for implementing `block_on`, where the
/// implementation does not expose the resulting Task and does not return
/// until all tasks have finished execution.
///
/// # Safety
/// Caller must ensure that the Task<T> does not outlive the Future F or
/// F::Output T.
#[allow(unsafe_code)]
pub(crate) unsafe fn spawn_unchecked<F, T>(&self, fut: F) -> Task<T>
where
F: Future<Output = T> + 'static,
T: 'static,
F: Future<Output = T>,
{
let this = self.clone();
let schedule = move |runnable| this.inner.ready_list.lock().unwrap().push_back(runnable);
Expand All @@ -278,6 +284,19 @@ impl Reactor {
task
}

/// Spawn a `Task` on the `Reactor`.
pub fn spawn<F, T>(&self, fut: F) -> Task<T>
where
F: Future<Output = T> + 'static,
T: 'static,
{
// Safety: 'static constraints satisfy the lifetime requirements
#[allow(unsafe_code)]
unsafe {
self.spawn_unchecked(fut)
}
}

pub(super) fn pop_ready_list(&self) -> Option<Runnable> {
self.inner.ready_list.lock().unwrap().pop_front()
}
Expand Down
Loading