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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ exclude = ["/.*"]
# Adds support for executors optimized for use in static variables.
static = []

[lib]
bench = false

[dependencies]
async-task = "4.4.0"
concurrent-queue = "2.5.0"
Expand Down
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ impl<'a> Executor<'a> {
.spawn_unchecked(|()| future, Self::schedule(state));
entry.insert(runnable.waker());

runnable.schedule();
// `Runnable::schedule` has an extra clone/drop of the Waker, which can
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
Self::schedule_runnable(&state, runnable);

task
}

Expand Down Expand Up @@ -353,12 +356,16 @@ impl<'a> Executor<'a> {
fn schedule(state: Pin<&'a State>) -> impl Fn(Runnable) + Send + Sync + 'a {
// TODO: If possible, push into the current local queue and notify the ticker.
move |runnable| {
let result = state.queue.push(runnable);
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
state.notify();
Self::schedule_runnable(&state, runnable);
}
}

fn schedule_runnable(state: &State, runnable: Runnable) {
let result = state.queue.push(runnable);
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
state.notify();
}

/// Returns a pointer to the inner state.
#[inline]
fn state(&self) -> Pin<&'a State> {
Expand Down
46 changes: 36 additions & 10 deletions src/static_executors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ impl StaticExecutor {
let (runnable, task) = Builder::new()
.propagate_panic(true)
.spawn(|()| future, self.schedule());
runnable.schedule();

// `Runnable::schedule` has an extra clone/drop of the Waker, which can
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
Self::schedule_runnable(&self.state, runnable);

task
}

Expand All @@ -219,7 +223,11 @@ impl StaticExecutor {
.propagate_panic(true)
.spawn_unchecked(|()| future, self.schedule())
};
runnable.schedule();

// `Runnable::schedule` has an extra clone/drop of the Waker, which can
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
Self::schedule_runnable(&self.state, runnable);

task
}

Expand Down Expand Up @@ -294,11 +302,16 @@ impl StaticExecutor {
let state: &'static State = &self.state;
// TODO: If possible, push into the current local queue and notify the ticker.
move |runnable| {
let result = state.queue.push(runnable);
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
state.notify();
Self::schedule_runnable(state, runnable);
}
}

#[inline]
fn schedule_runnable(state: &'static State, runnable: Runnable) {
let result = state.queue.push(runnable);
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
state.notify();
}
}

impl Default for StaticExecutor {
Expand Down Expand Up @@ -375,7 +388,11 @@ impl StaticLocalExecutor {
let (runnable, task) = Builder::new()
.propagate_panic(true)
.spawn_local(|()| future, self.schedule());
runnable.schedule();

// `Runnable::schedule` has an extra clone/drop of the Waker, which can
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
Self::schedule_runnable(&self.state, runnable);

task
}

Expand Down Expand Up @@ -408,7 +425,11 @@ impl StaticLocalExecutor {
.propagate_panic(true)
.spawn_unchecked(|()| future, self.schedule())
};
runnable.schedule();

// `Runnable::schedule` has an extra clone/drop of the Waker, which can
// be skipped by directly scheduling instead of calling `Runnable::schedule`.
Self::schedule_runnable(&self.state, runnable);

task
}

Expand Down Expand Up @@ -480,11 +501,16 @@ impl StaticLocalExecutor {
let state: &'static State = &self.state;
// TODO: If possible, push into the current local queue and notify the ticker.
move |runnable| {
let result = state.queue.push(runnable);
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
state.notify();
Self::schedule_runnable(state, runnable);
}
}

#[inline]
fn schedule_runnable(state: &'static State, runnable: Runnable) {
let result = state.queue.push(runnable);
debug_assert!(result.is_ok()); // Since we use unbounded queue, push will never fail.
state.notify();
}
}

impl Default for StaticLocalExecutor {
Expand Down