2 * Copyright © 2011 Ryan Lortie
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
25 * SECTION:atomic_operations
26 * @title: Atomic Operations
27 * @short_description: basic atomic integer and pointer operations
30 * The following is a collection of compiler macros to provide atomic
31 * access to integer and pointer-sized values.
33 * The macros that have 'int' in the name will operate on pointers to
34 * #gint and #guint. The macros with 'pointer' in the name will operate
35 * on pointers to any pointer-sized value, including #gsize. There is
36 * no support for 64bit operations on platforms with 32bit pointers
37 * because it is not generally possible to perform these operations
40 * The get, set and exchange operations for integers and pointers
41 * nominally operate on #gint and #gpointer, respectively. Of the
42 * arithmetic operations, the 'add' operation operates on (and returns)
43 * signed integer values (#gint and #gssize) and the 'and', 'or', and
44 * 'xor' operations operate on (and return) unsigned integer values
45 * (#guint and #gsize).
47 * All of the operations act as a full compiler and (where appropriate)
48 * hardware memory barrier. Acquire and release or producer and
49 * consumer barrier semantics are not available through this API.
51 * It is very important that all accesses to a particular integer or
52 * pointer be performed using only this API and that different sizes of
53 * operation are not mixed or used on overlapping memory regions. Never
54 * read or assign directly from or to a value -- always use this API.
56 * For simple reference counting purposes you should use
57 * g_atomic_int_inc() and g_atomic_int_dec_and_test(). Other uses that
58 * fall outside of simple reference counting patterns are prone to
59 * subtle bugs and occasionally undefined behaviour. It is also worth
60 * noting that since all of these operations require global
61 * synchronisation of the entire machine, they can be quite slow. In
62 * the case of performing multiple atomic operations it can often be
63 * faster to simply acquire a mutex lock around the critical area,
64 * perform the operations normally and then release the lock.
70 * This macro is defined if the atomic operations of GLib are
71 * implemented using real hardware atomic operations. This means that
72 * the GLib atomic API can be used between processes and safely mixed
73 * with other (hardware) atomic APIs.
75 * If this macro is not defined, the atomic operations may be
76 * emulated using a mutex. In that case, the GLib atomic operations are
77 * only atomic relative to themselves and within a single process.
82 * This file is the lowest-level part of GLib.
84 * Other lowlevel parts of GLib (threads, slice allocator, g_malloc,
85 * messages, etc) call into these functions and macros to get work done.
87 * As such, these functions can not call back into any part of GLib
88 * without risking recursion.
91 #ifdef G_ATOMIC_LOCK_FREE
93 /* if G_ATOMIC_LOCK_FREE was defined by `meson configure` then we MUST
94 * implement the atomic operations in a lock-free manner.
97 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
101 * @atomic: a pointer to a #gint or #guint
103 * Gets the current value of @atomic.
105 * This call acts as a full compiler and hardware
106 * memory barrier (before the get).
108 * While @atomic has a `volatile` qualifier, this is a historical artifact and
109 * the pointer passed to it should not be `volatile`.
111 * Returns: the value of the integer
116 (g_atomic_int_get) (const volatile gint *atomic)
118 return g_atomic_int_get (atomic);
123 * @atomic: a pointer to a #gint or #guint
124 * @newval: a new value to store
126 * Sets the value of @atomic to @newval.
128 * This call acts as a full compiler and hardware
129 * memory barrier (after the set).
131 * While @atomic has a `volatile` qualifier, this is a historical artifact and
132 * the pointer passed to it should not be `volatile`.
137 (g_atomic_int_set) (volatile gint *atomic,
140 g_atomic_int_set (atomic, newval);
145 * @atomic: a pointer to a #gint or #guint
147 * Increments the value of @atomic by 1.
149 * Think of this operation as an atomic version of `{ *atomic += 1; }`.
151 * This call acts as a full compiler and hardware memory barrier.
153 * While @atomic has a `volatile` qualifier, this is a historical artifact and
154 * the pointer passed to it should not be `volatile`.
159 (g_atomic_int_inc) (volatile gint *atomic)
161 g_atomic_int_inc (atomic);
165 * g_atomic_int_dec_and_test:
166 * @atomic: a pointer to a #gint or #guint
168 * Decrements the value of @atomic by 1.
170 * Think of this operation as an atomic version of
171 * `{ *atomic -= 1; return (*atomic == 0); }`.
173 * This call acts as a full compiler and hardware memory barrier.
175 * While @atomic has a `volatile` qualifier, this is a historical artifact and
176 * the pointer passed to it should not be `volatile`.
178 * Returns: %TRUE if the resultant value is zero
183 (g_atomic_int_dec_and_test) (volatile gint *atomic)
185 return g_atomic_int_dec_and_test (atomic);
189 * g_atomic_int_compare_and_exchange:
190 * @atomic: a pointer to a #gint or #guint
191 * @oldval: the value to compare with
192 * @newval: the value to conditionally replace with
194 * Compares @atomic to @oldval and, if equal, sets it to @newval.
195 * If @atomic was not equal to @oldval then no change occurs.
197 * This compare and exchange is done atomically.
199 * Think of this operation as an atomic version of
200 * `{ if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }`.
202 * This call acts as a full compiler and hardware memory barrier.
204 * While @atomic has a `volatile` qualifier, this is a historical artifact and
205 * the pointer passed to it should not be `volatile`.
207 * Returns: %TRUE if the exchange took place
212 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
216 return g_atomic_int_compare_and_exchange (atomic, oldval, newval);
221 * @atomic: a pointer to a #gint or #guint
222 * @val: the value to add
224 * Atomically adds @val to the value of @atomic.
226 * Think of this operation as an atomic version of
227 * `{ tmp = *atomic; *atomic += val; return tmp; }`.
229 * This call acts as a full compiler and hardware memory barrier.
231 * Before version 2.30, this function did not return a value
232 * (but g_atomic_int_exchange_and_add() did, and had the same meaning).
234 * While @atomic has a `volatile` qualifier, this is a historical artifact and
235 * the pointer passed to it should not be `volatile`.
237 * Returns: the value of @atomic before the add, signed
242 (g_atomic_int_add) (volatile gint *atomic,
245 return g_atomic_int_add (atomic, val);
250 * @atomic: a pointer to a #gint or #guint
251 * @val: the value to 'and'
253 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
254 * storing the result back in @atomic.
256 * This call acts as a full compiler and hardware memory barrier.
258 * Think of this operation as an atomic version of
259 * `{ tmp = *atomic; *atomic &= val; return tmp; }`.
261 * While @atomic has a `volatile` qualifier, this is a historical artifact and
262 * the pointer passed to it should not be `volatile`.
264 * Returns: the value of @atomic before the operation, unsigned
269 (g_atomic_int_and) (volatile guint *atomic,
272 return g_atomic_int_and (atomic, val);
277 * @atomic: a pointer to a #gint or #guint
278 * @val: the value to 'or'
280 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
281 * storing the result back in @atomic.
283 * Think of this operation as an atomic version of
284 * `{ tmp = *atomic; *atomic |= val; return tmp; }`.
286 * This call acts as a full compiler and hardware memory barrier.
288 * While @atomic has a `volatile` qualifier, this is a historical artifact and
289 * the pointer passed to it should not be `volatile`.
291 * Returns: the value of @atomic before the operation, unsigned
296 (g_atomic_int_or) (volatile guint *atomic,
299 return g_atomic_int_or (atomic, val);
304 * @atomic: a pointer to a #gint or #guint
305 * @val: the value to 'xor'
307 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
308 * storing the result back in @atomic.
310 * Think of this operation as an atomic version of
311 * `{ tmp = *atomic; *atomic ^= val; return tmp; }`.
313 * This call acts as a full compiler and hardware memory barrier.
315 * While @atomic has a `volatile` qualifier, this is a historical artifact and
316 * the pointer passed to it should not be `volatile`.
318 * Returns: the value of @atomic before the operation, unsigned
323 (g_atomic_int_xor) (volatile guint *atomic,
326 return g_atomic_int_xor (atomic, val);
331 * g_atomic_pointer_get:
332 * @atomic: (not nullable): a pointer to a #gpointer-sized value
334 * Gets the current value of @atomic.
336 * This call acts as a full compiler and hardware
337 * memory barrier (before the get).
339 * While @atomic has a `volatile` qualifier, this is a historical artifact and
340 * the pointer passed to it should not be `volatile`.
342 * Returns: the value of the pointer
347 (g_atomic_pointer_get) (const volatile void *atomic)
349 return g_atomic_pointer_get ((gpointer *) atomic);
353 * g_atomic_pointer_set:
354 * @atomic: (not nullable): a pointer to a #gpointer-sized value
355 * @newval: a new value to store
357 * Sets the value of @atomic to @newval.
359 * This call acts as a full compiler and hardware
360 * memory barrier (after the set).
362 * While @atomic has a `volatile` qualifier, this is a historical artifact and
363 * the pointer passed to it should not be `volatile`.
368 (g_atomic_pointer_set) (volatile void *atomic,
371 g_atomic_pointer_set ((gpointer *) atomic, newval);
375 * g_atomic_pointer_compare_and_exchange:
376 * @atomic: (not nullable): a pointer to a #gpointer-sized value
377 * @oldval: the value to compare with
378 * @newval: the value to conditionally replace with
380 * Compares @atomic to @oldval and, if equal, sets it to @newval.
381 * If @atomic was not equal to @oldval then no change occurs.
383 * This compare and exchange is done atomically.
385 * Think of this operation as an atomic version of
386 * `{ if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }`.
388 * This call acts as a full compiler and hardware memory barrier.
390 * While @atomic has a `volatile` qualifier, this is a historical artifact and
391 * the pointer passed to it should not be `volatile`.
393 * Returns: %TRUE if the exchange took place
398 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
402 return g_atomic_pointer_compare_and_exchange ((gpointer *) atomic,
407 * g_atomic_pointer_add:
408 * @atomic: (not nullable): a pointer to a #gpointer-sized value
409 * @val: the value to add
411 * Atomically adds @val to the value of @atomic.
413 * Think of this operation as an atomic version of
414 * `{ tmp = *atomic; *atomic += val; return tmp; }`.
416 * This call acts as a full compiler and hardware memory barrier.
418 * While @atomic has a `volatile` qualifier, this is a historical artifact and
419 * the pointer passed to it should not be `volatile`.
421 * Returns: the value of @atomic before the add, signed
426 (g_atomic_pointer_add) (volatile void *atomic,
429 return g_atomic_pointer_add ((gpointer *) atomic, val);
433 * g_atomic_pointer_and:
434 * @atomic: (not nullable): a pointer to a #gpointer-sized value
435 * @val: the value to 'and'
437 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
438 * storing the result back in @atomic.
440 * Think of this operation as an atomic version of
441 * `{ tmp = *atomic; *atomic &= val; return tmp; }`.
443 * This call acts as a full compiler and hardware memory barrier.
445 * While @atomic has a `volatile` qualifier, this is a historical artifact and
446 * the pointer passed to it should not be `volatile`.
448 * Returns: the value of @atomic before the operation, unsigned
453 (g_atomic_pointer_and) (volatile void *atomic,
456 return g_atomic_pointer_and ((gpointer *) atomic, val);
460 * g_atomic_pointer_or:
461 * @atomic: (not nullable): a pointer to a #gpointer-sized value
462 * @val: the value to 'or'
464 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
465 * storing the result back in @atomic.
467 * Think of this operation as an atomic version of
468 * `{ tmp = *atomic; *atomic |= val; return tmp; }`.
470 * This call acts as a full compiler and hardware memory barrier.
472 * While @atomic has a `volatile` qualifier, this is a historical artifact and
473 * the pointer passed to it should not be `volatile`.
475 * Returns: the value of @atomic before the operation, unsigned
480 (g_atomic_pointer_or) (volatile void *atomic,
483 return g_atomic_pointer_or ((gpointer *) atomic, val);
487 * g_atomic_pointer_xor:
488 * @atomic: (not nullable): a pointer to a #gpointer-sized value
489 * @val: the value to 'xor'
491 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
492 * storing the result back in @atomic.
494 * Think of this operation as an atomic version of
495 * `{ tmp = *atomic; *atomic ^= val; return tmp; }`.
497 * This call acts as a full compiler and hardware memory barrier.
499 * While @atomic has a `volatile` qualifier, this is a historical artifact and
500 * the pointer passed to it should not be `volatile`.
502 * Returns: the value of @atomic before the operation, unsigned
507 (g_atomic_pointer_xor) (volatile void *atomic,
510 return g_atomic_pointer_xor ((gpointer *) atomic, val);
513 #elif defined (G_PLATFORM_WIN32)
516 #if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64) && !(defined _MSC_VER && _MSC_VER <= 1200)
517 #define InterlockedAnd _InterlockedAnd
518 #define InterlockedOr _InterlockedOr
519 #define InterlockedXor _InterlockedXor
522 #if !defined (_MSC_VER) || _MSC_VER <= 1200
523 #include "gmessages.h"
524 /* Inlined versions for older compiler */
526 _gInterlockedAnd (volatile guint *atomic,
534 j = InterlockedCompareExchange(atomic, i & val, i);
539 #define InterlockedAnd(a,b) _gInterlockedAnd(a,b)
541 _gInterlockedOr (volatile guint *atomic,
549 j = InterlockedCompareExchange(atomic, i | val, i);
554 #define InterlockedOr(a,b) _gInterlockedOr(a,b)
556 _gInterlockedXor (volatile guint *atomic,
564 j = InterlockedCompareExchange(atomic, i ^ val, i);
569 #define InterlockedXor(a,b) _gInterlockedXor(a,b)
573 * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
576 (g_atomic_int_get) (const volatile gint *atomic)
583 (g_atomic_int_set) (volatile gint *atomic,
591 (g_atomic_int_inc) (volatile gint *atomic)
593 InterlockedIncrement (atomic);
597 (g_atomic_int_dec_and_test) (volatile gint *atomic)
599 return InterlockedDecrement (atomic) == 0;
603 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
607 return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
611 (g_atomic_int_add) (volatile gint *atomic,
614 return InterlockedExchangeAdd (atomic, val);
618 (g_atomic_int_and) (volatile guint *atomic,
621 return InterlockedAnd (atomic, val);
625 (g_atomic_int_or) (volatile guint *atomic,
628 return InterlockedOr (atomic, val);
632 (g_atomic_int_xor) (volatile guint *atomic,
635 return InterlockedXor (atomic, val);
640 (g_atomic_pointer_get) (const volatile void *atomic)
642 const gpointer *ptr = atomic;
649 (g_atomic_pointer_set) (volatile void *atomic,
652 gpointer *ptr = atomic;
659 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
663 return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
667 (g_atomic_pointer_add) (volatile void *atomic,
670 #if GLIB_SIZEOF_VOID_P == 8
671 return InterlockedExchangeAdd64 (atomic, val);
673 return InterlockedExchangeAdd (atomic, val);
678 (g_atomic_pointer_and) (volatile void *atomic,
681 #if GLIB_SIZEOF_VOID_P == 8
682 return InterlockedAnd64 (atomic, val);
684 return InterlockedAnd (atomic, val);
689 (g_atomic_pointer_or) (volatile void *atomic,
692 #if GLIB_SIZEOF_VOID_P == 8
693 return InterlockedOr64 (atomic, val);
695 return InterlockedOr (atomic, val);
700 (g_atomic_pointer_xor) (volatile void *atomic,
703 #if GLIB_SIZEOF_VOID_P == 8
704 return InterlockedXor64 (atomic, val);
706 return InterlockedXor (atomic, val);
711 /* This error occurs when `meson configure` decided that we should be capable
712 * of lock-free atomics but we find at compile-time that we are not.
714 #error G_ATOMIC_LOCK_FREE defined, but incapable of lock-free atomics.
716 #endif /* defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
718 #else /* G_ATOMIC_LOCK_FREE */
720 /* We are not permitted to call into any GLib functions from here, so we
721 * can not use GMutex.
723 * Fortunately, we already take care of the Windows case above, and all
724 * non-Windows platforms on which glib runs have pthreads. Use those.
728 static pthread_mutex_t g_atomic_lock = PTHREAD_MUTEX_INITIALIZER;
731 (g_atomic_int_get) (const volatile gint *atomic)
735 pthread_mutex_lock (&g_atomic_lock);
737 pthread_mutex_unlock (&g_atomic_lock);
743 (g_atomic_int_set) (volatile gint *atomic,
746 pthread_mutex_lock (&g_atomic_lock);
748 pthread_mutex_unlock (&g_atomic_lock);
752 (g_atomic_int_inc) (volatile gint *atomic)
754 pthread_mutex_lock (&g_atomic_lock);
756 pthread_mutex_unlock (&g_atomic_lock);
760 (g_atomic_int_dec_and_test) (volatile gint *atomic)
764 pthread_mutex_lock (&g_atomic_lock);
765 is_zero = --(*atomic) == 0;
766 pthread_mutex_unlock (&g_atomic_lock);
772 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
778 pthread_mutex_lock (&g_atomic_lock);
780 if ((success = (*atomic == oldval)))
783 pthread_mutex_unlock (&g_atomic_lock);
789 (g_atomic_int_add) (volatile gint *atomic,
794 pthread_mutex_lock (&g_atomic_lock);
796 *atomic = oldval + val;
797 pthread_mutex_unlock (&g_atomic_lock);
803 (g_atomic_int_and) (volatile guint *atomic,
808 pthread_mutex_lock (&g_atomic_lock);
810 *atomic = oldval & val;
811 pthread_mutex_unlock (&g_atomic_lock);
817 (g_atomic_int_or) (volatile guint *atomic,
822 pthread_mutex_lock (&g_atomic_lock);
824 *atomic = oldval | val;
825 pthread_mutex_unlock (&g_atomic_lock);
831 (g_atomic_int_xor) (volatile guint *atomic,
836 pthread_mutex_lock (&g_atomic_lock);
838 *atomic = oldval ^ val;
839 pthread_mutex_unlock (&g_atomic_lock);
846 (g_atomic_pointer_get) (const volatile void *atomic)
848 const gpointer *ptr = atomic;
851 pthread_mutex_lock (&g_atomic_lock);
853 pthread_mutex_unlock (&g_atomic_lock);
859 (g_atomic_pointer_set) (volatile void *atomic,
862 gpointer *ptr = atomic;
864 pthread_mutex_lock (&g_atomic_lock);
866 pthread_mutex_unlock (&g_atomic_lock);
870 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
874 gpointer *ptr = atomic;
877 pthread_mutex_lock (&g_atomic_lock);
879 if ((success = (*ptr == oldval)))
882 pthread_mutex_unlock (&g_atomic_lock);
888 (g_atomic_pointer_add) (volatile void *atomic,
891 gssize *ptr = atomic;
894 pthread_mutex_lock (&g_atomic_lock);
897 pthread_mutex_unlock (&g_atomic_lock);
903 (g_atomic_pointer_and) (volatile void *atomic,
909 pthread_mutex_lock (&g_atomic_lock);
912 pthread_mutex_unlock (&g_atomic_lock);
918 (g_atomic_pointer_or) (volatile void *atomic,
924 pthread_mutex_lock (&g_atomic_lock);
927 pthread_mutex_unlock (&g_atomic_lock);
933 (g_atomic_pointer_xor) (volatile void *atomic,
939 pthread_mutex_lock (&g_atomic_lock);
942 pthread_mutex_unlock (&g_atomic_lock);
950 * g_atomic_int_exchange_and_add:
951 * @atomic: a pointer to a #gint
952 * @val: the value to add
954 * This function existed before g_atomic_int_add() returned the prior
955 * value of the integer (which it now does). It is retained only for
956 * compatibility reasons. Don't use this function in new code.
958 * Returns: the value of @atomic before the add, signed
960 * Deprecated: 2.30: Use g_atomic_int_add() instead.
963 g_atomic_int_exchange_and_add (volatile gint *atomic,
966 return (g_atomic_int_add) ((gint *) atomic, val);