gobject: Use atomic compare and exchange full to force floating

In case first exchange failed we can avoid repeating the pointer get
operation given that exchange full can provide us the old value.
This commit is contained in:
Marco Trevisan (Treviño) 2023-05-09 14:18:50 +02:00
parent e54f27322d
commit f792b2ce64

View File

@ -3436,16 +3436,20 @@ object_floating_flag_handler (GObject *object,
{
gpointer oldvalue;
case +1: /* force floating if possible */
do
oldvalue = g_atomic_pointer_get (&object->qdata);
while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
(gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
oldvalue = g_atomic_pointer_get (&object->qdata);
while (!g_atomic_pointer_compare_and_exchange_full (
(void**) &object->qdata, oldvalue,
(void *) ((gsize) oldvalue | OBJECT_FLOATING_FLAG),
&oldvalue))
;
return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
case -1: /* sink if possible */
do
oldvalue = g_atomic_pointer_get (&object->qdata);
while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
(gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
oldvalue = g_atomic_pointer_get (&object->qdata);
while (!g_atomic_pointer_compare_and_exchange_full (
(void**) &object->qdata, oldvalue,
(void *) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG),
&oldvalue))
;
return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
default: /* check floating */
return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);