glib/gobject
Thomas Haller c4c76d77cb gbitlock: add g_pointer_bit_unlock_and_set() and g_pointer_bit_lock_mask_ptr()
The existing g_pointer_bit_lock() and g_pointer_bit_unlock() API
requires the user to understand/reimplement how bits of the pointer get
mangled. Add helper functions for that.

The useful thing to do with g_pointer_bit_lock() API is to get/set
pointers while having it locked. For example, to set the pointer a user
can do:

    g_pointer_bit_lock (&lockptr, lock_bit);
    ptr2 = set_bit_pointer_as_if_locked(ptr, lock_bit);
    g_atomic_pointer_set (&lockptr, ptr2);
    g_pointer_bit_unlock (&lockptr, lock_bit);

That has several problems:

- it requires one extra atomic operations (3 instead of 2, in the
  non-contended case).

- the first g_atomic_pointer_set() already wakes blocked threads,
  which find themselves still being locked and needs to go back to
  sleep.

- the user needs to re-implement how bit-locking mangles the pointer so
  that it looks as if it were locked.

- while the user tries to re-implement what glib does to mangle the
  pointer for bitlocking, there is no immediate guarantee that they get
  it right.

Now we can do instead:

  g_pointer_bit_lock(&lockptr, lock_bit);
  g_pointer_bit_unlock_and_set(&lockptr, lock_bit, ptr, 0);

This will also emit a critical if @ptr has the locked bit set.
g_pointer_bit_lock() really only works with pointers that have a certain
alignment, and the lowest bits unset. Otherwise, there is no space to
encode both the locking and all pointer values. The new assertion helps
to catch such bugs.

Also, g_pointer_bit_lock_mask_ptr() is here, so we can do:

  g_pointer_bit_lock(&lockptr, lock_bit);
  /* set a pointer separately, when g_pointer_bit_unlock_and_set() is unsuitable. */
  g_atomic_pointer_set(&lockptr, g_pointer_bit_lock_mask_ptr(ptr, lock_bit, TRUE, 0, NULL));
  ...
  g_pointer_bit_unlock(&lockptr, lock_bit);

and:

  g_pointer_bit_lock(&lockptr, lock_bit);
  /* read the real pointer after getting the lock. */
  ptr = g_pointer_bit_lock_mask_ptr(lockptr, lock_bit, FALSE, 0, NULL));
  ...
  g_pointer_bit_unlock(&lockptr, lock_bit);
2024-01-16 12:16:42 +01:00
..
tests gbitlock: add g_pointer_bit_unlock_and_set() and g_pointer_bit_lock_mask_ptr() 2024-01-16 12:16:42 +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 Rename all visibility macros 2022-10-13 20:53:56 -04: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: fix deadlock with g_weak_ref_set() 2024-01-04 16:42:59 +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: Add missing (scope forever) annotations 2023-11-29 12:04:59 +00: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