glib/gobject
Thomas Haller 092be080c5 gobject: avoid global GRWLock for weak locations in g_object_unref() in some cases
_object_unref_clear_weak_locations() is called twice during
g_object_unref(). In both cases, it is when we expect that the reference
count is 1 and we are either about to call dispose() or finalize().

At this point, we must check for GWeakRef to avoid a race that the ref
count gets increased just at that point.

However, we can do something better than to always take the global lock.

On the object, whenever an object is set to a GWeakRef, set a flag
OPTIONAL_FLAG_EVER_HAD_WEAK_REF. Most objects are not involved with weak
references and won't have this flag set.

If we reach _object_unref_clear_weak_locations() we just (atomically)
checked that the ref count is one. If the object at this point never had
a GWeakRef registered, we know that nobody else could have raced against
obtaining another reference. In this case, we can skip taking the lock
and checking for weak locations.

As most object don't ever have a GWeakRef registered, this significantly
avoids unnecessary work during _object_unref_clear_weak_locations().

This even fixes a hard to hit race in the do_unref=FALSE case.
Previously, if do_unref=FALSE there were code paths where we avoided
taking the global lock. We do so, when quark_weak_locations is unset.
However, that is not race free. If we enter
_object_unref_clear_weak_locations() with a ref-count of 1 and one
GWeakRef registered, another thread can take a strong reference and
unset the GWeakRef. Then quark_weak_locations will be unset, and
_object_unref_clear_weak_locations() misses the fact that the ref count
is now bumped to two. That is now fixed, because once
OPTIONAL_FLAG_EVER_HAD_WEAK_REF is set, it will stick.

Previously, there was an optimization to first take a read lock to check
whether there are weak locations to clear. It's not clear that this is
worth it, because we now already have a hint that there might be a weak
location. Unfortunately, GRWLock does not support an upgradable lock, so
we cannot take an (upgradable) read lock, and when necessary upgrade
that to a write lock.
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: avoid global GRWLock for weak locations in g_object_unref() in some cases 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