From c0946ee21161c896e1f6bf69d7aa140fdba1892f Mon Sep 17 00:00:00 2001 From: peamaeq Date: Thu, 23 Jun 2022 15:56:27 +0800 Subject: [PATCH] Shrink unsafe blocks --- src/join_handle.rs | 30 +++++++++++++++--------------- src/raw.rs | 9 +++++---- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/join_handle.rs b/src/join_handle.rs index f4710cc..4ac8717 100644 --- a/src/join_handle.rs +++ b/src/join_handle.rs @@ -39,8 +39,8 @@ impl JoinHandle { 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. @@ -56,13 +56,13 @@ impl JoinHandle { }; // 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 { @@ -79,7 +79,7 @@ impl JoinHandle { Err(s) => state = s, } } - } + } /// Returns a reference to the tag stored inside the task. @@ -193,15 +193,15 @@ impl Future for JoinHandle { 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); } @@ -210,12 +210,12 @@ impl Future for JoinHandle { // 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 @@ -231,13 +231,13 @@ impl Future for JoinHandle { } // 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 { @@ -251,7 +251,7 @@ impl Future for JoinHandle { Err(s) => state = s, } } - } + } } diff --git a/src/raw.rs b/src/raw.rs index ed3ee97..7b9e9dd 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -112,9 +112,9 @@ 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, }; @@ -122,6 +122,7 @@ where 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), @@ -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.