|
1 | 1 | use crate::task::{waker_ref, ArcWake}; |
| 2 | +use alloc::sync::{Arc, Weak}; |
| 3 | +use core::cell::UnsafeCell; |
| 4 | +use core::fmt; |
| 5 | +use core::hash::Hasher; |
| 6 | +use core::pin::Pin; |
| 7 | +use core::ptr; |
| 8 | +use core::sync::atomic::AtomicUsize; |
| 9 | +use core::sync::atomic::Ordering::{Acquire, SeqCst}; |
2 | 10 | use futures_core::future::{FusedFuture, Future}; |
3 | 11 | use futures_core::task::{Context, Poll, Waker}; |
4 | 12 | use slab::Slab; |
5 | | -use std::cell::UnsafeCell; |
6 | | -use std::fmt; |
7 | | -use std::hash::Hasher; |
8 | | -use std::pin::Pin; |
9 | | -use std::ptr; |
10 | | -use std::sync::atomic::AtomicUsize; |
11 | | -use std::sync::atomic::Ordering::{Acquire, SeqCst}; |
12 | | -use std::sync::{Arc, Mutex, Weak}; |
| 13 | + |
| 14 | +#[cfg(feature = "std")] |
| 15 | +type Mutex<T> = std::sync::Mutex<T>; |
| 16 | +#[cfg(not(feature = "std"))] |
| 17 | +type Mutex<T> = spin::Mutex<T>; |
13 | 18 |
|
14 | 19 | /// Future for the [`shared`](super::FutureExt::shared) method. |
15 | 20 | #[must_use = "futures do nothing unless you `.await` or poll them"] |
@@ -204,7 +209,10 @@ where |
204 | 209 | { |
205 | 210 | /// Registers the current task to receive a wakeup when we are awoken. |
206 | 211 | fn record_waker(&self, waker_key: &mut usize, cx: &mut Context<'_>) { |
| 212 | + #[cfg(feature = "std")] |
207 | 213 | let mut wakers_guard = self.notifier.wakers.lock().unwrap(); |
| 214 | + #[cfg(not(feature = "std"))] |
| 215 | + let mut wakers_guard = self.notifier.wakers.lock(); |
208 | 216 |
|
209 | 217 | let wakers_mut = wakers_guard.as_mut(); |
210 | 218 |
|
@@ -345,7 +353,11 @@ where |
345 | 353 | inner.notifier.state.store(COMPLETE, SeqCst); |
346 | 354 |
|
347 | 355 | // Wake all tasks and drop the slab |
| 356 | + #[cfg(feature = "std")] |
348 | 357 | let mut wakers_guard = inner.notifier.wakers.lock().unwrap(); |
| 358 | + #[cfg(not(feature = "std"))] |
| 359 | + let mut wakers_guard = inner.notifier.wakers.lock(); |
| 360 | + |
349 | 361 | let mut wakers = wakers_guard.take().unwrap(); |
350 | 362 | for waker in wakers.drain().flatten() { |
351 | 363 | waker.wake(); |
@@ -375,19 +387,28 @@ where |
375 | 387 | fn drop(&mut self) { |
376 | 388 | if self.waker_key != NULL_WAKER_KEY { |
377 | 389 | if let Some(ref inner) = self.inner { |
| 390 | + #[cfg(feature = "std")] |
378 | 391 | if let Ok(mut wakers) = inner.notifier.wakers.lock() { |
379 | 392 | if let Some(wakers) = wakers.as_mut() { |
380 | 393 | wakers.remove(self.waker_key); |
381 | 394 | } |
382 | 395 | } |
| 396 | + #[cfg(not(feature = "std"))] |
| 397 | + if let Some(wakers) = inner.notifier.wakers.lock().as_mut() { |
| 398 | + wakers.remove(self.waker_key); |
| 399 | + } |
383 | 400 | } |
384 | 401 | } |
385 | 402 | } |
386 | 403 | } |
387 | 404 |
|
388 | 405 | impl ArcWake for Notifier { |
389 | 406 | fn wake_by_ref(arc_self: &Arc<Self>) { |
| 407 | + #[cfg(feature = "std")] |
390 | 408 | let wakers = &mut *arc_self.wakers.lock().unwrap(); |
| 409 | + #[cfg(not(feature = "std"))] |
| 410 | + let wakers = &mut *arc_self.wakers.lock(); |
| 411 | + |
391 | 412 | if let Some(wakers) = wakers.as_mut() { |
392 | 413 | for (_key, opt_waker) in wakers { |
393 | 414 | if let Some(waker) = opt_waker.take() { |
|
0 commit comments