Э
unsafe impl<T: ?Sized + Send> Send
PhantomData<&mut T> в MutexGuard, и оно само там везде нужный Send проставит.Size: a a a
Э
unsafe impl<T: ?Sized + Send> Send
PhantomData<&mut T> в MutexGuard, и оно само там везде нужный Send проставит.Э
Э
Relaxed не используется, так что поведение будет идентично и на стронк и на weak.Э

waker.clone() и хранить склонированное.Э
Э
WakerPtr {
ptr: AtomicPtr<Waker>,
}
impl WakerPtr {
const fn new() -> Self { Self { ptr: AtomicPtr::new(null_ptr()) } }
fn swap(&self, b: Box<Waker>) -> Option<Box<Waker>> {
let new_waker = b.into_raw();
let old = self.ptr.swap(new_waker);
if old.is_null() {
None
} else {
// SAFETY:
// the pointer could be either `NULL` or valid,
// and it is not `NULL` here.
unsafe {
Some(Box::from_raw(old))
}
}
}
}Э
WakerPtr {
ptr: AtomicPtr<Waker>,
}
impl WakerPtr {
const fn new() -> Self { Self { ptr: AtomicPtr::new(null_ptr()) } }
fn swap(&self, b: Box<Waker>) -> Option<Box<Waker>> {
let new_waker = b.into_raw();
let old = self.ptr.swap(new_waker);
if old.is_null() {
None
} else {
// SAFETY:
// the pointer could be either `NULL` or valid,
// and it is not `NULL` here.
unsafe {
Some(Box::from_raw(old))
}
}
}
}KK
Э
impl WakerPtr {
// Sets the waker if it is unset.
// Does nothing otherwise.
fn set_once(&self, w: &Waker) {
if self.ptr.load() == null_mut() {
let new_waker = Box::into_raw(Box::new(w.clone()));
if self.ptr.compare_exchange(null_mut(), new_waker).is_err() {
// Some flaw in atomic fences, and another thread already set the waker.
// SAFETY:
// `new_waker` was *not* set, thus there's no owner anywhere and it is safe.
unsafe { drop(Box::from_raw(new_waker)) }
}
}
}
}Э

OA
OA
Э
OA
Э
OA
Э
DT