|
1 | 1 | use std::cell::{Cell, RefCell};
|
2 | 2 | use std::fmt;
|
3 | 3 | use std::rc::Rc;
|
4 |
| -use std::sync::atomic::{AtomicBool, AtomicI32, Ordering}; |
| 4 | +use std::sync::atomic::{AtomicI32, Ordering}; |
5 | 5 | use std::sync::Arc;
|
6 | 6 |
|
7 | 7 | use futures::executor::{self, Notify, Spawn};
|
@@ -196,23 +196,18 @@ fn _future_to_promise(future: Box<dyn Future<Item = JsValue, Error = JsValue>>)
|
196 | 196 | Waiting(Arc<Package>),
|
197 | 197 | }
|
198 | 198 |
|
| 199 | + #[derive(Default)] |
199 | 200 | struct Waker {
|
| 201 | + // worker will be waiting on this value |
| 202 | + // 0 by default, which means not notified |
200 | 203 | value: AtomicI32,
|
201 |
| - notified: AtomicBool, |
202 | 204 | };
|
203 | 205 |
|
204 |
| - impl Default for Waker { |
205 |
| - fn default() -> Self { |
206 |
| - Waker { |
207 |
| - value: AtomicI32::new(0), |
208 |
| - notified: AtomicBool::new(false), |
209 |
| - } |
210 |
| - } |
211 |
| - } |
212 |
| - |
213 | 206 | impl Notify for Waker {
|
214 | 207 | fn notify(&self, _id: usize) {
|
215 |
| - if !self.notified.swap(true, Ordering::SeqCst) { |
| 208 | + // since we have only value field here |
| 209 | + // let it be 1 if notified, 0 if not |
| 210 | + if self.value.swap(1, Ordering::SeqCst) == 0 { |
216 | 211 | let _ = unsafe {
|
217 | 212 | core::arch::wasm32::atomic_notify(
|
218 | 213 | &self.value as *const AtomicI32 as *mut i32,
|
@@ -256,9 +251,9 @@ fn _future_to_promise(future: Box<dyn Future<Item = JsValue, Error = JsValue>>)
|
256 | 251 | // helps avoid blowing the stack by accident.
|
257 | 252 | let promise =
|
258 | 253 | crate::polyfill::wait_async(&package.waker.value).expect("Should create a Promise");
|
259 |
| - let closure = Closure::once(Box::new(move |_| { |
| 254 | + let closure = Closure::once(move |_| { |
260 | 255 | Package::poll(&me);
|
261 |
| - }) as Box<dyn FnMut(JsValue)>); |
| 256 | + }); |
262 | 257 | promise.then(&closure);
|
263 | 258 | closure.forget();
|
264 | 259 | }
|
|
0 commit comments