glib/gobject
Thomas Haller 7382cc4383 gobject: use per-object bit-lock instead of global RWLock for GWeakRef
Replace the global RWLock with per-object locking. Note that there are
three places where we needed to take the globlal lock. g_weak_ref_get(),
g_weak_ref_set() and in _object_unref_clear_weak_locations(), during
g_object_unref(). The calls during g_object_unref() seem the most
relevant here, where we would want to avoid a global lock. Luckily, that
global lock only had to be taken if the object ever had a GWeakRef
registered, so most objects wouldn't care. The global lock only affects
objects, that are ever set via g_weak_ref_set(). Still, try to avoid that
global lock.

Related to GWeakRef, there are various moments when we don't hold a
strong reference to the object. So the per-object lock cannot be on the
object itself, because when we want to unlock we no longer have access
to the object. And we cannot take a strong reference on the GObject
either, because that triggers toggle notifications. And worse, when one
thread holds the last strong reference of an object and decides to
destroy it, then a `g_weak_ref_set(weak_ref, NULL)` on another thread
could acquire a temporary reference, and steal the destruction of the
object from the other thread.

Instead, we already had a "quark_weak_locations" GData and an allocated
structure for tracking the GSList with GWeakRef. Extend that to be
ref-counted and have a separate lifetime from the object. This
WeakRefData now contains the per-object mutex for locking. We can
request the WeakRefData from an object, take a reference to keep it
alive, and use it to hold the lock without having the object alive.

We also need a bitlock on GWeakRef itself. So to set or get a
GWeakRef we must take the per-object lock on the WeakRefData and the
lock on the GWeakRef (in this order). During g_weak_ref_set() there may
be of course two objects (and two WeakRefData) involved, the previous
and the new object.

Note that now once an object gets a WeakRefData allocated, it can no
longer be freed. It must stick until the object gets destroyed. This
allocation happens, once an object is set via g_weak_ref_set(). In
other words, objects involved with GWeakRef will have extra data
allocated.

