Skip to content

Add waker and conversion to raw pointer #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/join_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::marker::{PhantomData, Unpin};
use core::pin::Pin;
use core::ptr::NonNull;
use core::sync::atomic::Ordering;
use core::task::{Context, Poll};
use core::task::{Context, Poll, Waker};

use crate::header::Header;
use crate::state::*;
Expand Down Expand Up @@ -92,6 +92,17 @@ impl<R, T> JoinHandle<R, T> {
&*raw
}
}

/// Returns a waker associated with the task.
pub fn waker(&self) -> Waker {
let ptr = self.raw_task.as_ptr();
let header = ptr as *const Header;

unsafe {
let raw_waker = ((*header).vtable.clone_waker)(ptr);
Waker::from_raw(raw_waker)
}
}
}

impl<R, T> Drop for JoinHandle<R, T> {
Expand Down
4 changes: 4 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub(crate) struct TaskVTable {

/// Runs the task.
pub(crate) run: unsafe fn(*const ()),

/// Creates a new waker associated with the task.
pub(crate) clone_waker: unsafe fn(ptr: *const ()) -> RawWaker,
}

/// Memory layout of a task.
Expand Down Expand Up @@ -131,6 +134,7 @@ where
drop_task: Self::drop_task,
destroy: Self::destroy,
run: Self::run,
clone_waker: Self::clone_waker,
},
});

Expand Down
37 changes: 36 additions & 1 deletion src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::marker::PhantomData;
use core::mem::{self, ManuallyDrop};
use core::pin::Pin;
use core::ptr::NonNull;
use core::task::{Context, Poll};
use core::task::{Context, Poll, Waker};

use crate::header::Header;
use crate::raw::RawTask;
Expand Down Expand Up @@ -264,6 +264,41 @@ impl<T> Task<T> {
&*raw
}
}

/// Converts this task into a raw pointer to the tag.
pub fn into_raw(self) -> *const T {
let offset = Header::offset_tag::<T>();
let ptr = self.raw_task.as_ptr();
mem::forget(self);

unsafe { (ptr as *mut u8).add(offset) as *const T }
}

/// Converts a raw pointer to the tag into a task.
///
/// This method should only be used with raw pointers returned from [`into_raw`].
///
/// [`into_raw`]: #method.into_raw
pub unsafe fn from_raw(raw: *const T) -> Task<T> {
let offset = Header::offset_tag::<T>();
let ptr = (raw as *mut u8).sub(offset) as *mut ();

Task {
raw_task: NonNull::new_unchecked(ptr),
_marker: PhantomData,
}
}

/// Returns a waker associated with this task.
pub fn waker(&self) -> Waker {
let ptr = self.raw_task.as_ptr();
let header = ptr as *const Header;

unsafe {
let raw_waker = ((*header).vtable.clone_waker)(ptr);
Waker::from_raw(raw_waker)
}
}
}

impl<T> Drop for Task<T> {
Expand Down
36 changes: 36 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,39 @@ fn drop_inside_schedule() {
);
task.schedule();
}

#[test]
fn waker() {
let (s, r) = channel::unbounded();
let schedule = move |t| s.send(t).unwrap();
let (task, handle) = async_task::spawn(
future::poll_fn(|_| Poll::<()>::Pending),
schedule,
Box::new(0),
);

assert!(r.is_empty());
let w = task.waker();
task.run();
w.wake();

let task = r.recv().unwrap();
task.run();
handle.waker().wake();

r.recv().unwrap();
}

#[test]
fn raw() {
let (task, _handle) = async_task::spawn(async {}, |_| panic!(), Box::new(AtomicUsize::new(7)));

let a = task.into_raw();
let task = unsafe {
(*a).fetch_add(1, Ordering::SeqCst);
Task::from_raw(a)
};

assert_eq!(task.tag().load(Ordering::SeqCst), 8);
task.run();
}