runtime: use Arc::increment_strong_count instead of mem::forget (#5872)

This commit is contained in:
Alice Ryhl
2023-07-17 13:46:41 +02:00
committed by GitHub
parent 05feb2b0bb
commit 267a231581
4 changed files with 11 additions and 28 deletions
+5 -13
View File
@@ -222,7 +222,6 @@ impl UnparkThread {
use crate::loom::thread::AccessError;
use std::future::Future;
use std::marker::PhantomData;
use std::mem;
use std::rc::Rc;
use std::task::{RawWaker, RawWakerVTable, Waker};
@@ -317,16 +316,12 @@ unsafe fn unparker_to_raw_waker(unparker: Arc<Inner>) -> RawWaker {
}
unsafe fn clone(raw: *const ()) -> RawWaker {
let unparker = Inner::from_raw(raw);
// Increment the ref count
mem::forget(unparker.clone());
unparker_to_raw_waker(unparker)
Arc::increment_strong_count(raw as *const Inner);
unparker_to_raw_waker(Inner::from_raw(raw))
}
unsafe fn drop_waker(raw: *const ()) {
let _ = Inner::from_raw(raw);
drop(Inner::from_raw(raw));
}
unsafe fn wake(raw: *const ()) {
@@ -335,11 +330,8 @@ unsafe fn wake(raw: *const ()) {
}
unsafe fn wake_by_ref(raw: *const ()) {
let unparker = Inner::from_raw(raw);
unparker.unpark();
// We don't actually own a reference to the unparker
mem::forget(unparker);
let raw = raw as *const Inner;
(*raw).unpark();
}
#[cfg(loom)]
+1 -1
View File
@@ -63,7 +63,7 @@ cfg_loom! {
// Make sure debug assertions are enabled
#[cfg(not(debug_assertions))]
compiler_error!("these tests require debug assertions to be enabled");
compile_error!("these tests require debug assertions to be enabled");
}
cfg_not_loom! {
+4 -5
View File
@@ -1,6 +1,5 @@
use crate::sync::Notify;
use std::future::Future;
use std::mem::ManuallyDrop;
use std::sync::Arc;
use std::task::{Context, RawWaker, RawWakerVTable, Waker};
@@ -12,16 +11,16 @@ fn notify_clones_waker_before_lock() {
const VTABLE: &RawWakerVTable = &RawWakerVTable::new(clone_w, wake, wake_by_ref, drop_w);
unsafe fn clone_w(data: *const ()) -> RawWaker {
let arc = ManuallyDrop::new(Arc::<Notify>::from_raw(data as *const Notify));
let ptr = data as *const Notify;
Arc::<Notify>::increment_strong_count(ptr);
// Or some other arbitrary code that shouldn't be executed while the
// Notify wait list is locked.
arc.notify_one();
let _arc_clone: ManuallyDrop<_> = arc.clone();
(*ptr).notify_one();
RawWaker::new(data, VTABLE)
}
unsafe fn drop_w(data: *const ()) {
let _ = Arc::<Notify>::from_raw(data as *const Notify);
drop(Arc::<Notify>::from_raw(data as *const Notify));
}
unsafe fn wake(_data: *const ()) {
+1 -9
View File
@@ -50,16 +50,8 @@ fn waker_vtable<W: Wake>() -> &'static RawWakerVTable {
)
}
unsafe fn inc_ref_count<T: Wake>(data: *const ()) {
// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
let arc = ManuallyDrop::new(Arc::<T>::from_raw(data as *const T));
// Now increase refcount, but don't drop new refcount either
let _arc_clone: ManuallyDrop<_> = arc.clone();
}
unsafe fn clone_arc_raw<T: Wake>(data: *const ()) -> RawWaker {
inc_ref_count::<T>(data);
Arc::<T>::increment_strong_count(data as *const T);
RawWaker::new(data, waker_vtable::<T>())
}