Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on waker-fn #171

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ polling = "3.0.0"
rustix = { version = "0.38.2", default-features = false, features = ["fs", "net", "std"] }
slab = "0.4.2"
tracing = { version = "0.1.37", default-features = false }
waker-fn = "1.1.0"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.48.0", features = ["Win32_Foundation"] }
Expand Down
40 changes: 29 additions & 11 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::time::{Duration, Instant};
use async_lock::OnceCell;
use futures_lite::pin;
use parking::Parker;
use waker_fn::waker_fn;

use crate::reactor::Reactor;

Expand Down Expand Up @@ -131,19 +130,38 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
let io_blocked = Arc::new(AtomicBool::new(false));

// Prepare the waker.
let waker = waker_fn({
let io_blocked = io_blocked.clone();
move || {
if u.unpark() {
// Check if waking from another thread and if currently blocked on I/O.
if !IO_POLLING.with(Cell::get) && io_blocked.load(Ordering::SeqCst) {
Reactor::get().notify();
}
let waker = BlockOnWaker::create(io_blocked.clone(), u);

(p, waker, io_blocked)
}

struct BlockOnWaker {
io_blocked: Arc<AtomicBool>,
unparker: parking::Unparker,
}
notgull marked this conversation as resolved.
Show resolved Hide resolved

impl BlockOnWaker {
fn create(io_blocked: Arc<AtomicBool>, unparker: parking::Unparker) -> Waker {
Waker::from(Arc::new(BlockOnWaker {
io_blocked,
unparker,
}))
}
}

impl std::task::Wake for BlockOnWaker {
fn wake_by_ref(self: &Arc<Self>) {
if self.unparker.unpark() {
// Check if waking from another thread and if currently blocked on I/O.
if !IO_POLLING.with(Cell::get) && self.io_blocked.load(Ordering::SeqCst) {
Reactor::get().notify();
}
}
});
}

(p, waker, io_blocked)
fn wake(self: Arc<Self>) {
self.wake_by_ref()
}
}

thread_local! {
Expand Down
Loading