From 02be3690cfb6cd5f32e7811b1cfc8d4589fe52c9 Mon Sep 17 00:00:00 2001 From: ibaryshnikov Date: Wed, 17 Jul 2019 01:52:55 +0300 Subject: [PATCH] removed AtomicBool from Waker struct --- crates/futures/src/atomics.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/crates/futures/src/atomics.rs b/crates/futures/src/atomics.rs index 5a5e1dd4..b18b5309 100644 --- a/crates/futures/src/atomics.rs +++ b/crates/futures/src/atomics.rs @@ -1,7 +1,7 @@ use std::cell::{Cell, RefCell}; use std::fmt; use std::rc::Rc; -use std::sync::atomic::{AtomicBool, AtomicI32, Ordering}; +use std::sync::atomic::{AtomicI32, Ordering}; use std::sync::Arc; use futures::executor::{self, Notify, Spawn}; @@ -196,23 +196,18 @@ fn _future_to_promise(future: Box>) Waiting(Arc), } + #[derive(Default)] struct Waker { + // worker will be waiting on this value + // 0 by default, which means not notified value: AtomicI32, - notified: AtomicBool, }; - impl Default for Waker { - fn default() -> Self { - Waker { - value: AtomicI32::new(0), - notified: AtomicBool::new(false), - } - } - } - impl Notify for Waker { fn notify(&self, _id: usize) { - if !self.notified.swap(true, Ordering::SeqCst) { + // since we have only value field here + // let it be 1 if notified, 0 if not + if self.value.swap(1, Ordering::SeqCst) == 0 { let _ = unsafe { core::arch::wasm32::atomic_notify( &self.value as *const AtomicI32 as *mut i32, @@ -256,9 +251,9 @@ fn _future_to_promise(future: Box>) // helps avoid blowing the stack by accident. let promise = crate::polyfill::wait_async(&package.waker.value).expect("Should create a Promise"); - let closure = Closure::once(Box::new(move |_| { + let closure = Closure::once(move |_| { Package::poll(&me); - }) as Box); + }); promise.then(&closure); closure.forget(); }