Skip to content

Commit 5af0bb8

Browse files
committed
Auto merge of #54732 - cramertj:waker, r=aturon
LocalWaker and Waker cleanups r? @aturon
2 parents e1041c6 + 00e0565 commit 5af0bb8

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

src/libcore/task/wake.rs

+33-32
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
reason = "futures in libcore are unstable",
1313
issue = "50547")]
1414

15-
use {fmt, mem};
15+
use fmt;
1616
use marker::Unpin;
1717
use ptr::NonNull;
1818

@@ -63,6 +63,20 @@ impl Waker {
6363
pub fn will_wake(&self, other: &Waker) -> bool {
6464
self.inner == other.inner
6565
}
66+
67+
/// Returns whether or not this `Waker` and `other` `LocalWaker` awaken
68+
/// the same task.
69+
///
70+
/// This function works on a best-effort basis, and may return false even
71+
/// when the `Waker`s would awaken the same task. However, if this function
72+
/// returns true, it is guaranteed that the `Waker`s will awaken the same
73+
/// task.
74+
///
75+
/// This function is primarily used for optimization purposes.
76+
#[inline]
77+
pub fn will_wake_local(&self, other: &LocalWaker) -> bool {
78+
self.will_wake(&other.0)
79+
}
6680
}
6781

6882
impl Clone for Waker {
@@ -97,9 +111,8 @@ impl Drop for Waker {
97111
/// Task executors can use this type to implement more optimized singlethreaded wakeup
98112
/// behavior.
99113
#[repr(transparent)]
100-
pub struct LocalWaker {
101-
inner: NonNull<dyn UnsafeWake>,
102-
}
114+
#[derive(Clone)]
115+
pub struct LocalWaker(Waker);
103116

104117
impl Unpin for LocalWaker {}
105118
impl !Send for LocalWaker {}
@@ -120,7 +133,16 @@ impl LocalWaker {
120133
/// on the current thread.
121134
#[inline]
122135
pub unsafe fn new(inner: NonNull<dyn UnsafeWake>) -> Self {
123-
LocalWaker { inner }
136+
LocalWaker(Waker::new(inner))
137+
}
138+
139+
/// Borrows this `LocalWaker` as a `Waker`.
140+
///
141+
/// `Waker` is nearly identical to `LocalWaker`, but is threadsafe
142+
/// (implements `Send` and `Sync`).
143+
#[inline]
144+
pub fn as_waker(&self) -> &Waker {
145+
&self.0
124146
}
125147

126148
/// Converts this `LocalWaker` into a `Waker`.
@@ -129,13 +151,13 @@ impl LocalWaker {
129151
/// (implements `Send` and `Sync`).
130152
#[inline]
131153
pub fn into_waker(self) -> Waker {
132-
self.into()
154+
self.0
133155
}
134156

135157
/// Wake up the task associated with this `LocalWaker`.
136158
#[inline]
137159
pub fn wake(&self) {
138-
unsafe { self.inner.as_ref().wake_local() }
160+
unsafe { self.0.inner.as_ref().wake_local() }
139161
}
140162

141163
/// Returns whether or not this `LocalWaker` and `other` `LocalWaker` awaken the same task.
@@ -148,7 +170,7 @@ impl LocalWaker {
148170
/// This function is primarily used for optimization purposes.
149171
#[inline]
150172
pub fn will_wake(&self, other: &LocalWaker) -> bool {
151-
self.inner == other.inner
173+
self.0.will_wake(&other.0)
152174
}
153175

154176
/// Returns whether or not this `LocalWaker` and `other` `Waker` awaken the same task.
@@ -161,45 +183,24 @@ impl LocalWaker {
161183
/// This function is primarily used for optimization purposes.
162184
#[inline]
163185
pub fn will_wake_nonlocal(&self, other: &Waker) -> bool {
164-
self.inner == other.inner
186+
self.0.will_wake(other)
165187
}
166188
}
167189

168190
impl From<LocalWaker> for Waker {
169191
#[inline]
170192
fn from(local_waker: LocalWaker) -> Self {
171-
let inner = local_waker.inner;
172-
mem::forget(local_waker);
173-
Waker { inner }
174-
}
175-
}
176-
177-
impl Clone for LocalWaker {
178-
#[inline]
179-
fn clone(&self) -> Self {
180-
let waker = unsafe { self.inner.as_ref().clone_raw() };
181-
let inner = waker.inner;
182-
mem::forget(waker);
183-
LocalWaker { inner }
193+
local_waker.0
184194
}
185195
}
186196

187197
impl fmt::Debug for LocalWaker {
188198
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189-
f.debug_struct("Waker")
199+
f.debug_struct("LocalWaker")
190200
.finish()
191201
}
192202
}
193203

194-
impl Drop for LocalWaker {
195-
#[inline]
196-
fn drop(&mut self) {
197-
unsafe {
198-
self.inner.as_ref().drop_raw()
199-
}
200-
}
201-
}
202-
203204
/// An unsafe trait for implementing custom memory management for a `Waker` or `LocalWaker`.
204205
///
205206
/// A `Waker` conceptually is a cloneable trait object for `Wake`, and is

0 commit comments

Comments
 (0)