1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * g_atomic_*: atomic operations.
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.
26 #include "gthreadinit.h"
28 #if defined (__GNUC__)
29 # if defined (G_ATOMIC_I486)
30 /* Adapted from CVS version 1.10 of glibc's sysdeps/i386/i486/bits/atomic.h
33 g_atomic_int_exchange_and_add (gint *atomic,
38 __asm__ __volatile__ ("lock; xaddl %0,%1"
39 : "=r" (result), "=m" (*atomic)
40 : "0" (val), "m" (*atomic));
45 g_atomic_int_add (gint *atomic,
48 __asm__ __volatile__ ("lock; addl %1,%0"
50 : "ir" (val), "m" (*atomic));
54 g_atomic_int_compare_and_exchange (gint *atomic,
60 __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
61 : "=a" (result), "=m" (*atomic)
62 : "r" (newval), "m" (*atomic), "0" (oldval));
64 return result == oldval;
67 /* The same code as above, as on i386 gpointer is 32 bit as well.
68 * Duplicating the code here seems more natural than casting the
69 * arguments and calling the former function */
72 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
78 __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
79 : "=a" (result), "=m" (*atomic)
80 : "r" (newval), "m" (*atomic), "0" (oldval));
82 return result == oldval;
85 # elif defined (G_ATOMIC_SPARCV9)
86 /* Adapted from CVS version 1.3 of glibc's sysdeps/sparc/sparc64/bits/atomic.h
88 # define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval) \
91 __asm__ __volatile__ ("cas [%4], %2, %0" \
92 : "=r" (__result), "=m" (*(atomic)) \
93 : "r" (oldval), "m" (*(atomic)), "r" (atomic),\
98 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
100 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
105 __asm__ __volatile__ ("cas [%4], %2, %0"
106 : "=r" (result), "=m" (*atomic)
107 : "r" (oldval), "m" (*atomic), "r" (atomic),
109 return result == oldval;
111 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
113 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
118 gpointer *a = atomic;
119 __asm__ __volatile__ ("casx [%4], %2, %0"
120 : "=r" (result), "=m" (*a)
121 : "r" (oldval), "m" (*a), "r" (a),
125 # else /* What's that */
126 # error "Your system has an unsupported pointer size"
127 # endif /* GLIB_SIZEOF_VOID_P */
128 # define G_ATOMIC_MEMORY_BARRIER \
129 __asm__ __volatile__ ("membar #LoadLoad | #LoadStore" \
130 " | #StoreLoad | #StoreStore" : : : "memory")
132 # elif defined (G_ATOMIC_ALPHA)
133 /* Adapted from CVS version 1.3 of glibc's sysdeps/alpha/bits/atomic.h
135 # define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval) \
139 __asm__ __volatile__ ( \
142 " cmpeq %0,%3,%1\n" \
157 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
159 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
165 __asm__ __volatile__ (
183 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
185 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
191 __asm__ __volatile__ (
209 # else /* What's that */
210 # error "Your system has an unsupported pointer size"
211 # endif /* GLIB_SIZEOF_VOID_P */
212 # define G_ATOMIC_MEMORY_BARRIER __asm__ ("mb" : : : "memory")
213 # elif defined (G_ATOMIC_X86_64)
214 /* Adapted from CVS version 1.9 of glibc's sysdeps/x86_64/bits/atomic.h
217 g_atomic_int_exchange_and_add (gint *atomic,
222 __asm__ __volatile__ ("lock; xaddl %0,%1"
223 : "=r" (result), "=m" (*atomic)
224 : "0" (val), "m" (*atomic));
229 g_atomic_int_add (gint *atomic,
232 __asm__ __volatile__ ("lock; addl %1,%0"
234 : "ir" (val), "m" (*atomic));
238 g_atomic_int_compare_and_exchange (gint *atomic,
244 __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
245 : "=a" (result), "=m" (*atomic)
246 : "r" (newval), "m" (*atomic), "0" (oldval));
248 return result == oldval;
252 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
258 __asm__ __volatile__ ("lock; cmpxchgq %q2, %1"
259 : "=a" (result), "=m" (*atomic)
260 : "r" (newval), "m" (*atomic), "0" (oldval));
262 return result == oldval;
265 # elif defined (G_ATOMIC_POWERPC)
266 /* Adapted from CVS version 1.12 of glibc's sysdeps/powerpc/bits/atomic.h
267 * and CVS version 1.3 of glibc's sysdeps/powerpc/powerpc32/bits/atomic.h
268 * and CVS version 1.2 of glibc's sysdeps/powerpc/powerpc64/bits/atomic.h
271 /* Non-optimizing compile bails on the following two asm statements
272 * for reasons unknown to the author */
274 g_atomic_int_exchange_and_add (gint *atomic,
278 __asm__ __volatile__ ("1: lwarx %0,0,%3\n"
282 : "=&b" (result), "=&r" (temp), "=m" (*atomic)
283 : "b" (atomic), "r" (val), "2" (*atomic)
288 /* The same as above, to save a function call repeated here */
290 g_atomic_int_add (gint *atomic,
294 __asm__ __volatile__ ("1: lwarx %0,0,%3\n"
298 : "=&b" (result), "=&r" (temp), "=m" (*atomic)
299 : "b" (atomic), "r" (val), "2" (*atomic)
302 # else /* !__OPTIMIZE__ */
304 g_atomic_int_exchange_and_add (gint *atomic,
310 while (!g_atomic_int_compare_and_exchange (atomic, result, result + val));
316 g_atomic_int_add (gint *atomic,
322 while (!g_atomic_int_compare_and_exchange (atomic, result, result + val));
324 # endif /* !__OPTIMIZE__ */
326 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
328 g_atomic_int_compare_and_exchange (gint *atomic,
333 __asm__ __volatile__ ("sync\n"
341 : "b" (atomic), "r" (oldval), "r" (newval)
347 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
352 __asm__ __volatile__ ("sync\n"
360 : "b" (atomic), "r" (oldval), "r" (newval)
364 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
366 g_atomic_int_compare_and_exchange (gint *atomic,
371 __asm__ __volatile__ ("sync\n"
380 : "b" (atomic), "r" (oldval), "r" (newval)
386 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
391 __asm__ __volatile__ ("sync\n"
399 : "b" (atomic), "r" (oldval), "r" (newval)
403 # else /* What's that */
404 # error "Your system has an unsupported pointer size"
405 # endif /* GLIB_SIZEOF_VOID_P */
407 # define G_ATOMIC_MEMORY_BARRIER __asm__ ("sync" : : : "memory")
409 # elif defined (G_ATOMIC_IA64)
410 /* Adapted from CVS version 1.8 of glibc's sysdeps/ia64/bits/atomic.h
413 g_atomic_int_exchange_and_add (gint *atomic,
416 return __sync_fetch_and_add_si (atomic, val);
420 g_atomic_int_add (gint *atomic,
423 __sync_fetch_and_add_si (atomic, val);
427 g_atomic_int_compare_and_exchange (gint *atomic,
431 return __sync_bool_compare_and_swap_si (atomic, oldval, newval);
435 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
439 return __sync_bool_compare_and_swap_di ((long *)atomic,
440 (long)oldval, (long)newval);
443 # define G_ATOMIC_MEMORY_BARRIER __sync_synchronize ()
444 # else /* !G_ATOMIC */
445 # define DEFINE_WITH_MUTEXES
446 # endif /* G_ATOMIC */
447 #else /* !__GNUC__ */
448 # ifdef G_PLATFORM_WIN32
449 # define DEFINE_WITH_WIN32_INTERLOCKED
451 # define DEFINE_WITH_MUTEXES
453 #endif /* __GNUC__ */
455 #ifdef DEFINE_WITH_WIN32_INTERLOCKED
456 # include <windows.h>
458 g_atomic_int_exchange_and_add (gint32 *atomic,
461 return InterlockedExchangeAdd (atomic, val);
465 g_atomic_int_add (gint32 *atomic,
468 InterlockedExchangeAdd (atomic, val);
472 g_atomic_int_compare_and_exchange (gint32 *atomic,
476 return (guint32)InterlockedCompareExchange ((PVOID*)atomic,
478 (PVOID)oldval) == oldval;
482 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
486 # if GLIB_SIZEOF_VOID_P != 4 /* no 32-bit system */
487 # error "InterlockedCompareExchangePointer needed"
489 return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
492 #endif /* DEFINE_WITH_WIN32_INTERLOCKED */
494 #ifdef DEFINE_WITH_MUTEXES
495 /* We have to use the slow, but safe locking method */
496 static GMutex *g_atomic_mutex;
499 g_atomic_int_exchange_and_add (gint *atomic,
504 g_mutex_lock (g_atomic_mutex);
507 g_mutex_unlock (g_atomic_mutex);
514 g_atomic_int_add (gint *atomic,
517 g_mutex_lock (g_atomic_mutex);
519 g_mutex_unlock (g_atomic_mutex);
523 g_atomic_int_compare_and_exchange (gint *atomic,
529 g_mutex_lock (g_atomic_mutex);
530 if (*atomic == oldval)
537 g_mutex_unlock (g_atomic_mutex);
543 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
549 g_mutex_lock (g_atomic_mutex);
550 if (*atomic == oldval)
557 g_mutex_unlock (g_atomic_mutex);
562 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
564 g_atomic_int_get (gint *atomic)
568 g_mutex_lock (g_atomic_mutex);
570 g_mutex_unlock (g_atomic_mutex);
576 g_atomic_pointer_get (gpointer *atomic)
580 g_mutex_lock (g_atomic_mutex);
582 g_mutex_unlock (g_atomic_mutex);
586 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
587 #elif defined (G_ATOMIC_OP_MEMORY_BARRIER_NEEDED)
589 g_atomic_int_get (gint *atomic)
591 gint result = *atomic;
593 G_ATOMIC_MEMORY_BARRIER;
599 g_atomic_pointer_get (gpointer *atomic)
601 gpointer result = *atomic;
603 G_ATOMIC_MEMORY_BARRIER;
607 #endif /* DEFINE_WITH_MUTEXES || G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
609 #ifdef ATOMIC_INT_CMP_XCHG
611 g_atomic_int_compare_and_exchange (gint *atomic,
615 return ATOMIC_INT_CMP_XCHG (atomic, oldval, newval);
619 g_atomic_int_exchange_and_add (gint *atomic,
625 while (!ATOMIC_INT_CMP_XCHG (atomic, result, result + val));
631 g_atomic_int_add (gint *atomic,
637 while (!ATOMIC_INT_CMP_XCHG (atomic, result, result + val));
639 #endif /* ATOMIC_INT_CMP_XCHG */
642 _g_atomic_thread_init ()
644 #ifdef DEFINE_WITH_MUTEXES
645 g_atomic_mutex = g_mutex_new ();
646 #endif /* DEFINE_WITH_MUTEXES */