Skip to content

Commit f01641e

Browse files
committed
reexport alloc::task::Wake
1 parent 88b7557 commit f01641e

File tree

16 files changed

+104
-189
lines changed

16 files changed

+104
-189
lines changed

futures-core/src/task/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ pub mod __internal;
88

99
#[doc(no_inline)]
1010
pub use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
11+
12+
#[doc(no_inline)]
13+
#[cfg(feature = "alloc")]
14+
pub use alloc::task::Wake;

futures-executor/src/local_pool.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::enter;
22
use futures_core::future::Future;
33
use futures_core::stream::Stream;
44
use futures_core::task::{Context, Poll};
5-
use futures_task::{waker_ref, ArcWake};
5+
use futures_task::{waker_ref, Wake};
66
use futures_task::{FutureObj, LocalFutureObj, LocalSpawn, Spawn, SpawnError};
77
use futures_util::pin_mut;
88
use futures_util::stream::FuturesUnordered;
@@ -60,17 +60,21 @@ thread_local! {
6060
});
6161
}
6262

63-
impl ArcWake for ThreadNotify {
64-
fn wake_by_ref(arc_self: &Arc<Self>) {
63+
impl Wake for ThreadNotify {
64+
fn wake(self: Arc<Self>) {
65+
self.wake_by_ref();
66+
}
67+
68+
fn wake_by_ref(self: &Arc<Self>) {
6569
// Make sure the wakeup is remembered until the next `park()`.
66-
let unparked = arc_self.unparked.swap(true, Ordering::Relaxed);
70+
let unparked = self.unparked.swap(true, Ordering::Relaxed);
6771
if !unparked {
6872
// If the thread has not been unparked yet, it must be done
6973
// now. If it was actually parked, it will run again,
7074
// otherwise the token made available by `unpark`
7175
// may be consumed before reaching `park()`, but `unparked`
7276
// ensures it is not forgotten.
73-
arc_self.thread.unpark();
77+
self.thread.unpark();
7478
}
7579
}
7680
}

futures-executor/src/thread_pool.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::enter;
22
use crate::unpark_mutex::UnparkMutex;
33
use futures_core::future::Future;
44
use futures_core::task::{Context, Poll};
5-
use futures_task::{waker_ref, ArcWake};
5+
use futures_task::{waker_ref, Wake};
66
use futures_task::{FutureObj, Spawn, SpawnError};
77
use futures_util::future::FutureExt;
88
use std::cmp;
@@ -344,10 +344,14 @@ impl fmt::Debug for Task {
344344
}
345345
}
346346

347-
impl ArcWake for WakeHandle {
348-
fn wake_by_ref(arc_self: &Arc<Self>) {
349-
match arc_self.mutex.notify() {
350-
Ok(task) => arc_self.exec.state.send(Message::Run(task)),
347+
impl Wake for WakeHandle {
348+
fn wake(self: Arc<Self>) {
349+
self.wake_by_ref();
350+
}
351+
352+
fn wake_by_ref(self: &Arc<Self>) {
353+
match self.mutex.notify() {
354+
Ok(task) => self.exec.state.send(Message::Run(task)),
351355
Err(()) => {}
352356
}
353357
}

futures-task/src/arc_wake.rs

-49
This file was deleted.

futures-task/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ pub use crate::spawn::{LocalSpawn, Spawn, SpawnError};
2222

2323
cfg_target_has_atomic! {
2424
#[cfg(feature = "alloc")]
25-
mod arc_wake;
26-
#[cfg(feature = "alloc")]
27-
pub use crate::arc_wake::ArcWake;
25+
#[doc(no_inline)]
26+
pub use alloc::task::Wake;
2827

2928
#[cfg(feature = "alloc")]
3029
mod waker;

futures-task/src/waker.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use super::arc_wake::ArcWake;
1+
use super::Wake;
22
use alloc::sync::Arc;
33
use core::mem;
44
use core::task::{RawWaker, RawWakerVTable, Waker};
55

6-
pub(super) fn waker_vtable<W: ArcWake>() -> &'static RawWakerVTable {
6+
pub(super) fn waker_vtable<W: Wake>() -> &'static RawWakerVTable {
77
&RawWakerVTable::new(
88
clone_arc_raw::<W>,
99
wake_arc_raw::<W>,
@@ -12,48 +12,46 @@ pub(super) fn waker_vtable<W: ArcWake>() -> &'static RawWakerVTable {
1212
)
1313
}
1414

15-
/// Creates a [`Waker`] from an `Arc<impl ArcWake>`.
15+
/// Creates a [`Waker`] from an `Arc<impl Wake>`.
1616
///
1717
/// The returned [`Waker`] will call
18-
/// [`ArcWake.wake()`](ArcWake::wake) if awoken.
18+
/// [`Wake.wake()`](Wake::wake) if awoken.
1919
pub fn waker<W>(wake: Arc<W>) -> Waker
2020
where
21-
W: ArcWake + 'static,
21+
W: Wake + Send + Sync + 'static,
2222
{
23-
let ptr = Arc::into_raw(wake) as *const ();
24-
25-
unsafe { Waker::from_raw(RawWaker::new(ptr, waker_vtable::<W>())) }
23+
wake.into()
2624
}
2725

2826
// FIXME: panics on Arc::clone / refcount changes could wreak havoc on the
2927
// code here. We should guard against this by aborting.
3028

3129
#[allow(clippy::redundant_clone)] // The clone here isn't actually redundant.
32-
unsafe fn increase_refcount<T: ArcWake>(data: *const ()) {
30+
unsafe fn increase_refcount<T: Wake>(data: *const ()) {
3331
// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
3432
let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data as *const T));
3533
// Now increase refcount, but don't drop new refcount either
3634
let _arc_clone: mem::ManuallyDrop<_> = arc.clone();
3735
}
3836

3937
// used by `waker_ref`
40-
unsafe fn clone_arc_raw<T: ArcWake>(data: *const ()) -> RawWaker {
38+
unsafe fn clone_arc_raw<T: Wake>(data: *const ()) -> RawWaker {
4139
increase_refcount::<T>(data);
4240
RawWaker::new(data, waker_vtable::<T>())
4341
}
4442

45-
unsafe fn wake_arc_raw<T: ArcWake>(data: *const ()) {
43+
unsafe fn wake_arc_raw<T: Wake>(data: *const ()) {
4644
let arc: Arc<T> = Arc::from_raw(data as *const T);
47-
ArcWake::wake(arc);
45+
Wake::wake(arc);
4846
}
4947

5048
// used by `waker_ref`
51-
unsafe fn wake_by_ref_arc_raw<T: ArcWake>(data: *const ()) {
49+
unsafe fn wake_by_ref_arc_raw<T: Wake>(data: *const ()) {
5250
// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
5351
let arc = mem::ManuallyDrop::new(Arc::<T>::from_raw(data as *const T));
54-
ArcWake::wake_by_ref(&arc);
52+
Wake::wake_by_ref(&arc);
5553
}
5654

57-
unsafe fn drop_arc_raw<T: ArcWake>(data: *const ()) {
55+
unsafe fn drop_arc_raw<T: Wake>(data: *const ()) {
5856
drop(Arc::<T>::from_raw(data as *const T))
5957
}

futures-task/src/waker_ref.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::arc_wake::ArcWake;
21
use super::waker::waker_vtable;
2+
use super::Wake;
33
use alloc::sync::Arc;
44
use core::marker::PhantomData;
55
use core::mem::ManuallyDrop;
@@ -44,14 +44,14 @@ impl Deref for WakerRef<'_> {
4444
}
4545
}
4646

47-
/// Creates a reference to a [`Waker`] from a reference to `Arc<impl ArcWake>`.
47+
/// Creates a reference to a [`Waker`] from a reference to `Arc<impl Wake>`.
4848
///
4949
/// The resulting [`Waker`] will call
50-
/// [`ArcWake.wake()`](ArcWake::wake) if awoken.
50+
/// [`Wake.wake()`](Wake::wake) if awoken.
5151
#[inline]
5252
pub fn waker_ref<W>(wake: &Arc<W>) -> WakerRef<'_>
5353
where
54-
W: ArcWake,
54+
W: Wake,
5555
{
5656
// simply copy the pointer instead of using Arc::into_raw,
5757
// as we don't actually keep a refcount by using ManuallyDrop.<

futures-test/src/task/wake_counter.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use futures_core::task::Waker;
2-
use futures_util::task::{self, ArcWake};
1+
use futures_core::task::{Wake, Waker};
2+
use futures_util::task;
33
use std::sync::atomic::{AtomicUsize, Ordering};
44
use std::sync::Arc;
55

@@ -29,9 +29,13 @@ struct WakerInner {
2929
count: AtomicUsize,
3030
}
3131

32-
impl ArcWake for WakerInner {
33-
fn wake_by_ref(arc_self: &Arc<Self>) {
34-
let _ = arc_self.count.fetch_add(1, Ordering::SeqCst);
32+
impl Wake for WakerInner {
33+
fn wake(self: Arc<Self>) {
34+
self.wake_by_ref()
35+
}
36+
37+
fn wake_by_ref(self: &Arc<Self>) {
38+
let _ = self.count.fetch_add(1, Ordering::SeqCst);
3539
}
3640
}
3741

futures-util/benches_disabled/bilock.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,33 @@ mod bench {
77
use futures_util::lock::BiLock;
88
use futures_util::lock::BiLockAcquire;
99
use futures_util::lock::BiLockAcquired;
10-
use futures_util::task::ArcWake;
10+
use futures_core::Wake;
1111

1212
use std::sync::Arc;
1313
use test::Bencher;
1414

1515
fn notify_noop() -> Waker {
1616
struct Noop;
1717

18-
impl ArcWake for Noop {
18+
impl Wake for Noop {
1919
fn wake(_: &Arc<Self>) {}
2020
}
2121

22-
ArcWake::into_waker(Arc::new(Noop))
22+
Arc::new(Noop).into()
23+
}
24+
}
25+
26+
27+
/// Pseudo-stream which simply calls `lock.poll()` on `poll`
28+
struct LockStream {
29+
lock: BiLockAcquire<u32>,
30+
}
31+
32+
impl LockStream {
33+
fn new(lock: BiLock<u32>) -> Self {
34+
Self {
35+
lock: lock.lock()
36+
}
2337
}
2438

2539
/// Pseudo-stream which simply calls `lock.poll()` on `poll`

futures-util/src/compat/compat03as01.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::task::{self as task03, ArcWake as ArcWake03, WakerRef};
1+
use crate::task::{self as task03, Wake as ArcWake03, WakerRef};
22
use futures_01::{
33
task as task01, Async as Async01, Future as Future01, Poll as Poll01, Stream as Stream01,
44
};
@@ -184,8 +184,12 @@ impl Current {
184184
}
185185

186186
impl ArcWake03 for Current {
187-
fn wake_by_ref(arc_self: &Arc<Self>) {
188-
arc_self.0.notify();
187+
fn wake(self: Arc<Self>) {
188+
self.wake_by_ref();
189+
}
190+
191+
fn wake_by_ref(self: &Arc<Self>) {
192+
self.0.notify();
189193
}
190194
}
191195

futures-util/src/future/future/shared.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::task::{waker_ref, ArcWake};
1+
use crate::task::{waker_ref, Wake};
22
use futures_core::future::{FusedFuture, Future};
33
use futures_core::task::{Context, Poll, Waker};
44
use slab::Slab;
@@ -347,9 +347,13 @@ where
347347
}
348348
}
349349

350-
impl ArcWake for Notifier {
351-
fn wake_by_ref(arc_self: &Arc<Self>) {
352-
let wakers = &mut *arc_self.wakers.lock().unwrap();
350+
impl Wake for Notifier {
351+
fn wake(self: Arc<Self>) {
352+
self.wake_by_ref();
353+
}
354+
355+
fn wake_by_ref(self: &Arc<Self>) {
356+
let wakers = &mut *self.wakers.lock().unwrap();
353357
if let Some(wakers) = wakers.as_mut() {
354358
for (_key, opt_waker) in wakers {
355359
if let Some(waker) = opt_waker.take() {

futures-util/src/stream/futures_unordered/task.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use alloc::sync::{Arc, Weak};
22
use core::cell::UnsafeCell;
33
use core::sync::atomic::Ordering::{self, SeqCst};
44
use core::sync::atomic::{AtomicBool, AtomicPtr};
5-
65
use super::abort::abort;
6+
use crate::task::{Wake, WakerRef, waker_ref};
77
use super::ReadyToRunQueue;
8-
use crate::task::{waker_ref, ArcWake, WakerRef};
98

109
pub(super) struct Task<Fut> {
1110
// The future
@@ -41,9 +40,13 @@ pub(super) struct Task<Fut> {
4140
unsafe impl<Fut> Send for Task<Fut> {}
4241
unsafe impl<Fut> Sync for Task<Fut> {}
4342

44-
impl<Fut> ArcWake for Task<Fut> {
45-
fn wake_by_ref(arc_self: &Arc<Self>) {
46-
let inner = match arc_self.ready_to_run_queue.upgrade() {
43+
impl<Fut> Wake for Task<Fut> {
44+
fn wake(self: Arc<Self>) {
45+
self.wake_by_ref();
46+
}
47+
48+
fn wake_by_ref(self: &Arc<Self>) {
49+
let inner = match self.ready_to_run_queue.upgrade() {
4750
Some(inner) => inner,
4851
None => return,
4952
};
@@ -60,9 +63,9 @@ impl<Fut> ArcWake for Task<Fut> {
6063
// implementation guarantees that if we set the `queued` flag that
6164
// there's a reference count held by the main `FuturesUnordered` queue
6265
// still.
63-
let prev = arc_self.queued.swap(true, SeqCst);
66+
let prev = self.queued.swap(true, SeqCst);
6467
if !prev {
65-
inner.enqueue(&**arc_self);
68+
inner.enqueue(&**self);
6669
inner.waker.wake();
6770
}
6871
}

futures-util/src/task/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub use futures_task::noop_waker;
2020
pub use futures_task::noop_waker_ref;
2121

2222
cfg_target_has_atomic! {
23-
#[cfg(feature = "alloc")]
24-
pub use futures_task::ArcWake;
23+
#[doc(no_inline)]
24+
pub use alloc::task::Wake;
2525

2626
#[cfg(feature = "alloc")]
2727
pub use futures_task::waker;

futures/tests/object_safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn io() {
4141

4242
#[test]
4343
fn task() {
44-
// `ArcWake`, `SpawnExt` and `LocalSpawnExt` are not object safe.
44+
// ``SpawnExt` and `LocalSpawnExt` are not object safe.
4545
use futures::task::{LocalSpawn, Spawn};
4646

4747
assert_is_object_safe::<&dyn Spawn>();

0 commit comments

Comments
 (0)