diff options
| author | bors <bors@rust-lang.org> | 2020-03-23 18:48:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-03-23 18:48:02 +0000 |
| commit | 1edd389cc4c7b5be7a3dd4fe4b986f6017018e54 (patch) | |
| tree | fa016e97f3653421cb503f322cfff32876f5a2bd /src/liballoc | |
| parent | 55299b2ba99432d10f925cd28ff52fe397577371 (diff) | |
| parent | 5b29348cfe67f5ec7b81dbf9cde03eb7a9c8ff01 (diff) | |
| download | rust-1edd389cc4c7b5be7a3dd4fe4b986f6017018e54.tar.gz rust-1edd389cc4c7b5be7a3dd4fe4b986f6017018e54.zip | |
Auto merge of #70330 - Centril:rollup-ts0clvx, r=Centril
Rollup of 9 pull requests Successful merges: - #68700 (Add Wake trait for safe construction of Wakers.) - #69494 (Stabilize --crate-version option in rustdoc) - #70080 (rustc_mir: remove extra space when pretty-printing MIR.) - #70195 (Add test for issue #53275) - #70199 (Revised span-to-lines conversion to produce an empty vec on DUMMY_SP.) - #70299 (add err_machine_stop macro) - #70300 (Reword unused variable warning) - #70315 (Rename remaining occurences of Void to Opaque.) - #70318 (Split long derive lists into two derive attributes.) Failed merges: r? @ghost
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/lib.rs | 2 | ||||
| -rw-r--r-- | src/liballoc/task.rs | 87 |
2 files changed, 89 insertions, 0 deletions
diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index d877ac6ac5c..d55a1a3b635 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -161,6 +161,8 @@ pub mod str; pub mod string; #[cfg(target_has_atomic = "ptr")] pub mod sync; +#[cfg(target_has_atomic = "ptr")] +pub mod task; #[cfg(test)] mod tests; pub mod vec; diff --git a/src/liballoc/task.rs b/src/liballoc/task.rs new file mode 100644 index 00000000000..981095302c7 --- /dev/null +++ b/src/liballoc/task.rs @@ -0,0 +1,87 @@ +#![unstable(feature = "wake_trait", issue = "69912")] +//! Types and Traits for working with asynchronous tasks. +use core::mem::{self, ManuallyDrop}; +use core::task::{RawWaker, RawWakerVTable, Waker}; + +use crate::sync::Arc; + +/// The implementation of waking a task on an executor. +/// +/// This trait can be used to create a [`Waker`]. An executor can define an +/// implementation of this trait, and use that to construct a Waker to pass +/// to the tasks that are executed on that executor. +/// +/// This trait is a memory-safe and ergonomic alternative to constructing a +/// [`RawWaker`]. It supports the common executor design in which the data +/// used to wake up a task is stored in an [`Arc`]. Some executors (especially +/// those for embedded systems) cannot use this API, which is why [`RawWaker`] +/// exists as an alternative for those systems. +#[unstable(feature = "wake_trait", issue = "69912")] +pub trait Wake { + /// Wake this task. + #[unstable(feature = "wake_trait", issue = "69912")] + fn wake(self: Arc<Self>); + + /// Wake this task without consuming the waker. + /// + /// If an executor supports a cheaper way to wake without consuming the + /// waker, it should override this method. By default, it clones the + /// [`Arc`] and calls `wake` on the clone. + #[unstable(feature = "wake_trait", issue = "69912")] + fn wake_by_ref(self: &Arc<Self>) { + self.clone().wake(); + } +} + +#[unstable(feature = "wake_trait", issue = "69912")] +impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for Waker { + fn from(waker: Arc<W>) -> Waker { + // SAFETY: This is safe because raw_waker safely constructs + // a RawWaker from Arc<W>. + unsafe { Waker::from_raw(raw_waker(waker)) } + } +} + +#[unstable(feature = "wake_trait", issue = "69912")] +impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker { + fn from(waker: Arc<W>) -> RawWaker { + raw_waker(waker) + } +} + +// NB: This private function for constructing a RawWaker is used, rather than +// inlining this into the `From<Arc<W>> for RawWaker` impl, to ensure that +// the safety of `From<Arc<W>> for Waker` does not depend on the correct +// trait dispatch - instead both impls call this function directly and +// explicitly. +#[inline(always)] +fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker { + // Increment the reference count of the arc to clone it. + unsafe fn clone_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) -> RawWaker { + let waker: Arc<W> = Arc::from_raw(waker as *const W); + mem::forget(Arc::clone(&waker)); + raw_waker(waker) + } + + // Wake by value, moving the Arc into the Wake::wake function + unsafe fn wake<W: Wake + Send + Sync + 'static>(waker: *const ()) { + let waker: Arc<W> = Arc::from_raw(waker as *const W); + <W as Wake>::wake(waker); + } + + // Wake by reference, wrap the waker in ManuallyDrop to avoid dropping it + unsafe fn wake_by_ref<W: Wake + Send + Sync + 'static>(waker: *const ()) { + let waker: ManuallyDrop<Arc<W>> = ManuallyDrop::new(Arc::from_raw(waker as *const W)); + <W as Wake>::wake_by_ref(&waker); + } + + // Decrement the reference count of the Arc on drop + unsafe fn drop_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) { + mem::drop(Arc::from_raw(waker as *const W)); + } + + RawWaker::new( + Arc::into_raw(waker) as *const (), + &RawWakerVTable::new(clone_waker::<W>, wake::<W>, wake_by_ref::<W>, drop_waker::<W>), + ) +} |
