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
98 * memory barrier (before the get).
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
118 * memory barrier (after the set).
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 * Think of this operation as an atomic version of
136 * <literal>{ *@atomic += 1; }</literal>
138 * This call acts as a full compiler and hardware memory barrier.
143 (g_atomic_int_inc) (volatile gint *atomic)
145 g_atomic_int_inc (atomic);
149 * g_atomic_int_dec_and_test:
150 * @atomic: a pointer to a #gint or #guint
152 * Decrements the value of @atomic by 1.
154 * Think of this operation as an atomic version of
155 * <literal>{ *@atomic -= 1; return (*@atomic == 0); }</literal>
157 * This call acts as a full compiler and hardware memory barrier.
159 * Returns: %TRUE if the resultant value is zero
164 (g_atomic_int_dec_and_test) (volatile gint *atomic)
166 return g_atomic_int_dec_and_test (atomic);
170 * g_atomic_int_compare_and_exchange:
171 * @atomic: a pointer to a #gint or #guint
172 * @oldval: the value to compare with
173 * @newval: the value to conditionally replace with
175 * Compares @atomic to @oldval and, if equal, sets it to @newval.
176 * If @atomic was not equal to @oldval then no change occurs.
178 * This compare and exchange is done atomically.
180 * Think of this operation as an atomic version of
181 * <literal>{ if (*@atomic == @oldval) { *@atomic = @newval; return TRUE; } else return FALSE; }</literal>
183 * This call acts as a full compiler and hardware memory barrier.
185 * Returns: %TRUE if the exchange took place
190 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
194 return g_atomic_int_compare_and_exchange (atomic, oldval, newval);
199 * @atomic: a pointer to a #gint or #guint
200 * @val: the value to add
202 * Atomically adds @val to the value of @atomic.
204 * Think of this operation as an atomic version of
205 * <literal>{ tmp = *atomic; *@atomic += @val; return tmp; }</literal>
207 * This call acts as a full compiler and hardware memory barrier.
209 * Before version 2.30, this function did not return a value
210 * (but g_atomic_int_exchange_and_add() did, and had the same meaning).
212 * Returns: the value of @atomic before the add, signed
217 (g_atomic_int_add) (volatile gint *atomic,
220 return g_atomic_int_add (atomic, val);
225 * @atomic: a pointer to a #gint or #guint
226 * @val: the value to 'and'
228 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
229 * storing the result back in @atomic.
231 * This call acts as a full compiler and hardware memory barrier.
233 * Think of this operation as an atomic version of
234 * <literal>{ tmp = *atomic; *@atomic &= @val; return tmp; }</literal>
236 * Returns: the value of @atomic before the operation, unsigned
241 (g_atomic_int_and) (volatile guint *atomic,
244 return g_atomic_int_and (atomic, val);
249 * @atomic: a pointer to a #gint or #guint
250 * @val: the value to 'or'
252 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
253 * storing the result back in @atomic.
255 * Think of this operation as an atomic version of
256 * <literal>{ tmp = *atomic; *@atomic |= @val; return tmp; }</literal>
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_or) (volatile guint *atomic,
268 return g_atomic_int_or (atomic, val);
273 * @atomic: a pointer to a #gint or #guint
274 * @val: the value to 'xor'
276 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
277 * storing the result back in @atomic.
279 * Think of this operation as an atomic version of
280 * <literal>{ tmp = *atomic; *@atomic ^= @val; return tmp; }</literal>
282 * This call acts as a full compiler and hardware memory barrier.
284 * Returns: the value of @atomic before the operation, unsigned
289 (g_atomic_int_xor) (volatile guint *atomic,
292 return g_atomic_int_xor (atomic, val);
297 * g_atomic_pointer_get:
298 * @atomic: a pointer to a #gpointer-sized value
300 * Gets the current value of @atomic.
302 * This call acts as a full compiler and hardware
303 * memory barrier (before the get).
305 * Returns: the value of the pointer
310 (g_atomic_pointer_get) (volatile void *atomic)
312 return g_atomic_pointer_get ((volatile gpointer *) atomic);
316 * g_atomic_pointer_set:
317 * @atomic: a pointer to a #gpointer-sized value
318 * @newval: a new value to store
320 * Sets the value of @atomic to @newval.
322 * This call acts as a full compiler and hardware
323 * memory barrier (after the set).
328 (g_atomic_pointer_set) (volatile void *atomic,
331 g_atomic_pointer_set ((volatile gpointer *) atomic, newval);
335 * g_atomic_pointer_compare_and_exchange:
336 * @atomic: a pointer to a #gpointer-sized value
337 * @oldval: the value to compare with
338 * @newval: the value to conditionally replace with
340 * Compares @atomic to @oldval and, if equal, sets it to @newval.
341 * If @atomic was not equal to @oldval then no change occurs.
343 * This compare and exchange is done atomically.
345 * Think of this operation as an atomic version of
346 * <literal>{ if (*@atomic == @oldval) { *@atomic = @newval; return TRUE; } else return FALSE; }</literal>
348 * This call acts as a full compiler and hardware memory barrier.
350 * Returns: %TRUE if the exchange took place
355 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
359 return g_atomic_pointer_compare_and_exchange ((volatile gpointer *) atomic,
364 * g_atomic_pointer_add:
365 * @atomic: a pointer to a #gpointer-sized value
366 * @val: the value to add
368 * Atomically adds @val to the value of @atomic.
370 * Think of this operation as an atomic version of
371 * <literal>{ tmp = *atomic; *@atomic += @val; return tmp; }</literal>
373 * This call acts as a full compiler and hardware memory barrier.
375 * Returns: the value of @atomic before the add, signed
380 (g_atomic_pointer_add) (volatile void *atomic,
383 return g_atomic_pointer_add ((volatile gpointer *) atomic, val);
387 * g_atomic_pointer_and:
388 * @atomic: a pointer to a #gpointer-sized value
389 * @val: the value to 'and'
391 * Performs an atomic bitwise 'and' of the value of @atomic and @val,
392 * storing the result back in @atomic.
394 * Think of this operation as an atomic version of
395 * <literal>{ tmp = *atomic; *@atomic &= @val; return tmp; }</literal>
397 * This call acts as a full compiler and hardware memory barrier.
399 * Returns: the value of @atomic before the operation, unsigned
404 (g_atomic_pointer_and) (volatile void *atomic,
407 return g_atomic_pointer_and ((volatile gpointer *) atomic, val);
411 * g_atomic_pointer_or:
412 * @atomic: a pointer to a #gpointer-sized value
413 * @val: the value to 'or'
415 * Performs an atomic bitwise 'or' of the value of @atomic and @val,
416 * storing the result back in @atomic.
418 * Think of this operation as an atomic version of
419 * <literal>{ tmp = *atomic; *@atomic |= @val; return tmp; }</literal>
421 * This call acts as a full compiler and hardware memory barrier.
423 * Returns: the value of @atomic before the operation, unsigned
428 (g_atomic_pointer_or) (volatile void *atomic,
431 return g_atomic_pointer_or ((volatile gpointer *) atomic, val);
435 * g_atomic_pointer_xor:
436 * @atomic: a pointer to a #gpointer-sized value
437 * @val: the value to 'xor'
439 * Performs an atomic bitwise 'xor' of the value of @atomic and @val,
440 * storing the result back in @atomic.
442 * Think of this operation as an atomic version of
443 * <literal>{ tmp = *atomic; *@atomic ^= @val; return tmp; }</literal>
445 * This call acts as a full compiler and hardware memory barrier.
447 * Returns: the value of @atomic before the operation, unsigned
452 (g_atomic_pointer_xor) (volatile void *atomic,
455 return g_atomic_pointer_xor ((volatile gpointer *) atomic, val);
458 #elif defined (G_PLATFORM_WIN32)
461 #if !defined(_M_AMD64) && !defined (_M_IA64) && !defined(_M_X64)
462 #define InterlockedAnd _InterlockedAnd
463 #define InterlockedOr _InterlockedOr
464 #define InterlockedXor _InterlockedXor
468 * http://msdn.microsoft.com/en-us/library/ms684122(v=vs.85).aspx
471 (g_atomic_int_get) (volatile gint *atomic)
478 (g_atomic_int_set) (volatile gint *atomic,
486 (g_atomic_int_inc) (volatile gint *atomic)
488 InterlockedIncrement (atomic);
492 (g_atomic_int_dec_and_test) (volatile gint *atomic)
494 return InterlockedDecrement (atomic) == 0;
498 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
502 return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
506 (g_atomic_int_add) (volatile gint *atomic,
509 return InterlockedExchangeAdd (atomic, val);
513 (g_atomic_int_and) (volatile guint *atomic,
516 return InterlockedAnd (atomic, val);
520 (g_atomic_int_or) (volatile guint *atomic,
523 return InterlockedOr (atomic, val);
527 (g_atomic_int_xor) (volatile guint *atomic,
530 return InterlockedXor (atomic, val);
535 (g_atomic_pointer_get) (volatile void *atomic)
537 volatile gpointer *ptr = atomic;
544 (g_atomic_pointer_set) (volatile void *atomic,
547 volatile gpointer *ptr = atomic;
554 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
558 return InterlockedCompareExchangePointer (atomic, newval, oldval) == oldval;
562 (g_atomic_pointer_add) (volatile void *atomic,
565 #if GLIB_SIZEOF_VOID_P == 8
566 return InterlockedExchangeAdd64 (atomic, val);
568 return InterlockedExchangeAdd (atomic, val);
573 (g_atomic_pointer_and) (volatile void *atomic,
576 #if GLIB_SIZEOF_VOID_P == 8
577 return InterlockedAnd64 (atomic, val);
579 return InterlockedAnd (atomic, val);
584 (g_atomic_pointer_or) (volatile void *atomic,
587 #if GLIB_SIZEOF_VOID_P == 8
588 return InterlockedOr64 (atomic, val);
590 return InterlockedOr (atomic, val);
595 (g_atomic_pointer_xor) (volatile void *atomic,
598 #if GLIB_SIZEOF_VOID_P == 8
599 return InterlockedXor64 (atomic, val);
601 return InterlockedXor (atomic, val);
609 static GStaticMutex g_atomic_lock;
612 (g_atomic_int_get) (volatile gint *atomic)
616 g_static_mutex_lock (&g_atomic_lock);
618 g_static_mutex_unlock (&g_atomic_lock);
624 (g_atomic_int_set) (volatile gint *atomic,
627 g_static_mutex_lock (&g_atomic_lock);
629 g_static_mutex_unlock (&g_atomic_lock);
633 (g_atomic_int_inc) (volatile gint *atomic)
635 g_static_mutex_lock (&g_atomic_lock);
637 g_static_mutex_unlock (&g_atomic_lock);
641 (g_atomic_int_dec_and_test) (volatile gint *atomic)
645 g_static_mutex_lock (&g_atomic_lock);
646 is_zero = --(*atomic) == 0;
647 g_static_mutex_unlock (&g_atomic_lock);
653 (g_atomic_int_compare_and_exchange) (volatile gint *atomic,
659 g_static_mutex_lock (&g_atomic_lock);
661 if ((success = (*atomic == oldval)))
664 g_static_mutex_unlock (&g_atomic_lock);
670 (g_atomic_int_add) (volatile gint *atomic,
675 g_static_mutex_lock (&g_atomic_lock);
677 *atomic = oldval + val;
678 g_static_mutex_unlock (&g_atomic_lock);
684 (g_atomic_int_and) (volatile guint *atomic,
689 g_static_mutex_lock (&g_atomic_lock);
691 *atomic = oldval & val;
692 g_static_mutex_unlock (&g_atomic_lock);
698 (g_atomic_int_or) (volatile guint *atomic,
703 g_static_mutex_lock (&g_atomic_lock);
705 *atomic = oldval | val;
706 g_static_mutex_unlock (&g_atomic_lock);
712 (g_atomic_int_xor) (volatile guint *atomic,
717 g_static_mutex_lock (&g_atomic_lock);
719 *atomic = oldval ^ val;
720 g_static_mutex_unlock (&g_atomic_lock);
727 (g_atomic_pointer_get) (volatile void *atomic)
729 volatile gpointer *ptr = atomic;
732 g_static_mutex_lock (&g_atomic_lock);
734 g_static_mutex_unlock (&g_atomic_lock);
740 (g_atomic_pointer_set) (volatile void *atomic,
743 volatile gpointer *ptr = atomic;
745 g_static_mutex_lock (&g_atomic_lock);
747 g_static_mutex_unlock (&g_atomic_lock);
751 (g_atomic_pointer_compare_and_exchange) (volatile void *atomic,
755 volatile gpointer *ptr = atomic;
758 g_static_mutex_lock (&g_atomic_lock);
760 if ((success = (*ptr == oldval)))
763 g_static_mutex_unlock (&g_atomic_lock);
769 (g_atomic_pointer_add) (volatile void *atomic,
772 volatile gssize *ptr = atomic;
775 g_static_mutex_lock (&g_atomic_lock);
778 g_static_mutex_unlock (&g_atomic_lock);
784 (g_atomic_pointer_and) (volatile void *atomic,
787 volatile gsize *ptr = atomic;
790 g_static_mutex_lock (&g_atomic_lock);
793 g_static_mutex_unlock (&g_atomic_lock);
799 (g_atomic_pointer_or) (volatile void *atomic,
802 volatile gsize *ptr = atomic;
805 g_static_mutex_lock (&g_atomic_lock);
808 g_static_mutex_unlock (&g_atomic_lock);
814 (g_atomic_pointer_xor) (volatile void *atomic,
817 volatile gsize *ptr = atomic;
820 g_static_mutex_lock (&g_atomic_lock);
823 g_static_mutex_unlock (&g_atomic_lock);
831 * g_atomic_int_exchange_and_add:
832 * @atomic: a pointer to a #gint
833 * @val: the value to add
835 * This function existed before g_atomic_int_add() returned the prior
836 * value of the integer (which it now does). It is retained only for
837 * compatibility reasons. Don't use this function in new code.
839 * Returns: the value of @atomic before the add, signed
841 * Deprecated: 2.30: Use g_atomic_int_add() instead.
844 g_atomic_int_exchange_and_add (volatile gint *atomic,
847 return (g_atomic_int_add) (atomic, val);