Skip to content

Commit

Permalink
change waker getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnwa committed Sep 5, 2020
1 parent 40aba75 commit 43a88fa
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/mutex.rs
Expand Up @@ -128,7 +128,7 @@ impl<'a, T: ?Sized> Future for MutexGuardFuture<'a, T> {
if Some(current) == self.id.checked_sub(1) {
let _ = self.mutex.waker.compare_exchange_weak(
null_mut(),
get_waker_ptr(cx.waker()),
Box::into_raw(Box::new(cx.waker().clone())),
Ordering::AcqRel,
Ordering::Acquire,
);
Expand All @@ -152,7 +152,7 @@ impl<T: ?Sized> Future for MutexOwnedGuardFuture<T> {
if Some(current) == self.id.checked_sub(1) {
let _ = self.mutex.waker.compare_exchange_weak(
null_mut(),
get_waker_ptr(cx.waker()),
Box::into_raw(Box::new(cx.waker().clone())),
Ordering::AcqRel,
Ordering::Acquire,
);
Expand All @@ -163,12 +163,6 @@ impl<T: ?Sized> Future for MutexOwnedGuardFuture<T> {
}
}

#[inline(always)]
fn get_waker_ptr(waker: &Waker) -> *mut Waker {
let waker = waker.clone();
Box::into_raw(Box::new(waker))
}

impl<T: ?Sized> Deref for MutexGuard<'_, T> {
type Target = T;

Expand Down Expand Up @@ -233,13 +227,13 @@ impl<T: ?Sized> Drop for MutexOwnedGuardFuture<T> {
}
}

#[inline(always)]
#[inline]
fn wake_ptr(waker_ptr: &AtomicPtr<Waker>) {
let waker_ptr = waker_ptr.swap(null_mut(), Ordering::AcqRel);

unsafe {
if let Some(waker) = waker_ptr.as_ref() {
waker.wake_by_ref();
if let Err(waker_ptr) =
waker_ptr.compare_exchange_weak(null_mut(), null_mut(), Ordering::AcqRel, Ordering::Acquire)
{
unsafe {
(*waker_ptr).wake_by_ref();
waker_ptr.drop_in_place();
}
}
Expand Down

0 comments on commit 43a88fa

Please sign in to comment.