Skip to content

Commit

Permalink
glib: Make WeakRef cheaply cloneable
Browse files Browse the repository at this point in the history
By using an `Arc` instead of a `Box`, we can make `WeakRef::clone` a
cheap operation, compared to taking a global write lock for setting a
`GWeakRef`. The extra space used for the refcounts should be a fine
trade-off.
  • Loading branch information
heftig committed Mar 6, 2023
1 parent 6bdbfe0 commit cfd0b86
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
// rustdoc-stripper-ignore-next
//! `IMPL` Object wrapper implementation and `Object` binding.
use std::{cmp, fmt, hash, marker::PhantomData, mem, mem::ManuallyDrop, ops, pin::Pin, ptr};
use std::{
cmp, fmt, hash,
marker::PhantomData,
mem::{self, ManuallyDrop},
ops,
pin::Pin,
ptr,
sync::Arc,
};

use crate::{
closure::TryFromClosureReturnValue,
Expand Down Expand Up @@ -3029,9 +3037,12 @@ impl<T: ObjectType> ObjectExt for T {
#[inline]
fn downgrade(&self) -> WeakRef<T> {
unsafe {
let w = WeakRef(Box::pin(mem::zeroed()), PhantomData);
let w = WeakRef(Arc::pin(WeakRefInner {
ffi: mem::zeroed(),
phantom: PhantomData,
}));
gobject_ffi::g_weak_ref_init(
mut_override(&*w.0),
mut_override(&w.0.ffi),
self.as_object_ref().to_glib_none().0,
);
w
Expand Down Expand Up @@ -3375,7 +3386,13 @@ impl<T: ObjectType> WeakRefNotify<T> {
/// A weak reference to an object.
#[derive(Debug)]
#[doc(alias = "GWeakRef")]
pub struct WeakRef<T: ObjectType>(Pin<Box<gobject_ffi::GWeakRef>>, PhantomData<*mut T>);
pub struct WeakRef<T: ObjectType>(Pin<Arc<WeakRefInner<T>>>);

#[derive(Debug)]
struct WeakRefInner<T: ObjectType> {
ffi: gobject_ffi::GWeakRef,
phantom: PhantomData<*mut T>,
}

impl<T: ObjectType> WeakRef<T> {
// rustdoc-stripper-ignore-next
Expand All @@ -3385,11 +3402,11 @@ impl<T: ObjectType> WeakRef<T> {
#[inline]
pub fn new() -> WeakRef<T> {
unsafe {
let mut w = WeakRef(Box::pin(mem::zeroed()), PhantomData);
gobject_ffi::g_weak_ref_init(
Pin::as_mut(&mut w.0).get_unchecked_mut(),
ptr::null_mut(),
);
let w = WeakRef(Arc::pin(WeakRefInner {
ffi: mem::zeroed(),
phantom: PhantomData,
}));
gobject_ffi::g_weak_ref_init(mut_override(&w.0.ffi), ptr::null_mut());
w
}
}
Expand All @@ -3401,7 +3418,7 @@ impl<T: ObjectType> WeakRef<T> {
pub fn set(&self, obj: Option<&T>) {
unsafe {
gobject_ffi::g_weak_ref_set(
mut_override(Pin::as_ref(&self.0).get_ref()),
mut_override(&self.0.ffi),
obj.map_or(std::ptr::null_mut(), |obj| {
obj.as_object_ref().to_glib_none().0
}),
Expand All @@ -3417,7 +3434,7 @@ impl<T: ObjectType> WeakRef<T> {
#[inline]
pub fn upgrade(&self) -> Option<T> {
unsafe {
let ptr = gobject_ffi::g_weak_ref_get(mut_override(Pin::as_ref(&self.0).get_ref()));
let ptr = gobject_ffi::g_weak_ref_get(mut_override(&self.0.ffi));
if ptr.is_null() {
None
} else {
Expand All @@ -3428,29 +3445,19 @@ impl<T: ObjectType> WeakRef<T> {
}
}

impl<T: ObjectType> Drop for WeakRef<T> {
impl<T: ObjectType> Drop for WeakRefInner<T> {
#[inline]
fn drop(&mut self) {
unsafe {
gobject_ffi::g_weak_ref_clear(Pin::as_mut(&mut self.0).get_unchecked_mut());
gobject_ffi::g_weak_ref_clear(&mut self.ffi);
}
}
}

impl<T: ObjectType> Clone for WeakRef<T> {
#[inline]
fn clone(&self) -> Self {
unsafe {
let o = self.upgrade();

let mut c = WeakRef(Box::pin(mem::zeroed()), PhantomData);
gobject_ffi::g_weak_ref_init(
Pin::as_mut(&mut c.0).get_unchecked_mut(),
o.to_glib_none().0 as *mut gobject_ffi::GObject,
);

c
}
Self(self.0.clone())
}
}

Expand All @@ -3461,27 +3468,27 @@ impl<T: ObjectType> Default for WeakRef<T> {
}
}

unsafe impl<T: ObjectType + Sync + Sync> Sync for WeakRef<T> {}
unsafe impl<T: ObjectType + Send + Sync> Send for WeakRef<T> {}
unsafe impl<T: ObjectType + Send + Sync> Sync for WeakRefInner<T> {}
unsafe impl<T: ObjectType + Send + Sync> Send for WeakRefInner<T> {}

impl<T: ObjectType> PartialEq for WeakRef<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
unsafe { self.0.priv_.p == other.0.priv_.p }
unsafe { self.0.ffi.priv_.p == other.0.ffi.priv_.p }
}
}

impl<T: ObjectType> PartialEq<T> for WeakRef<T> {
#[inline]
fn eq(&self, other: &T) -> bool {
unsafe { self.0.priv_.p == other.as_ptr() as *mut std::os::raw::c_void }
unsafe { self.0.ffi.priv_.p == other.as_ptr() as *mut std::os::raw::c_void }
}
}

impl<T: ObjectType> PartialOrd for WeakRef<T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
unsafe { self.0.priv_.p.partial_cmp(&other.0.priv_.p) }
unsafe { self.0.ffi.priv_.p.partial_cmp(&other.0.ffi.priv_.p) }
}
}

Expand Down

0 comments on commit cfd0b86

Please sign in to comment.