gatomic: Add APIs to perform atomic int / pointer exchanges

Atomic APIs provide a way to exchange values only if we compare a value
that is equal to the old value, but not to just exchange the value
returning the old one.

However, compilers provide such built-in functions, so we can use them
to expose such functionality to GLib.

The only drawback is that when using an old version of gcc not providing
atomic APIs to swap values, we need to re-implement it with an
implementation that may not be fully atomic, but that is safe enough.

However this codepath should really not be used currently as gcc
introduced __atomic_exchange_n() at version 4.7.4, so 8 years ago.
This commit is contained in:
Marco Trevisan (Treviño)
2022-06-06 21:30:41 +02:00
parent 2b437402e8
commit fa4e34667c
6 changed files with 235 additions and 0 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_exchange
g_atomic_int_add
g_atomic_int_and
g_atomic_int_or
@@ -1282,6 +1283,7 @@ g_atomic_int_xor
g_atomic_pointer_get
g_atomic_pointer_set
g_atomic_pointer_compare_and_exchange
g_atomic_pointer_exchange
g_atomic_pointer_add
g_atomic_pointer_and
g_atomic_pointer_or