gatomic: Add Compare and Exchange functions that returns the previous value

Atomic primitives allow to do conditional compare and exchange but also
to get the value that was previously stored in the atomic variable.

Now, we provided an exchange function that allows to do an exchange if
the atomic value matches an expected value but we had no way to know
at the same time what was the value in the atomic at the moment of the
exchange try, an this can be useful in case that the operation fails,
for example if the current value is still acceptable for us, allowing
to do a kind of "OR" check:

  gint old_value;
  gint valid_value = 222;
  while (!g_atomic_pointer_compare_and_exchange_value (&atomic,
                                                       valid_value, 555,
                                                       &old_value)
    {
      if (old_value == 555 || old_value == 222)
        valid_value = old_value;
    }
This commit is contained in:
Marco Trevisan (Treviño)
2022-06-22 11:24:44 +02:00
parent 9a2bedb22c
commit bfdeb37f6e
5 changed files with 322 additions and 1 deletions

View File

@@ -1273,6 +1273,7 @@ g_atomic_int_set
g_atomic_int_inc
g_atomic_int_dec_and_test
g_atomic_int_compare_and_exchange
g_atomic_int_compare_and_exchange_full
g_atomic_int_exchange
g_atomic_int_add
g_atomic_int_and
@@ -1283,6 +1284,7 @@ g_atomic_int_xor
g_atomic_pointer_get
g_atomic_pointer_set
g_atomic_pointer_compare_and_exchange
g_atomic_pointer_compare_and_exchange_full
g_atomic_pointer_exchange
g_atomic_pointer_add
g_atomic_pointer_and