Skip to content

Commit e779304

Browse files
authored
m: Remove dependency on waker-fn
Resolves #165 Adds a new concrete type BlockOnWaker that implements Wake
1 parent 87ad890 commit e779304

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ polling = "3.0.0"
3333
rustix = { version = "0.38.2", default-features = false, features = ["fs", "net", "std"] }
3434
slab = "0.4.2"
3535
tracing = { version = "0.1.37", default-features = false }
36-
waker-fn = "1.1.0"
3736

3837
[target.'cfg(windows)'.dependencies]
3938
windows-sys = { version = "0.48.0", features = ["Win32_Foundation"] }

src/driver.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::time::{Duration, Instant};
1010
use async_lock::OnceCell;
1111
use futures_lite::pin;
1212
use parking::Parker;
13-
use waker_fn::waker_fn;
1413

1514
use crate::reactor::Reactor;
1615

@@ -131,17 +130,7 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
131130
let io_blocked = Arc::new(AtomicBool::new(false));
132131

133132
// Prepare the waker.
134-
let waker = waker_fn({
135-
let io_blocked = io_blocked.clone();
136-
move || {
137-
if u.unpark() {
138-
// Check if waking from another thread and if currently blocked on I/O.
139-
if !IO_POLLING.with(Cell::get) && io_blocked.load(Ordering::SeqCst) {
140-
Reactor::get().notify();
141-
}
142-
}
143-
}
144-
});
133+
let waker = BlockOnWaker::create(io_blocked.clone(), u);
145134

146135
(p, waker, io_blocked)
147136
}
@@ -154,6 +143,35 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
154143
static IO_POLLING: Cell<bool> = Cell::new(false);
155144
}
156145

146+
struct BlockOnWaker {
147+
io_blocked: Arc<AtomicBool>,
148+
unparker: parking::Unparker,
149+
}
150+
151+
impl BlockOnWaker {
152+
fn create(io_blocked: Arc<AtomicBool>, unparker: parking::Unparker) -> Waker {
153+
Waker::from(Arc::new(BlockOnWaker {
154+
io_blocked,
155+
unparker,
156+
}))
157+
}
158+
}
159+
160+
impl std::task::Wake for BlockOnWaker {
161+
fn wake_by_ref(self: &Arc<Self>) {
162+
if self.unparker.unpark() {
163+
// Check if waking from another thread and if currently blocked on I/O.
164+
if !IO_POLLING.with(Cell::get) && self.io_blocked.load(Ordering::SeqCst) {
165+
Reactor::get().notify();
166+
}
167+
}
168+
}
169+
170+
fn wake(self: Arc<Self>) {
171+
self.wake_by_ref()
172+
}
173+
}
174+
157175
CACHE.with(|cache| {
158176
// Try grabbing the cached parker and waker.
159177
let tmp_cached;

0 commit comments

Comments
 (0)