Skip to content

Commit f89a0fb

Browse files
authored
Minor improvements to wasm-bindgen-futures (#3594)
1 parent 41617bd commit f89a0fb

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

crates/futures/src/task/singlethread.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ impl Task {
3535
crate::queue::QUEUE.with(|queue| queue.schedule_task(this));
3636
}
3737

38+
fn force_wake(this: Rc<Self>) {
39+
crate::queue::QUEUE.with(|queue| {
40+
queue.push_task(this);
41+
});
42+
}
43+
44+
fn wake(this: Rc<Self>) {
45+
// If we've already been placed on the run queue then there's no need to
46+
// requeue ourselves since we're going to run at some point in the
47+
// future anyway.
48+
if this.is_queued.replace(true) {
49+
return;
50+
}
51+
52+
Self::force_wake(this);
53+
}
54+
3855
fn wake_by_ref(this: &Rc<Self>) {
3956
// If we've already been placed on the run queue then there's no need to
4057
// requeue ourselves since we're going to run at some point in the
@@ -43,9 +60,7 @@ impl Task {
4360
return;
4461
}
4562

46-
crate::queue::QUEUE.with(|queue| {
47-
queue.push_task(Rc::clone(this));
48-
});
63+
Self::force_wake(Rc::clone(this));
4964
}
5065

5166
/// Creates a standard library `RawWaker` from an `Rc` of ourselves.
@@ -55,15 +70,17 @@ impl Task {
5570
/// however, everything is guaranteed to be singlethreaded (since we're
5671
/// compiled without the `atomics` feature) so we "safely lie" and say our
5772
/// `Rc` pointer is good enough.
73+
///
74+
/// The implementation is based off of futures::task::ArcWake
5875
unsafe fn into_raw_waker(this: Rc<Self>) -> RawWaker {
5976
unsafe fn raw_clone(ptr: *const ()) -> RawWaker {
6077
let ptr = ManuallyDrop::new(Rc::from_raw(ptr as *const Task));
61-
Task::into_raw_waker((*ptr).clone())
78+
Task::into_raw_waker(Rc::clone(&ptr))
6279
}
6380

6481
unsafe fn raw_wake(ptr: *const ()) {
6582
let ptr = Rc::from_raw(ptr as *const Task);
66-
Task::wake_by_ref(&ptr);
83+
Task::wake(ptr);
6784
}
6885

6986
unsafe fn raw_wake_by_ref(ptr: *const ()) {
@@ -75,7 +92,7 @@ impl Task {
7592
drop(Rc::from_raw(ptr as *const Task));
7693
}
7794

78-
const VTABLE: RawWakerVTable =
95+
static VTABLE: RawWakerVTable =
7996
RawWakerVTable::new(raw_clone, raw_wake, raw_wake_by_ref, raw_drop);
8097

8198
RawWaker::new(Rc::into_raw(this) as *const (), &VTABLE)

0 commit comments

Comments
 (0)