Add spawn_local function
diff --git a/src/join_handle.rs b/src/join_handle.rs
index bd9366f..b3c4da5 100644
--- a/src/join_handle.rs
+++ b/src/join_handle.rs
@@ -24,8 +24,8 @@
pub(crate) _marker: PhantomData<(R, T)>,
}
-unsafe impl<R, T> Send for JoinHandle<R, T> {}
-unsafe impl<R, T> Sync for JoinHandle<R, T> {}
+unsafe impl<R: Send, T> Send for JoinHandle<R, T> {}
+unsafe impl<R: Send, T> Sync for JoinHandle<R, T> {}
impl<R, T> Unpin for JoinHandle<R, T> {}
diff --git a/src/lib.rs b/src/lib.rs
index 3f61ea4..a265679 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -23,7 +23,7 @@
//! # let (task, handle) = async_task::spawn(future, schedule, ());
//! ```
//!
-//! A task is constructed using the [`spawn`] function:
+//! A task is constructed using either [`spawn`] or [`spawn_local`]:
//!
//! ```
//! # let (sender, receiver) = crossbeam::channel::unbounded();
@@ -93,6 +93,7 @@
//! union of the future and its output.
//!
//! [`spawn`]: fn.spawn.html
+//! [`spawn_local`]: fn.spawn_local.html
//! [`Task`]: struct.Task.html
//! [`JoinHandle`]: struct.JoinHandle.html
@@ -108,4 +109,4 @@
mod utils;
pub use crate::join_handle::JoinHandle;
-pub use crate::task::{spawn, Task};
+pub use crate::task::{spawn, spawn_local, Task};
diff --git a/src/raw.rs b/src/raw.rs
index 3b993a3..2c47f0c 100644
--- a/src/raw.rs
+++ b/src/raw.rs
@@ -95,15 +95,13 @@
impl<F, R, S, T> RawTask<F, R, S, T>
where
- F: Future<Output = R> + Send + 'static,
- R: Send + 'static,
+ F: Future<Output = R> + 'static,
S: Fn(Task<T>) + Send + Sync + 'static,
- T: Send + 'static,
{
/// Allocates a task with the given `future` and `schedule` function.
///
/// It is assumed that initially only the `Task` reference and the `JoinHandle` exist.
- pub(crate) fn allocate(tag: T, future: F, schedule: S) -> NonNull<()> {
+ pub(crate) fn allocate(future: F, schedule: S, tag: T) -> NonNull<()> {
// Compute the layout of the task for allocation. Abort if the computation fails.
let task_layout = abort_on_panic(|| Self::task_layout());
@@ -592,17 +590,13 @@
/// A guard that closes the task if polling its future panics.
struct Guard<F, R, S, T>(RawTask<F, R, S, T>)
where
- F: Future<Output = R> + Send + 'static,
- R: Send + 'static,
- S: Fn(Task<T>) + Send + Sync + 'static,
- T: Send + 'static;
+ F: Future<Output = R> + 'static,
+ S: Fn(Task<T>) + Send + Sync + 'static;
impl<F, R, S, T> Drop for Guard<F, R, S, T>
where
- F: Future<Output = R> + Send + 'static,
- R: Send + 'static,
+ F: Future<Output = R> + 'static,
S: Fn(Task<T>) + Send + Sync + 'static,
- T: Send + 'static,
{
fn drop(&mut self) {
let raw = self.0;
diff --git a/src/task.rs b/src/task.rs
index 42a4024..b12cace 100644
--- a/src/task.rs
+++ b/src/task.rs
@@ -1,8 +1,11 @@
use std::fmt;
use std::future::Future;
use std::marker::PhantomData;
-use std::mem;
+use std::mem::{self, ManuallyDrop};
+use std::pin::Pin;
use std::ptr::NonNull;
+use std::task::{Context, Poll};
+use std::thread::{self, ThreadId};
use crate::header::Header;
use crate::raw::RawTask;
@@ -16,8 +19,13 @@
/// When run, the task polls `future`. When woken up, it gets scheduled for running by the
/// `schedule` function. Argument `tag` is an arbitrary piece of data stored inside the task.
///
+/// If you need to spawn a future that does not implement [`Send`], consider using the
+/// [`spawn_local`] function instead.
+///
/// [`Task`]: struct.Task.html
/// [`JoinHandle`]: struct.JoinHandle.html
+/// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html
+/// [`spawn_local`]: fn.spawn_local.html
///
/// # Examples
///
@@ -43,7 +51,95 @@
S: Fn(Task<T>) + Send + Sync + 'static,
T: Send + Sync + 'static,
{
- let raw_task = RawTask::<F, R, S, T>::allocate(tag, future, schedule);
+ let raw_task = RawTask::<F, R, S, T>::allocate(future, schedule, tag);
+ let task = Task {
+ raw_task,
+ _marker: PhantomData,
+ };
+ let handle = JoinHandle {
+ raw_task,
+ _marker: PhantomData,
+ };
+ (task, handle)
+}
+
+/// Creates a new local task.
+///
+/// This constructor returns a [`Task`] reference that runs the future and a [`JoinHandle`] that
+/// awaits its result.
+///
+/// When run, the task polls `future`. When woken up, it gets scheduled for running by the
+/// `schedule` function. Argument `tag` is an arbitrary piece of data stored inside the task.
+///
+/// Unlike [`spawn`], this function does not require the future to implement [`Send`]. If the
+/// [`Task`] reference is run or dropped on a thread it was not created on, a panic will occur.
+///
+/// [`Task`]: struct.Task.html
+/// [`JoinHandle`]: struct.JoinHandle.html
+/// [`spawn`]: fn.spawn.html
+/// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html
+///
+/// # Examples
+///
+/// ```
+/// use crossbeam::channel;
+///
+/// // The future inside the task.
+/// let future = async {
+/// println!("Hello, world!");
+/// };
+///
+/// // If the task gets woken up, it will be sent into this channel.
+/// let (s, r) = channel::unbounded();
+/// let schedule = move |task| s.send(task).unwrap();
+///
+/// // Create a task with the future and the schedule function.
+/// let (task, handle) = async_task::spawn_local(future, schedule, ());
+/// ```
+pub fn spawn_local<F, R, S, T>(future: F, schedule: S, tag: T) -> (Task<T>, JoinHandle<R, T>)
+where
+ F: Future<Output = R> + 'static,
+ R: 'static,
+ S: Fn(Task<T>) + Send + Sync + 'static,
+ T: Send + Sync + 'static,
+{
+ thread_local! {
+ static ID: ThreadId = thread::current().id();
+ }
+
+ struct Checked<F> {
+ id: ThreadId,
+ inner: ManuallyDrop<F>,
+ }
+
+ impl<F> Drop for Checked<F> {
+ fn drop(&mut self) {
+ if ID.with(|id| *id) != self.id {
+ panic!("local task dropped by a thread that didn't spawn it");
+ }
+ unsafe {
+ ManuallyDrop::drop(&mut self.inner);
+ }
+ }
+ }
+
+ impl<F: Future> Future for Checked<F> {
+ type Output = F::Output;
+
+ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ if ID.with(|id| *id) != self.id {
+ panic!("local task polled by a thread that didn't spawn it");
+ }
+ unsafe { self.map_unchecked_mut(|c| &mut *c.inner).poll(cx) }
+ }
+ }
+
+ let future = Checked {
+ id: ID.with(|id| *id),
+ inner: ManuallyDrop::new(future),
+ };
+
+ let raw_task = RawTask::<_, R, S, T>::allocate(future, schedule, tag);
let task = Task {
raw_task,
_marker: PhantomData,