It may be possible to also release the WeakRefData once it's no longer
needed. However, that would be quite complicated, and require additional
atomic operations, so it's not clear to be worth it. So it's not done.
Instead, the WeakRefData sticks on the object once it's set.
2024-01-31 17:30:28 +01:00
..
tests gbitlock: add g_pointer_bit_lock_and_get() 2024-01-16 12:50:31 +01:00
gatomicarray.c GAtomicArray: Ensure metadata does not misalign the payload 2023-01-29 12:36:06 +00:00
gatomicarray.h GAtomicArray: Ensure metadata does not misalign the payload 2023-01-29 12:36:06 +00:00
gbinding.c Remove all nicks and blurbs from param specs 2023-11-29 13:41:34 +00:00
gbinding.h docs: Move the GBinding SECTION 2023-10-16 23:34:04 +01:00
gbindinggroup.c Remove all nicks and blurbs from param specs 2023-11-29 13:41:34 +00:00
gbindinggroup.h docs: Move the GBindingGroup SECTION 2023-10-16 23:34:20 +01:00
gboxed.c Add boxed GType for GRand 2023-12-31 09:09:48 +00:00
gboxed.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gclosure.c GType: introduce GTYPE_TO_POINTER/GPOINTER_TO_TYPE 2023-10-25 12:01:02 +01:00
gclosure.h docs: Move the GClosure SECTION 2023-10-16 23:35:05 +01:00
genums.c docs: Move enum type documentation to Markdown 2023-10-11 14:01:29 +01:00
genums.h genums: use g_once_init_enter_pointer for GType initializers 2024-01-15 17:32:11 -08:00
glib-enumtypes.c.template gobject: use g_once_init_enter_pointer for GType initializers 2023-10-04 14:50:54 +01:00
glib-enumtypes.h.template Revert "meson: Use gnome.mkenum_simple()" 2022-11-24 12:06:10 +00:00
glib-genmarshal.in Replace all uses of G_VA_COPY with Standard C va_copy 2022-10-26 18:07:55 +01:00
glib-mkenums.in mkenums: Allow , in a character literal 2023-11-21 11:54:42 +00:00
glib-types.h Add boxed GType for GRand 2023-12-31 09:09:48 +00:00
gmarshal.c Replace all uses of G_VA_COPY with Standard C va_copy 2022-10-26 18:07:55 +01:00
gmarshal.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gobject_gdb.py gobject_gdb.py: Do not break bt on optimized build 2023-12-12 17:30:01 +00:00
gobject_probes.d Add missing semicolons to gobject_probes.d 2012-10-15 19:48:44 -04:00
gobject_trace.h Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gobject-autocleanups.h Ignore autocleanups when generating introspection data 2023-10-23 10:25:30 +01:00
gobject-query.c Merge branch 'more-spdx' into 'main' 2022-07-05 11:06:49 +00:00
gobject.c gobject: use per-object bit-lock instead of global RWLock for GWeakRef 2024-01-31 17:30:28 +01:00
gobject.h docs: Move the GObject SECTION 2023-10-16 23:34:24 +01:00
gobject.rc.in meson: build Windows resource files 2018-01-04 22:19:30 +01:00
gobject.stp.in stp: add code comment to gobject.object_unref about object being dangling 2023-12-30 00:20:17 +01:00
gobjectnotifyqueue.c Replace most GObject warnings with criticals 2022-08-09 13:18:47 -05:00
gparam.c Add destructor for GParamSpecPool 2023-12-19 18:41:02 +00:00
gparam.h Add destructor for GParamSpecPool 2023-12-19 18:41:02 +00:00
gparamspecs.c GType: introduce GTYPE_TO_POINTER/GPOINTER_TO_TYPE 2023-10-25 12:01:02 +01:00
gparamspecs.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gsignal.c gsignal.c: drop an optimization that is undefined behaviour 2024-01-05 16:46:29 -08:00
gsignal.h gsignal: fix reference to signals documentation page 2023-12-15 21:03:42 +01:00
gsignalgroup.c Remove all nicks and blurbs from param specs 2023-11-29 13:41:34 +00:00
gsignalgroup.h docs: Move the GSignalGroup SECTION 2023-10-16 23:34:44 +01:00
gsourceclosure.c gobject: use g_once_init_enter_pointer for GType initializers 2023-10-04 14:50:54 +01:00
gsourceclosure.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gtype-private.h Merge branch 'more-spdx' into 'main' 2022-07-05 11:06:49 +00:00
gtype.c Change GType print placeholders to G_GUINTPTR_FORMAT 2023-10-25 12:01:02 +01:00
gtype.h GType: introduce GTYPE_TO_POINTER/GPOINTER_TO_TYPE 2023-10-25 12:01:02 +01:00
gtypemodule.c gtypemodule: Add assertions in finalize() 2023-12-21 18:25:36 +01:00
gtypemodule.h docs: Move the GTypeModule SECTION 2023-10-16 23:34:59 +01:00
gtypeplugin.c gobject: Fix invalid doc links 2023-10-23 11:26:53 +01:00
gtypeplugin.h docs: Move the GTypePlugin SECTION 2023-10-16 23:34:53 +01:00
gvalue.c docs: Move GValue documentation to Markdown 2023-10-11 14:01:29 +01:00
gvalue.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gvaluearray.c gobject: Fix invalid doc links 2023-10-23 11:26:53 +01:00
gvaluearray.h docs: Move the GValueArray SECTION 2023-10-16 23:34:39 +01:00
gvaluecollector.h docs: Move the value collection documentation to Markdown 2023-10-23 13:18:13 +01:00
gvaluetransform.c Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gvaluetypes.c gvalue: add g_value_steal_string() 2023-11-04 10:40:30 +00:00
gvaluetypes.h gvalue: add g_value_steal_string() 2023-11-04 10:40:30 +00:00
libgobject-gdb.py.in Rename gdb macros with _gdb suffix to avoid ns clashes 2016-05-23 10:52:10 -04:00
meson.build build: Move gir generation to an introspection folder 2023-12-20 21:35:53 +01:00