1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GAtomic: atomic integer operation.
5 * Copyright (C) 2003 Sebastian Wilhelmi
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 #ifndef __G_ATOMIC_H__
31 #define __G_ATOMIC_H__
33 #include <glib/gtypes.h>
37 #ifdef G_THREADS_ENABLED
39 gint32 g_atomic_int_exchange_and_add_fallback (gint32 *atomic,
41 void g_atomic_int_add_fallback (gint32 *atomic,
43 gboolean g_atomic_int_compare_and_exchange_fallback (gint32 *atomic,
46 gboolean g_atomic_pointer_compare_and_exchange_fallback (gpointer *atomic,
50 # if defined (__GNUC__)
51 # if defined (G_ATOMIC_INLINED_IMPLEMENTATION_I486)
52 /* Adapted from CVS version 1.10 of glibc's sysdeps/i386/i486/bits/atomic.h
55 g_atomic_int_exchange_and_add (gint32 *atomic,
60 __asm__ __volatile__ ("lock; xaddl %0,%1"
61 : "=r" (result), "=m" (*atomic)
62 : "0" (val), "m" (*atomic));
67 g_atomic_int_add (gint32 *atomic,
70 __asm__ __volatile__ ("lock; addl %1,%0"
72 : "ir" (val), "m" (*atomic));
75 static inline gboolean
76 g_atomic_int_compare_and_exchange (gint32 *atomic,
82 __asm __volatile ("lock; cmpxchgl %2, %1"
83 : "=a" (result), "=m" (*atomic)
84 : "r" (newval), "m" (*atomic), "0" (oldval));
86 return result == oldval;
89 /* The same code as above, as on i386 gpointer is 32 bit as well.
90 * Duplicating the code here seems more natural than casting the
91 * arguments and calling the former function */
93 static inline gboolean
94 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
100 __asm __volatile ("lock; cmpxchgl %2, %1"
101 : "=a" (result), "=m" (*atomic)
102 : "r" (newval), "m" (*atomic), "0" (oldval));
104 return result == oldval;
107 # define G_ATOMIC_MEMORY_BARRIER() /* Not needed */
109 # elif defined(G_ATOMIC_INLINED_IMPLEMENTATION_SPARCV9) \
110 && (defined(__sparcv8) || defined(__sparcv9) || defined(__sparc_v9__))
111 /* Adapted from CVS version 1.3 of glibc's sysdeps/sparc/sparc64/bits/atomic.h
113 /* Why the test for __sparcv8, wheras really the sparcv9 architecture
114 * is required for the folowing assembler instructions? On
115 * sparc-solaris the only difference detectable at compile time
116 * between no -m and -mcpu=v9 is __sparcv8.
118 * However, in case -mcpu=v8 is set, the assembler will fail. This
119 * should be rare however, as there are only very few v8-not-v9
120 * machines still out there (and we can't do better).
122 static inline gboolean
123 g_atomic_int_compare_and_exchange (gint32 *atomic,
128 __asm __volatile ("cas [%4], %2, %0"
129 : "=r" (result), "=m" (*atomic)
130 : "r" (oldval), "m" (*atomic), "r" (atomic),
135 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
136 static inline gboolean
137 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
142 __asm __volatile ("cas [%4], %2, %0"
143 : "=r" (result), "=m" (*atomic)
144 : "r" (oldval), "m" (*atomic), "r" (atomic),
148 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
149 static inline gboolean
150 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
155 gpointer *a = atomic;
156 __asm __volatile ("casx [%4], %2, %0"
157 : "=r" (result), "=m" (*a)
158 : "r" (oldval), "m" (*a), "r" (a),
162 # else /* What's that */
163 # error "Your system has an unsupported pointer size"
164 # endif /* GLIB_SIZEOF_VOID_P */
166 g_atomic_int_exchange_and_add (gint32 *atomic,
172 while (!g_atomic_int_compare_and_exchange (atomic, result, result + val));
178 g_atomic_int_add (gint32 *atomic,
181 g_atomic_int_exchange_and_add (atomic, val);
184 # define G_ATOMIC_MEMORY_BARRIER() \
185 __asm __volatile ("membar #LoadLoad | #LoadStore" \
186 " | #StoreLoad | #StoreStore" : : : "memory")
188 # elif defined(G_ATOMIC_INLINED_IMPLEMENTATION_ALPHA)
189 /* Adapted from CVS version 1.3 of glibc's sysdeps/alpha/bits/atomic.h
191 static inline gboolean
192 g_atomic_int_compare_and_exchange (gint32 *atomic,
198 __asm__ __volatile__ (
211 "Ir" ((gint64)oldval),
216 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
217 static inline gboolean
218 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
224 __asm__ __volatile__ (
237 "Ir" ((gint64)oldval),
242 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
243 static inline gboolean
244 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
250 __asm__ __volatile__ (
263 "Ir" ((gint64)oldval),
268 # else /* What's that */
269 # error "Your system has an unsupported pointer size"
270 # endif /* GLIB_SIZEOF_VOID_P */
272 g_atomic_int_exchange_and_add (gint32 *atomic,
278 while (!g_atomic_int_compare_and_exchange (atomic, result, result + val));
284 g_atomic_int_add (gint32 *atomic,
287 g_atomic_int_exchange_and_add (atomic, val);
290 # define G_ATOMIC_MEMORY_BARRIER() __asm ("mb" : : : "memory")
292 # elif defined(G_ATOMIC_INLINED_IMPLEMENTATION_X86_64)
293 /* Adapted from CVS version 1.9 of glibc's sysdeps/x86_64/bits/atomic.h
296 g_atomic_int_exchange_and_add (gint32 *atomic,
301 __asm__ __volatile__ ("lock; xaddl %0,%1"
302 : "=r" (result), "=m" (*atomic)
303 : "0" (val), "m" (*atomic));
308 g_atomic_int_add (gint32 *atomic,
311 __asm__ __volatile__ ("lock; addl %1,%0"
313 : "ir" (val), "m" (*atomic));
316 static inline gboolean
317 g_atomic_int_compare_and_exchange (gint32 *atomic,
323 __asm __volatile ("lock; cmpxchgl %2, %1"
324 : "=a" (result), "=m" (*atomic)
325 : "r" (newval), "m" (*atomic), "0" (oldval));
327 return result == oldval;
330 static inline gboolean
331 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
337 __asm __volatile ("lock; cmpxchgq %q2, %1"
338 : "=a" (result), "=m" (*atomic)
339 : "r" (newval), "m" (*atomic), "0" (oldval));
341 return result == oldval;
344 # define G_ATOMIC_MEMORY_BARRIER() /* Not needed */
346 # elif defined(G_ATOMIC_INLINED_IMPLEMENTATION_POWERPC)
347 /* Adapted from CVS version 1.12 of glibc's sysdeps/powerpc/bits/atomic.h
348 * and CVS version 1.3 of glibc's sysdeps/powerpc/powerpc32/bits/atomic.h
349 * and CVS version 1.2 of glibc's sysdeps/powerpc/powerpc64/bits/atomic.h
352 g_atomic_int_exchange_and_add (gint32 *atomic,
356 __asm __volatile ("1: lwarx %0,0,%3\n"
360 : "=&b" (result), "=&r" (temp), "=m" (*atomic)
361 : "b" (atomic), "r" (val), "2" (*atomic)
367 g_atomic_int_add (gint32 *atomic,
370 g_atomic_int_exchange_and_add (atomic, val);
373 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
374 static inline gboolean
375 g_atomic_int_compare_and_exchange (gint32 *atomic,
380 __asm __volatile ("sync\n"
388 : "b" (atomic), "r" (oldval), "r" (newval)
393 static inline gboolean
394 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
399 __asm __volatile ("sync\n"
407 : "b" (atomic), "r" (oldval), "r" (newval)
411 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
412 static inline gboolean
413 g_atomic_int_compare_and_exchange (gint32 *atomic,
417 __asm __volatile ("sync\n"
426 : "b" (atomic), "r" (oldval), "r" (newval)
431 static inline gboolean
432 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
437 __asm __volatile ("sync\n"
445 : "b" (atomic), "r" (oldval), "r" (newval)
449 # else /* What's that */
450 # error "Your system has an unsupported pointer size"
451 # endif /* GLIB_SIZEOF_VOID_P */
453 # define G_ATOMIC_MEMORY_BARRIER() __asm ("sync" : : : "memory")
455 # elif defined(G_ATOMIC_INLINED_IMPLEMENTATION_IA64)
456 /* Adapted from CVS version 1.8 of glibc's sysdeps/ia64/bits/atomic.h
459 g_atomic_int_exchange_and_add (gint32 *atomic,
462 return __sync_fetch_and_add_si (atomic, val);
466 g_atomic_int_add (gint32 *atomic,
469 __sync_fetch_and_add_si (atomic, val);
472 static inline gboolean
473 g_atomic_int_compare_and_exchange (gint32 *atomic,
477 return __sync_bool_compare_and_exchange_si (atomic, oldval, newval);
480 static inline gboolean
481 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
485 return __sync_bool_compare_and_exchange_di ((long *)atomic,
486 (long)oldval, (long)newval);
489 # define G_ATOMIC_MEMORY_BARRIER() __sync_synchronize ()
491 # else /* !G_ATOMIC_INLINED_IMPLEMENTATION_... */
492 # define G_ATOMIC_USE_FALLBACK_IMPLEMENTATION
493 # endif /* G_ATOMIC_INLINED_IMPLEMENTATION_... */
494 # else /* !__GNU__ */
495 # define G_ATOMIC_USE_FALLBACK_IMPLEMENTATION
496 # endif /* __GNUC__ */
497 # ifdef G_ATOMIC_USE_FALLBACK_IMPLEMENTATION
498 # define g_atomic_int_exchange_and_add \
499 g_atomic_int_exchange_and_add_fallback
500 # define g_atomic_int_add \
501 g_atomic_int_add_fallback
502 # define g_atomic_int_compare_and_exchange \
503 g_atomic_int_compare_and_exchange_fallback
504 # define g_atomic_pointer_compare_and_exchange \
505 g_atomic_pointer_compare_and_exchange_fallback
506 # define g_atomic_int_get \
507 g_atomic_int_get_fallback
508 # define g_atomic_pointer_get \
509 g_atomic_pointer_get_fallback
510 # else /* !G_ATOMIC_USE_FALLBACK_IMPLEMENTATION */
512 g_atomic_int_get (gint32 *atomic)
514 gint32 result = *atomic;
515 G_ATOMIC_MEMORY_BARRIER ();
519 static inline gpointer
520 g_atomic_pointer_get (gpointer *atomic)
522 gpointer result = *atomic;
523 G_ATOMIC_MEMORY_BARRIER ();
526 # endif /* G_ATOMIC_USE_FALLBACK_IMPLEMENTATION */
527 #else /* !G_THREADS_ENABLED */
528 gint32 g_atomic_int_exchange_and_add (gint32 *atomic, gint32 val);
529 # define g_atomic_int_add(atomic, val) (void)(*(atomic) += (val))
530 # define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
531 (*(atomic) == (oldval) ? (*(atomic) = (newval), TRUE) : FALSE)
532 # define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
533 (*(atomic) == (oldval) ? (*(atomic) = (newval), TRUE) : FALSE)
534 # define g_atomic_int_get(atomic) (*(atomic))
535 # define g_atomic_pointer_get(atomic) (*(atomic))
536 # define G_ATOMIC_MEMORY_BARRIER() /* Not needed */
537 #endif /* G_THREADS_ENABLED */
539 #define g_atomic_int_inc(atomic) (g_atomic_int_add ((atomic), 1))
540 #define g_atomic_int_dec_and_test(atomic) \
541 (g_atomic_int_exchange_and_add ((atomic), -1) == 1)
545 #endif /* __G_ATOMIC_H__ */