2 * Copyright © 2011 Ryan Lortie
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Ryan Lortie <desrt@desrt.ca>
27 * SECTION:atomic_operations
28 * @title: Atomic Operations
29 * @short_description: basic atomic integer and pointer operations
32 * The following is a collection of compiler macros to provide atomic
33 * access to integer and pointer-sized values.
35 * The macros that have 'int' in the name will operate on pointers to
36 * #gint and #guint. The macros with 'pointer' in the name will operate
37 * on pointers to any pointer-sized value, including #gsize. There is
38 * no support for 64bit operations on platforms with 32bit pointers
39 * because it is not generally possible to perform these operations
42 * The get, set and exchange operations for integers and pointers
43 * nominally operate on #gint and #gpointer, respectively. Of the
44 * arithmetic operations, the 'add' operation operates on (and returns)
45 * signed integer values (#gint and #gssize) and the 'and', 'or', and
46 * 'xor' operations operate on (and return) unsigned integer values
47 * (#guint and #gsize).
49 * All of the operations act as a full compiler and (where appropriate)
50 * hardware memory barrier. Acquire and release or producer and
51 * consumer barrier semantics are not available through this API.
53 * It is very important that all accesses to a particular integer or
54 * pointer be performed using only this API and that different sizes of
55 * operation are not mixed or used on overlapping memory regions. Never
56 * read or assign directly from or to a value -- always use this API.
58 * For simple reference counting purposes you should use
59 * g_atomic_int_inc() and g_atomic_int_dec_and_test(). Other uses that
60 * fall outside of simple reference counting patterns are prone to
61 * subtle bugs and occasionally undefined behaviour. It is also worth
62 * noting that since all of these operations require global
63 * synchronisation of the entire machine, they can be quite slow. In
64 * the case of performing multiple atomic operations it can often be
65 * faster to simply acquire a mutex lock around the critical area,
66 * perform the operations normally and then release the lock.
72 * This macro is defined if the atomic operations of GLib are
73 * implemented using real hardware atomic operations. This means that
74 * the GLib atomic API can be used between processes and safely mixed
75 * with other (hardware) atomic APIs.
77 * If this macro is not defined, the atomic operations may be
78 * emulated using a mutex. In that case, the GLib atomic operations are
79 * only atomic relative to themselves and within a single process.
84 * This file is the lowest-level part of GLib.
86 * Other lowlevel parts of GLib (threads, slice allocator, g_malloc,
87 * messages, etc) call into these functions and macros to get work done.
89 * As such, these functions can not call back into any part of GLib
90 * without risking recursion.
93 #ifdef G_ATOMIC_LOCK_FREE
95 /* if G_ATOMIC_LOCK_FREE was defined by `meson configure` then we MUST
96 * implement the atomic operations in a lock-free manner.
99 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
103 * @atomic: a pointer to a #gint or #guint
105 * Gets the current value of @atomic.
107 * This call acts as a full compiler and hardware
108 * memory barrier (before the get).
110 * While @atomic has a `volatile` qualifier, this is a historical artifact and
111 * the pointer passed to it should not be `volatile`.
113 * Returns: the value of the integer
118 (g_atomic_int_get) (const volatile gint *atomic)
120 return g_atomic_int_get (atomic);
125 * @atomic: a pointer to a #gint or #guint
126 * @newval: a new value to store
128 * Sets the value of @atomic to @newval.
130 * This call acts as a full compiler and hardware
131 * memory barrier (after the set).
133 * While @atomic has a `volatile` qualifier, this is a historical artifact and
134 * the pointer passed to it should not be `volatile`.
139 (g_atomic_int_set) (volatile gint *atomic,
142 g_atomic_int_set (atomic, newval);
147 * @atomic: a pointer to a #gint or #guint
149 * Increments the value of @atomic by 1.
151 * Think of this operation as an atomic version of `{ *atomic += 1; }`.
153 * This call acts as a full compiler and hardware memory barrier.
155 * While @atomic has a `volatile` qualifier, this is a historical artifact and
156 * the pointer passed to it should not be `volatile`.
161 (g_atomic_int_inc) (volatile gint *atomic)
163 g_atomic_int_inc (atomic);
167 * g_atomic_int_dec_and_test:
168 * @atomic: a pointer to a #gint or #guint
170 * Decrements the value of @atomic by 1.
172 * Think of this operation as an atomic version of
173 * `{ *atomic -= 1; return (*atomic == 0); }`.
175 * This call acts as a full compiler and hardware memory barrier.
177 * While @atomic has a `volatile` qualifier, this is a historical artifact and
178 * the pointer passed to it should not be `volatile`.
180 * Returns: %TRUE if the resultant value is zero
185 (g_atomic_int_dec_and_test) (volatile gint *atomic)
187 return g_atomic_int_dec_and_test (atomic);
191 * g_atomic_int_compare_and_exchange:
192 * @atomic: a pointer to a #gint or #guint
193 * @oldval: the value to compare with
194 * @newval: the value to conditionally replace with
196 * Compares @atomic to @oldval and, if equal, sets it to @newval.
197 * If @atomic was not equal to @oldval then no change occurs.
199 * This compare and exchange is done atomically.
201 * Think of this operation as an atomic version of
202 * `{ if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }`.
204 * This call acts as a full compiler and hardware memory barrier.
206 * While @atomic has a `volatile` qualifier, this is a historical artifact and
207 * the pointer passed to it should not be `volatile`.
209 * Returns: %TRUE if the exchange took place
214 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
218 return g_atomic_int_compare_and_exchange (atomic, oldval, newval);
222 * g_atomic_int_compare_and_exchange_full:
223 * @atomic: a pointer to a #gint or #guint
224 * @oldval: the value to compare with
225 * @newval: the value to conditionally replace with
226 * @preval: (out): the contents of @atomic before this operation
228 * Compares @atomic to @oldval and, if equal, sets it to @newval.
229 * If @atomic was not equal to @oldval then no change occurs.
230 * In any case the value of @atomic before this operation is stored in @preval.
232 * This compare and exchange is done atomically.
234 * Think of this operation as an atomic version of
235 * `{ *preval = *atomic; if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }`.
237 * This call acts as a full compiler and hardware memory barrier.
239 * See also g_atomic_int_compare_and_exchange()
241 * Returns: %TRUE if the exchange took place
246 (g_atomic_int_compare_and_exchange_full) (gint *atomic,
251 return g_atomic_int_compare_and_exchange_full (atomic, oldval, newval, preval);
255 * g_atomic_int_exchange:
256 * @atomic: a pointer to a #gint or #guint
257 * @newval: the value to replace with
259 * Sets the @atomic to @newval and returns the old value from @atomic.
261 * This exchange is done atomically.
263 * Think of this operation as an atomic version of
264 * `{ tmp = *atomic; *atomic = val; return tmp; }`.
266 * This call acts as a full compiler and hardware memory barrier.
268 * Returns: the value of @atomic before the exchange, signed
273 (g_atomic_int_exchange) (gint *atomic,
276 return g_atomic_int_exchange (atomic, newval);
281 * @atomic: a pointer to a #gint or #guint
282 * @val: the value to add
284 * Atomically adds @val to the value of @atomic.
286 * Think of this operation as an atomic version of
287 * `{ tmp = *atomic; *atomic += val; return tmp; }`.
289 * This call acts as a full compiler and hardware memory barrier.
291 * Before version 2.30, this function did not return a value
292 * (but g_atomic_int_exchange_and_add() did, and had the same meaning).
294 * While @atomic has a `volatile` qualifier, this is a historical artifact and
295 * the pointer passed to it should not be `volatile`.
297 * Returns: the value of @atomic before the add, signed
302 (g_atomic_int_add) (volatile gint *atomic,
305 return g_atomic_int_add (atomic, val);
310 * @atomic: a pointer to a #gint or #guint
311 * @val: the value to 'and'
313 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
314 * storing the result back in @atomic.
316 * This call acts as a full compiler and hardware memory barrier.
318 * Think of this operation as an atomic version of
319 * `{ tmp = *atomic; *atomic &= val; return tmp; }`.
321 * While @atomic has a `volatile` qualifier, this is a historical artifact and
322 * the pointer passed to it should not be `volatile`.
324 * Returns: the value of @atomic before the operation, unsigned
329 (g_atomic_int_and) (volatile guint *atomic,
332 return g_atomic_int_and (atomic, val);
337 * @atomic: a pointer to a #gint or #guint
338 * @val: the value to 'or'
340 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
341 * storing the result back in @atomic.
343 * Think of this operation as an atomic version of
344 * `{ tmp = *atomic; *atomic |= val; return tmp; }`.
346 * This call acts as a full compiler and hardware memory barrier.
348 * While @atomic has a `volatile` qualifier, this is a historical artifact and
349 * the pointer passed to it should not be `volatile`.
351 * Returns: the value of @atomic before the operation, unsigned
356 (g_atomic_int_or) (volatile guint *atomic,
359 return g_atomic_int_or (atomic, val);
364 * @atomic: a pointer to a #gint or #guint
365 * @val: the value to 'xor'
367 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
368 * storing the result back in @atomic.
370 * Think of this operation as an atomic version of
371 * `{ tmp = *atomic; *atomic ^= val; return tmp; }`.
373 * This call acts as a full compiler and hardware memory barrier.
375 * While @atomic has a `volatile` qualifier, this is a historical artifact and
376 * the pointer passed to it should not be `volatile`.
378 * Returns: the value of @atomic before the operation, unsigned
383 (g_atomic_int_xor) (volatile guint *atomic,
386 return g_atomic_int_xor (atomic, val);
391 * g_atomic_pointer_get:
392 * @atomic: (not nullable): a pointer to a #gpointer-sized value
394 * Gets the current value of @atomic.
396 * This call acts as a full compiler and hardware
397 * memory barrier (before the get).
399 * While @atomic has a `volatile` qualifier, this is a historical artifact and
400 * the pointer passed to it should not be `volatile`.
402 * Returns: the value of the pointer
407 (g_atomic_pointer_get) (const volatile void *atomic)
409 return g_atomic_pointer_get ((gpointer *) atomic);
413 * g_atomic_pointer_set:
414 * @atomic: (not nullable): a pointer to a #gpointer-sized value
415 * @newval: a new value to store
417 * Sets the value of @atomic to @newval.
419 * This call acts as a full compiler and hardware
420 * memory barrier (after the set).
422 * While @atomic has a `volatile` qualifier, this is a historical artifact and
423 * the pointer passed to it should not be `volatile`.
428 (g_atomic_pointer_set) (volatile void *atomic,
431 g_atomic_pointer_set ((gpointer *) atomic, newval);
435 * g_atomic_pointer_compare_and_exchange:
436 * @atomic: (not nullable): a pointer to a #gpointer-sized value
437 * @oldval: the value to compare with
438 * @newval: the value to conditionally replace with
440 * Compares @atomic to @oldval and, if equal, sets it to @newval.
441 * If @atomic was not equal to @oldval then no change occurs.
443 * This compare and exchange is done atomically.
445 * Think of this operation as an atomic version of
446 * `{ if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }`.
448 * This call acts as a full compiler and hardware memory barrier.
450 * While @atomic has a `volatile` qualifier, this is a historical artifact and
451 * the pointer passed to it should not be `volatile`.
453 * Returns: %TRUE if the exchange took place
458 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
462 return g_atomic_pointer_compare_and_exchange ((gpointer *) atomic,
467 * g_atomic_pointer_compare_and_exchange_full:
468 * @atomic: (not nullable): a pointer to a #gpointer-sized value
469 * @oldval: the value to compare with
470 * @newval: the value to conditionally replace with
471 * @preval: (not nullable) (out): the contents of @atomic before this operation
473 * Compares @atomic to @oldval and, if equal, sets it to @newval.
474 * If @atomic was not equal to @oldval then no change occurs.
475 * In any case the value of @atomic before this operation is stored in @preval.
477 * This compare and exchange is done atomically.
479 * Think of this operation as an atomic version of
480 * `{ *preval = *atomic; if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }`.
482 * This call acts as a full compiler and hardware memory barrier.
484 * See also g_atomic_pointer_compare_and_exchange()
486 * Returns: %TRUE if the exchange took place
491 (g_atomic_pointer_compare_and_exchange_full) (void *atomic,
496 return g_atomic_pointer_compare_and_exchange_full ((gpointer *) atomic,
498 (gpointer *) preval);
502 * g_atomic_pointer_exchange:
503 * @atomic: a pointer to a #gpointer-sized value
504 * @newval: the value to replace with
506 * Sets the @atomic to @newval and returns the old value from @atomic.
508 * This exchange is done atomically.
510 * Think of this operation as an atomic version of
511 * `{ tmp = *atomic; *atomic = val; return tmp; }`.
513 * This call acts as a full compiler and hardware memory barrier.
515 * Returns: the value of @atomic before the exchange
520 (g_atomic_pointer_exchange) (void *atomic,
523 return g_atomic_pointer_exchange ((gpointer *) atomic, newval);
527 * g_atomic_pointer_add:
528 * @atomic: (not nullable): a pointer to a #gpointer-sized value
529 * @val: the value to add
531 * Atomically adds @val to the value of @atomic.
533 * Think of this operation as an atomic version of
534 * `{ tmp = *atomic; *atomic += val; return tmp; }`.
536 * This call acts as a full compiler and hardware memory barrier.
538 * While @atomic has a `volatile` qualifier, this is a historical artifact and
539 * the pointer passed to it should not be `volatile`.
541 * Returns: the value of @atomic before the add, signed
546 (g_atomic_pointer_add) (volatile void *atomic,
549 return g_atomic_pointer_add ((gpointer *) atomic, val);
553 * g_atomic_pointer_and:
554 * @atomic: (not nullable): a pointer to a #gpointer-sized value
555 * @val: the value to 'and'
557 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
558 * storing the result back in @atomic.
560 * Think of this operation as an atomic version of
561 * `{ tmp = *atomic; *atomic &= val; return tmp; }`.
563 * This call acts as a full compiler and hardware memory barrier.
565 * While @atomic has a `volatile` qualifier, this is a historical artifact and
566 * the pointer passed to it should not be `volatile`.
568 * Returns: the value of @atomic before the operation, unsigned
573 (g_atomic_pointer_and) (volatile void *atomic,
576 return g_atomic_pointer_and ((gpointer *) atomic, val);
580 * g_atomic_pointer_or:
581 * @atomic: (not nullable): a pointer to a #gpointer-sized value
582 * @val: the value to 'or'
584 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
585 * storing the result back in @atomic.
587 * Think of this operation as an atomic version of
588 * `{ tmp = *atomic; *atomic |= val; return tmp; }`.
590 * This call acts as a full compiler and hardware memory barrier.
592 * While @atomic has a `volatile` qualifier, this is a historical artifact and
593 * the pointer passed to it should not be `volatile`.
595 * Returns: the value of @atomic before the operation, unsigned
600 (g_atomic_pointer_or) (volatile void *atomic,
603 return g_atomic_pointer_or ((gpointer *) atomic, val);
607 * g_atomic_pointer_xor:
608 * @atomic: (not nullable): a pointer to a #gpointer-sized value
609 * @val: the value to 'xor'
611 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
612 * storing the result back in @atomic.
614 * Think of this operation as an atomic version of
615 * `{ tmp = *atomic; *atomic ^= val; return tmp; }`.
617 * This call acts as a full compiler and hardware memory barrier.
619 * While @atomic has a `volatile` qualifier, this is a historical artifact and
620 * the pointer passed to it should not be `volatile`.
622 * Returns: the value of @atomic before the operation, unsigned
627 (g_atomic_pointer_xor) (volatile void *atomic,
630 return g_atomic_pointer_xor ((gpointer *) atomic, val);
633 #elif defined (G_PLATFORM_WIN32)
636 #if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64) && !(defined _MSC_VER && _MSC_VER <= 1200)
637 #define InterlockedAnd _InterlockedAnd
638 #define InterlockedOr _InterlockedOr
639 #define InterlockedXor _InterlockedXor
642 #if !defined (_MSC_VER) || _MSC_VER <= 1200
643 #include "gmessages.h"
644 /* Inlined versions for older compiler */
646 _gInterlockedAnd (volatile guint *atomic,
654 j = InterlockedCompareExchange(atomic, i & val, i);
659 #define InterlockedAnd(a,b) _gInterlockedAnd(a,b)
661 _gInterlockedOr (volatile guint *atomic,
669 j = InterlockedCompareExchange(atomic, i | val, i);
674 #define InterlockedOr(a,b) _gInterlockedOr(a,b)
676 _gInterlockedXor (volatile guint *atomic,
684 j = InterlockedCompareExchange(atomic, i ^ val, i);
689 #define InterlockedXor(a,b) _gInterlockedXor(a,b)
693 * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
696 (g_atomic_int_get) (const volatile gint *atomic)
703 (g_atomic_int_set) (volatile gint *atomic,
711 (g_atomic_int_inc) (volatile gint *atomic)
713 InterlockedIncrement (atomic);
717 (g_atomic_int_dec_and_test) (volatile gint *atomic)
719 return InterlockedDecrement (atomic) == 0;
723 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
727 return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
731 (g_atomic_int_compare_and_exchange_full) (gint *atomic,
736 *preval = InterlockedCompareExchange (atomic, newval, oldval);
737 return *preval == oldval;
741 (g_atomic_int_exchange) (gint *atomic,
744 return InterlockedExchange (atomic, newval);
748 (g_atomic_int_add) (volatile gint *atomic,
751 return InterlockedExchangeAdd (atomic, val);
755 (g_atomic_int_and) (volatile guint *atomic,
758 return InterlockedAnd (atomic, val);
762 (g_atomic_int_or) (volatile guint *atomic,
765 return InterlockedOr (atomic, val);
769 (g_atomic_int_xor) (volatile guint *atomic,
772 return InterlockedXor (atomic, val);
777 (g_atomic_pointer_get) (const volatile void *atomic)
779 const gpointer *ptr = atomic;
786 (g_atomic_pointer_set) (volatile void *atomic,
789 gpointer *ptr = atomic;
796 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
800 return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
804 (g_atomic_pointer_compare_and_exchange_full) (void *atomic,
809 gpointer *pre = preval;
811 *pre = InterlockedCompareExchangePointer (atomic, newval, oldval);
813 return *pre == oldval;
817 (g_atomic_pointer_exchange) (void *atomic,
820 return InterlockedExchangePointer (atomic, newval);
824 (g_atomic_pointer_add) (volatile void *atomic,
827 #if GLIB_SIZEOF_VOID_P == 8
828 return InterlockedExchangeAdd64 (atomic, val);
830 return InterlockedExchangeAdd (atomic, val);
835 (g_atomic_pointer_and) (volatile void *atomic,
838 #if GLIB_SIZEOF_VOID_P == 8
839 return InterlockedAnd64 (atomic, val);
841 return InterlockedAnd (atomic, val);
846 (g_atomic_pointer_or) (volatile void *atomic,
849 #if GLIB_SIZEOF_VOID_P == 8
850 return InterlockedOr64 (atomic, val);
852 return InterlockedOr (atomic, val);
857 (g_atomic_pointer_xor) (volatile void *atomic,
860 #if GLIB_SIZEOF_VOID_P == 8
861 return InterlockedXor64 (atomic, val);
863 return InterlockedXor (atomic, val);
868 /* This error occurs when `meson configure` decided that we should be capable
869 * of lock-free atomics but we find at compile-time that we are not.
871 #error G_ATOMIC_LOCK_FREE defined, but incapable of lock-free atomics.
873 #endif /* defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
875 #else /* G_ATOMIC_LOCK_FREE */
877 /* We are not permitted to call into any GLib functions from here, so we
878 * can not use GMutex.
880 * Fortunately, we already take care of the Windows case above, and all
881 * non-Windows platforms on which glib runs have pthreads. Use those.
885 static pthread_mutex_t g_atomic_lock = PTHREAD_MUTEX_INITIALIZER;
888 (g_atomic_int_get) (const volatile gint *atomic)
892 pthread_mutex_lock (&g_atomic_lock);
894 pthread_mutex_unlock (&g_atomic_lock);
900 (g_atomic_int_set) (volatile gint *atomic,
903 pthread_mutex_lock (&g_atomic_lock);
905 pthread_mutex_unlock (&g_atomic_lock);
909 (g_atomic_int_inc) (volatile gint *atomic)
911 pthread_mutex_lock (&g_atomic_lock);
913 pthread_mutex_unlock (&g_atomic_lock);
917 (g_atomic_int_dec_and_test) (volatile gint *atomic)
921 pthread_mutex_lock (&g_atomic_lock);
922 is_zero = --(*atomic) == 0;
923 pthread_mutex_unlock (&g_atomic_lock);
929 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
935 pthread_mutex_lock (&g_atomic_lock);
937 if ((success = (*atomic == oldval)))
940 pthread_mutex_unlock (&g_atomic_lock);
946 (g_atomic_int_compare_and_exchange_full) (gint *atomic,
953 pthread_mutex_lock (&g_atomic_lock);
957 if ((success = (*atomic == oldval)))
960 pthread_mutex_unlock (&g_atomic_lock);
966 (g_atomic_int_exchange) (gint *atomic,
972 pthread_mutex_lock (&g_atomic_lock);
975 pthread_mutex_unlock (&g_atomic_lock);
981 (g_atomic_int_add) (volatile gint *atomic,
986 pthread_mutex_lock (&g_atomic_lock);
988 *atomic = oldval + val;
989 pthread_mutex_unlock (&g_atomic_lock);
995 (g_atomic_int_and) (volatile guint *atomic,
1000 pthread_mutex_lock (&g_atomic_lock);
1002 *atomic = oldval & val;
1003 pthread_mutex_unlock (&g_atomic_lock);
1009 (g_atomic_int_or) (volatile guint *atomic,
1014 pthread_mutex_lock (&g_atomic_lock);
1016 *atomic = oldval | val;
1017 pthread_mutex_unlock (&g_atomic_lock);
1023 (g_atomic_int_xor) (volatile guint *atomic,
1028 pthread_mutex_lock (&g_atomic_lock);
1030 *atomic = oldval ^ val;
1031 pthread_mutex_unlock (&g_atomic_lock);
1038 (g_atomic_pointer_get) (const volatile void *atomic)
1040 const gpointer *ptr = atomic;
1043 pthread_mutex_lock (&g_atomic_lock);
1045 pthread_mutex_unlock (&g_atomic_lock);
1051 (g_atomic_pointer_set) (volatile void *atomic,
1054 gpointer *ptr = atomic;
1056 pthread_mutex_lock (&g_atomic_lock);
1058 pthread_mutex_unlock (&g_atomic_lock);
1062 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
1066 gpointer *ptr = atomic;
1069 pthread_mutex_lock (&g_atomic_lock);
1071 if ((success = (*ptr == oldval)))
1074 pthread_mutex_unlock (&g_atomic_lock);
1080 (g_atomic_pointer_compare_and_exchange_full) (void *atomic,
1085 gpointer *ptr = atomic;
1086 gpointer *pre = preval;
1089 pthread_mutex_lock (&g_atomic_lock);
1092 if ((success = (*ptr == oldval)))
1095 pthread_mutex_unlock (&g_atomic_lock);
1101 (g_atomic_pointer_exchange) (void *atomic,
1104 gpointer *ptr = atomic;
1107 pthread_mutex_lock (&g_atomic_lock);
1110 pthread_mutex_unlock (&g_atomic_lock);
1116 (g_atomic_pointer_add) (volatile void *atomic,
1119 gssize *ptr = atomic;
1122 pthread_mutex_lock (&g_atomic_lock);
1124 *ptr = oldval + val;
1125 pthread_mutex_unlock (&g_atomic_lock);
1131 (g_atomic_pointer_and) (volatile void *atomic,
1134 gsize *ptr = atomic;
1137 pthread_mutex_lock (&g_atomic_lock);
1139 *ptr = oldval & val;
1140 pthread_mutex_unlock (&g_atomic_lock);
1146 (g_atomic_pointer_or) (volatile void *atomic,
1149 gsize *ptr = atomic;
1152 pthread_mutex_lock (&g_atomic_lock);
1154 *ptr = oldval | val;
1155 pthread_mutex_unlock (&g_atomic_lock);
1161 (g_atomic_pointer_xor) (volatile void *atomic,
1164 gsize *ptr = atomic;
1167 pthread_mutex_lock (&g_atomic_lock);
1169 *ptr = oldval ^ val;
1170 pthread_mutex_unlock (&g_atomic_lock);
1178 * g_atomic_int_exchange_and_add:
1179 * @atomic: a pointer to a #gint
1180 * @val: the value to add
1182 * This function existed before g_atomic_int_add() returned the prior
1183 * value of the integer (which it now does). It is retained only for
1184 * compatibility reasons. Don't use this function in new code.
1186 * Returns: the value of @atomic before the add, signed
1188 * Deprecated: 2.30: Use g_atomic_int_add() instead.
1191 g_atomic_int_exchange_and_add (volatile gint *atomic,
1194 return (g_atomic_int_add) ((gint *) atomic, val);