2 * Copyright © 2011 Ryan Lortie
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence, 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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 * On GCC, these macros are implemented using GCC intrinsic operations.
54 * On non-GCC compilers they will evaluate to function calls to
55 * functions implemented by GLib.
57 * If GLib itself was compiled with GCC then these functions will again
58 * be implemented by the GCC intrinsics. On Windows without GCC, the
59 * interlocked API is used to implement the functions.
61 * With non-GCC compilers on non-Windows systems, the functions are
62 * currently incapable of implementing true atomic operations --
63 * instead, they fallback to holding a global lock while performing the
64 * operation. This provides atomicity between the threads of one
65 * process, but not between separate processes. For this reason, one
66 * should exercise caution when attempting to use these options on
67 * shared memory regions.
69 * It is very important that all accesses to a particular integer or
70 * pointer be performed using only this API and that different sizes of
71 * operation are not mixed or used on overlapping memory regions. Never
72 * read or assign directly from or to a value -- always use this API.
74 * For simple reference counting purposes you should use
75 * g_atomic_int_inc() and g_atomic_int_dec_and_test(). Other uses that
76 * fall outside of simple reference counting patterns are prone to
77 * subtle bugs and occasionally undefined behaviour. It is also worth
78 * noting that since all of these operations require global
79 * synchronisation of the entire machine, they can be quite slow. In
80 * the case of performing multiple atomic operations it can often be
81 * faster to simply acquire a mutex lock around the critical area,
82 * perform the operations normally and then release the lock.
85 #ifdef G_ATOMIC_OP_USE_GCC_BUILTINS
88 #error Using GCC builtin atomic ops, but not compiling with GCC?
93 * @atomic: a pointer to a #gint or #guint
95 * Gets the current value of @atomic.
97 * This call acts as a full compiler and hardware memory barrier (before
100 * Returns: the value of the integer
105 (g_atomic_int_get) (volatile gint *atomic)
107 return g_atomic_int_get (atomic);
112 * @atomic: a pointer to a #gint or #guint
113 * @newval: a new value to store
115 * Sets the value of @atomic to @newval.
117 * This call acts as a full compiler and hardware memory barrier (after
123 (g_atomic_int_set) (volatile gint *atomic,
126 g_atomic_int_set (atomic, newval);
131 * @atomic: a pointer to a #gint or #guint
133 * Increments the value of @atomic by 1.
135 * This call acts as a full compiler and hardware memory barrier.
140 (g_atomic_int_inc) (volatile gint *atomic)
142 g_atomic_int_inc (atomic);
146 * g_atomic_int_dec_and_test:
147 * @atomic: a pointer to a #gint or #guint
149 * Decrements the value of @atomic by 1.
151 * This call acts as a full compiler and hardware memory barrier.
153 * Returns: %TRUE if the resultant value is zero
158 (g_atomic_int_dec_and_test) (volatile gint *atomic)
160 return g_atomic_int_dec_and_test (atomic);
164 * g_atomic_int_compare_and_exchange:
165 * @atomic: a pointer to a #gint or #guint
166 * @oldval: the value to compare with
167 * @newval: the value to conditionally replace with
169 * Compares @atomic to @oldval and, if equal, sets it to @newval. If
170 * @atomic was not equal to @oldval then no change occurs.
172 * This compare and exchange is done atomically.
174 * This call acts as a full compiler and hardware memory barrier.
176 * Returns: %TRUE if the exchange took place
181 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
185 return g_atomic_int_compare_and_exchange (atomic, oldval, newval);
190 * @atomic: a pointer to a #gint or #guint
191 * @val: the value to add
193 * Atomically adds @val to the value of @atomic.
195 * This call acts as a full compiler and hardware memory barrier.
197 * Returns: the value of @atomic before the add, signed
202 (g_atomic_int_add) (volatile gint *atomic,
205 return g_atomic_int_add (atomic, val);
210 * @atomic: a pointer to a #gint or #guint
211 * @val: the value to 'and'
213 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
214 * storing the result back in @atomic.
216 * This call acts as a full compiler and hardware memory barrier.
218 * Returns: the value of @atomic before the operation, unsigned
223 (g_atomic_int_and) (volatile guint *atomic,
226 return g_atomic_int_and (atomic, val);
231 * @atomic: a pointer to a #gint or #guint
232 * @val: the value to 'or'
234 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
235 * storing the result back in @atomic.
237 * This call acts as a full compiler and hardware memory barrier.
239 * Returns: the value of @atomic before the operation, unsigned
244 (g_atomic_int_or) (volatile guint *atomic,
247 return g_atomic_int_or (atomic, val);
252 * @atomic: a pointer to a #gint or #guint
253 * @val: the value to 'xor'
255 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
256 * storing the result back in @atomic.
258 * This call acts as a full compiler and hardware memory barrier.
260 * Returns: the value of @atomic before the operation, unsigned
265 (g_atomic_int_xor) (volatile guint *atomic,
268 return g_atomic_int_xor (atomic, val);
273 * g_atomic_pointer_get:
274 * @atomic: a pointer to a #gpointer-sized value
276 * Gets the current value of @atomic.
278 * This call acts as a full compiler and hardware memory barrier (before
281 * Returns: the value of the pointer
286 (g_atomic_pointer_get) (volatile void *atomic)
288 return g_atomic_pointer_get ((volatile gpointer *) atomic);
292 * g_atomic_pointer_set:
293 * @atomic: a pointer to a #gpointer-sized value
294 * @newval: a new value to store
296 * Sets the value of @atomic to @newval.
298 * This call acts as a full compiler and hardware memory barrier (after
304 (g_atomic_pointer_set) (volatile void *atomic,
307 g_atomic_pointer_set ((volatile gpointer *) atomic, newval);
311 * g_atomic_pointer_compare_and_exchange:
312 * @atomic: a pointer to a #gpointer-sized value
313 * @oldval: the value to compare with
314 * @newval: the value to conditionally replace with
316 * Compares @atomic to @oldval and, if equal, sets it to @newval. If
317 * @atomic was not equal to @oldval then no change occurs.
319 * This compare and exchange is done atomically.
321 * This call acts as a full compiler and hardware memory barrier.
323 * Returns: %TRUE if the exchange took place
328 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
332 return g_atomic_pointer_compare_and_exchange ((volatile gpointer *) atomic,
337 * g_atomic_pointer_add:
338 * @atomic: a pointer to a #gpointer-sized value
339 * @val: the value to add
341 * Atomically adds @val to the value of @atomic.
343 * This call acts as a full compiler and hardware memory barrier.
345 * Returns: the value of @atomic before the add, signed
350 (g_atomic_pointer_add) (volatile void *atomic,
353 return g_atomic_pointer_add ((volatile gpointer *) atomic, val);
357 * g_atomic_pointer_and:
358 * @atomic: a pointer to a #gpointer-sized value
359 * @val: the value to 'and'
361 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
362 * storing the result back in @atomic.
364 * This call acts as a full compiler and hardware memory barrier.
366 * Returns: the value of @atomic before the operation, unsigned
371 (g_atomic_pointer_and) (volatile void *atomic,
374 return g_atomic_pointer_and ((volatile gpointer *) atomic, val);
378 * g_atomic_pointer_or:
379 * @atomic: a pointer to a #gpointer-sized value
380 * @val: the value to 'or'
382 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
383 * storing the result back in @atomic.
385 * This call acts as a full compiler and hardware memory barrier.
387 * Returns: the value of @atomic before the operation, unsigned
392 (g_atomic_pointer_or) (volatile void *atomic,
395 return g_atomic_pointer_or ((volatile gpointer *) atomic, val);
399 * g_atomic_pointer_xor:
400 * @atomic: a pointer to a #gpointer-sized value
401 * @val: the value to 'xor'
403 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
404 * storing the result back in @atomic.
406 * This call acts as a full compiler and hardware memory barrier.
408 * Returns: the value of @atomic before the operation, unsigned
413 (g_atomic_pointer_xor) (volatile void *atomic,
416 return g_atomic_pointer_xor ((volatile gpointer *) atomic, val);
419 #elif defined (G_PLATFORM_WIN32)
422 * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
425 (g_atomic_int_get) (volatile gint *atomic)
432 (g_atomic_int_set) (volatile gint *atomic,
440 (g_atomic_int_inc) (volatile gint *atomic)
442 InterlockedIncrement (atomic);
446 (g_atomic_int_dec_and_test) (volatile gint *atomic)
448 return InterlockedDecrement (atomic) == 0;
452 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
456 return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
460 (g_atomic_int_add) (volatile gint *atomic,
463 return InterlockedExchangeAdd (atomic, val);
467 (g_atomic_int_and) (volatile guint *atomic,
470 return InterlockedAnd (atomic, val);
474 (g_atomic_int_or) (volatile guint *atomic,
477 return InterlockedOr (atomic, val);
481 (g_atomic_int_xor) (volatile guint *atomic,
484 return InterlockedXor (atomic, val);
489 (g_atomic_pointer_get) (volatile void *atomic)
491 volatile gpointer *ptr = atomic;
498 (g_atomic_pointer_set) (volatile void *atomic,
501 volatile gpointer *ptr = atomic;
508 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
512 return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
516 (g_atomic_pointer_add) (volatile void *atomic,
519 #if GLIB_SIZEOF_VOID_P == 8
520 return InterlockedExchangeAdd64 (atomic, val);
522 return InterlockedExchangeAdd (atomic, val);
527 (g_atomic_pointer_and) (volatile void *atomic,
530 #if GLIB_SIZEOF_VOID_P == 8
531 return InterlockedAnd64 (atomic, val);
533 return InterlockedAnd (atomic, val);
538 (g_atomic_pointer_or) (volatile void *atomic,
541 #if GLIB_SIZEOF_VOID_P == 8
542 return InterlockedOr64 (atomic, val);
544 return InterlockedOr (atomic, val);
549 (g_atomic_pointer_xor) (volatile void *atomic,
552 #if GLIB_SIZEOF_VOID_P == 8
553 return InterlockedXor64 (atomic, val);
555 return InterlockedXor (atomic, val);
563 static GStaticMutex g_atomic_lock;
566 (g_atomic_int_get) (volatile gint *atomic)
570 g_static_mutex_lock (&g_atomic_lock);
572 g_static_mutex_unlock (&g_atomic_lock);
578 (g_atomic_int_set) (volatile gint *atomic,
581 g_static_mutex_lock (&g_atomic_lock);
583 g_static_mutex_unlock (&g_atomic_lock);
587 (g_atomic_int_inc) (volatile gint *atomic)
589 g_static_mutex_lock (&g_atomic_lock);
591 g_static_mutex_unlock (&g_atomic_lock);
595 (g_atomic_int_dec_and_test) (volatile gint *atomic)
599 g_static_mutex_lock (&g_atomic_lock);
600 is_zero = --(*atomic) == 0;
601 g_static_mutex_unlock (&g_atomic_lock);
607 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
613 g_static_mutex_lock (&g_atomic_lock);
615 if ((success = (*atomic == oldval)))
618 g_static_mutex_unlock (&g_atomic_lock);
624 (g_atomic_int_add) (volatile gint *atomic,
629 g_static_mutex_lock (&g_atomic_lock);
631 *atomic = oldval + val;
632 g_static_mutex_unlock (&g_atomic_lock);
638 (g_atomic_int_and) (volatile guint *atomic,
643 g_static_mutex_lock (&g_atomic_lock);
645 *atomic = oldval & val;
646 g_static_mutex_unlock (&g_atomic_lock);
652 (g_atomic_int_or) (volatile guint *atomic,
657 g_static_mutex_lock (&g_atomic_lock);
659 *atomic = oldval | val;
660 g_static_mutex_unlock (&g_atomic_lock);
666 (g_atomic_int_xor) (volatile guint *atomic,
671 g_static_mutex_lock (&g_atomic_lock);
673 *atomic = oldval ^ val;
674 g_static_mutex_unlock (&g_atomic_lock);
681 (g_atomic_pointer_get) (volatile void *atomic)
683 volatile gpointer *ptr = atomic;
686 g_static_mutex_lock (&g_atomic_lock);
688 g_static_mutex_unlock (&g_atomic_lock);
694 (g_atomic_pointer_set) (volatile void *atomic,
697 volatile gpointer *ptr = atomic;
699 g_static_mutex_lock (&g_atomic_lock);
701 g_static_mutex_unlock (&g_atomic_lock);
705 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
709 volatile gpointer *ptr = atomic;
712 g_static_mutex_lock (&g_atomic_lock);
714 if ((success = (*ptr == oldval)))
717 g_static_mutex_unlock (&g_atomic_lock);
723 (g_atomic_pointer_add) (volatile void *atomic,
726 volatile gssize *ptr = atomic;
729 g_static_mutex_lock (&g_atomic_lock);
732 g_static_mutex_unlock (&g_atomic_lock);
738 (g_atomic_pointer_and) (volatile void *atomic,
741 volatile gsize *ptr = atomic;
744 g_static_mutex_lock (&g_atomic_lock);
747 g_static_mutex_unlock (&g_atomic_lock);
753 (g_atomic_pointer_or) (volatile void *atomic,
756 volatile gsize *ptr = atomic;
759 g_static_mutex_lock (&g_atomic_lock);
762 g_static_mutex_unlock (&g_atomic_lock);
768 (g_atomic_pointer_xor) (volatile void *atomic,
771 volatile gsize *ptr = atomic;
774 g_static_mutex_lock (&g_atomic_lock);
777 g_static_mutex_unlock (&g_atomic_lock);
785 * g_atomic_int_exchange_and_add:
786 * @atomic: a pointer to a #gint
787 * @val: the value to add
789 * This function existed before g_atomic_int_add() returned the prior
790 * value of the integer (which it now does). It is retained only for
791 * compatibility reasons. Don't use this function in new code.
793 * Returns: the value of @atomic before the add, signed
795 * Deprecated: 2.30: Use g_atomic_int_add() instead.
798 g_atomic_int_exchange_and_add (volatile gint *atomic,
801 return (g_atomic_int_add) (atomic, val);