Skip to content
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

Shrink unsafe blocks #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 15 additions & 15 deletions src/join_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl<R, T> JoinHandle<R, T> {
let ptr = self.raw_task.as_ptr();
let header = ptr as *const Header;

unsafe {
let mut state = (*header).state.load(Ordering::Acquire);

let mut state = unsafe { (*header).state.load(Ordering::Acquire) };

loop {
// If the task has been completed or closed, it can't be cancelled.
Expand All @@ -56,13 +56,13 @@ impl<R, T> JoinHandle<R, T> {
};

// Mark the task as closed.
match (*header).state.compare_exchange_weak(
match unsafe { (*header).state.compare_exchange_weak(
state,
new,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => {
) }{
Ok(_) => unsafe {
// If the task is not scheduled nor running, schedule it one more time so
// that its future gets dropped by the executor.
if state & (SCHEDULED | RUNNING) == 0 {
Expand All @@ -79,7 +79,7 @@ impl<R, T> JoinHandle<R, T> {
Err(s) => state = s,
}
}
}

}

/// Returns a reference to the tag stored inside the task.
Expand Down Expand Up @@ -193,15 +193,15 @@ impl<R, T> Future for JoinHandle<R, T> {
let ptr = self.raw_task.as_ptr();
let header = ptr as *const Header;

unsafe {
let mut state = (*header).state.load(Ordering::Acquire);

let mut state = unsafe { (*header).state.load(Ordering::Acquire) };

loop {
// If the task has been closed, notify the awaiter and return `None`.
if state & CLOSED != 0 {
// Even though the awaiter is most likely the current task, it could also be
// another task.
(*header).notify(Some(cx.waker()));
unsafe { (*header).notify(Some(cx.waker())) };
return Poll::Ready(None);
}

Expand All @@ -210,12 +210,12 @@ impl<R, T> Future for JoinHandle<R, T> {
// Replace the waker with one associated with the current task. We need a
// safeguard against panics because dropping the previous waker can panic.
abort_on_panic(|| {
(*header).register(cx.waker());
unsafe { (*header).register(cx.waker()) };
});

// Reload the state after registering. It is possible that the task became
// completed or closed just before registration so we need to check for that.
state = (*header).state.load(Ordering::Acquire);
state = unsafe { (*header).state.load(Ordering::Acquire) };

// If the task has been closed, return `None`. We do not need to notify the
// awaiter here, since we have replaced the waker above, and the executor can
Expand All @@ -231,13 +231,13 @@ impl<R, T> Future for JoinHandle<R, T> {
}

// Since the task is now completed, mark it as closed in order to grab its output.
match (*header).state.compare_exchange(
match unsafe { (*header).state.compare_exchange(
state,
state | CLOSED,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => {
) }{
Ok(_) => unsafe {
// Notify the awaiter. Even though the awaiter is most likely the current
// task, it could also be another task.
if state & AWAITER != 0 {
Expand All @@ -251,7 +251,7 @@ impl<R, T> Future for JoinHandle<R, T> {
Err(s) => state = s,
}
}
}

}
}

Expand Down
9 changes: 5 additions & 4 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ where
// Compute the layout of the task for allocation. Abort if the computation fails.
let task_layout = abort_on_panic(|| Self::task_layout());

unsafe {

// Allocate enough space for the entire task.
let raw_task = match NonNull::new(alloc::alloc::alloc(task_layout.layout) as *mut ()) {
let raw_task = match NonNull::new(unsafe { alloc::alloc::alloc(task_layout.layout) } as *mut ()) {
None => abort(),
Some(p) => p,
};

let raw = Self::from_ptr(raw_task.as_ptr());

// Write the header as the first field of the task.
unsafe {
(raw.header as *mut Header).write(Header {
state: AtomicUsize::new(SCHEDULED | HANDLE | REFERENCE),
awaiter: UnsafeCell::new(None),
Expand All @@ -143,10 +144,10 @@ where
(raw.schedule as *mut S).write(schedule);

// Write the future as the fourth field of the task.
raw.future.write(future);
raw.future.write(future) };

raw_task
}

}

/// Creates a `RawTask` from a raw task pointer.
Expand Down