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.
27 #if defined (__GNUC__)
28 # if defined (G_ATOMIC_I486)
29 /* Adapted from CVS version 1.10 of glibc's sysdeps/i386/i486/bits/atomic.h
32 g_atomic_int_exchange_and_add (gint *atomic,
37 __asm__ __volatile__ ("lock; xaddl %0,%1"
38 : "=r" (result), "=m" (*atomic)
39 : "0" (val), "m" (*atomic));
44 g_atomic_int_add (gint *atomic,
47 __asm__ __volatile__ ("lock; addl %1,%0"
49 : "ir" (val), "m" (*atomic));
53 g_atomic_int_compare_and_exchange (gint *atomic,
59 __asm __volatile ("lock; cmpxchgl %2, %1"
60 : "=a" (result), "=m" (*atomic)
61 : "r" (newval), "m" (*atomic), "0" (oldval));
63 return result == oldval;
66 /* The same code as above, as on i386 gpointer is 32 bit as well.
67 * Duplicating the code here seems more natural than casting the
68 * arguments and calling the former function */
71 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
77 __asm __volatile ("lock; cmpxchgl %2, %1"
78 : "=a" (result), "=m" (*atomic)
79 : "r" (newval), "m" (*atomic), "0" (oldval));
81 return result == oldval;
84 # elif defined (G_ATOMIC_SPARCV9)
85 /* Adapted from CVS version 1.3 of glibc's sysdeps/sparc/sparc64/bits/atomic.h
87 # define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval) \
90 __asm __volatile ("cas [%4], %2, %0" \
91 : "=r" (__result), "=m" (*(atomic)) \
92 : "r" (oldval), "m" (*(atomic)), "r" (atomic), \
97 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
99 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
104 __asm __volatile ("cas [%4], %2, %0"
105 : "=r" (result), "=m" (*atomic)
106 : "r" (oldval), "m" (*atomic), "r" (atomic),
108 return result == oldval;
110 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
112 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
117 gpointer *a = atomic;
118 __asm __volatile ("casx [%4], %2, %0"
119 : "=r" (result), "=m" (*a)
120 : "r" (oldval), "m" (*a), "r" (a),
124 # else /* What's that */
125 # error "Your system has an unsupported pointer size"
126 # endif /* GLIB_SIZEOF_VOID_P */
127 # define G_ATOMIC_MEMORY_BARRIER \
128 __asm __volatile ("membar #LoadLoad | #LoadStore" \
129 " | #StoreLoad | #StoreStore" : : : "memory")
131 # elif defined (G_ATOMIC_ALPHA)
132 /* Adapted from CVS version 1.3 of glibc's sysdeps/alpha/bits/atomic.h
134 # define ATOMIC_INT_CMP_XCHG(atomic, oldval, newval) \
138 __asm__ __volatile__ ( \
141 " cmpeq %0,%3,%1\n" \
156 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
158 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
164 __asm__ __volatile__ (
182 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
184 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
190 __asm__ __volatile__ (
208 # else /* What's that */
209 # error "Your system has an unsupported pointer size"
210 # endif /* GLIB_SIZEOF_VOID_P */
211 # define G_ATOMIC_MEMORY_BARRIER __asm ("mb" : : : "memory")
212 # elif defined (G_ATOMIC_X86_64)
213 /* Adapted from CVS version 1.9 of glibc's sysdeps/x86_64/bits/atomic.h
216 g_atomic_int_exchange_and_add (gint *atomic,
221 __asm__ __volatile__ ("lock; xaddl %0,%1"
222 : "=r" (result), "=m" (*atomic)
223 : "0" (val), "m" (*atomic));
228 g_atomic_int_add (gint *atomic,
231 __asm__ __volatile__ ("lock; addl %1,%0"
233 : "ir" (val), "m" (*atomic));
237 g_atomic_int_compare_and_exchange (gint *atomic,
243 __asm __volatile ("lock; cmpxchgl %2, %1"
244 : "=a" (result), "=m" (*atomic)
245 : "r" (newval), "m" (*atomic), "0" (oldval));
247 return result == oldval;
251 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
257 __asm __volatile ("lock; cmpxchgq %q2, %1"
258 : "=a" (result), "=m" (*atomic)
259 : "r" (newval), "m" (*atomic), "0" (oldval));
261 return result == oldval;
264 # elif defined (G_ATOMIC_POWERPC)
265 /* Adapted from CVS version 1.12 of glibc's sysdeps/powerpc/bits/atomic.h
266 * and CVS version 1.3 of glibc's sysdeps/powerpc/powerpc32/bits/atomic.h
267 * and CVS version 1.2 of glibc's sysdeps/powerpc/powerpc64/bits/atomic.h
270 g_atomic_int_exchange_and_add (gint *atomic,
274 __asm __volatile ("1: lwarx %0,0,%3\n"
278 : "=&b" (result), "=&r" (temp), "=m" (*atomic)
279 : "b" (atomic), "r" (val), "2" (*atomic)
284 /* The same as above, to save a function call repeated here */
286 g_atomic_int_add (gint *atomic,
290 __asm __volatile ("1: lwarx %0,0,%3\n"
294 : "=&b" (result), "=&r" (temp), "=m" (*atomic)
295 : "b" (atomic), "r" (val), "2" (*atomic)
299 # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
301 g_atomic_int_compare_and_exchange (gint *atomic,
306 __asm __volatile ("sync\n"
314 : "b" (atomic), "r" (oldval), "r" (newval)
320 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
325 __asm __volatile ("sync\n"
333 : "b" (atomic), "r" (oldval), "r" (newval)
337 # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
339 g_atomic_int_compare_and_exchange (gint *atomic,
343 __asm __volatile ("sync\n"
352 : "b" (atomic), "r" (oldval), "r" (newval)
358 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
363 __asm __volatile ("sync\n"
371 : "b" (atomic), "r" (oldval), "r" (newval)
375 # else /* What's that */
376 # error "Your system has an unsupported pointer size"
377 # endif /* GLIB_SIZEOF_VOID_P */
379 # define G_ATOMIC_MEMORY_BARRIER __asm ("sync" : : : "memory")
381 # elif defined (G_ATOMIC_IA64)
382 /* Adapted from CVS version 1.8 of glibc's sysdeps/ia64/bits/atomic.h
385 g_atomic_int_exchange_and_add (gint *atomic,
388 return __sync_fetch_and_add_si (atomic, val);
392 g_atomic_int_add (gint *atomic,
395 __sync_fetch_and_add_si (atomic, val);
399 g_atomic_int_compare_and_exchange (gint *atomic,
403 return __sync_bool_compare_and_swap_si (atomic, oldval, newval);
407 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
411 return __sync_bool_compare_and_swap_di ((long *)atomic,
412 (long)oldval, (long)newval);
415 # define G_ATOMIC_MEMORY_BARRIER __sync_synchronize ()
416 # else /* !G_ATOMIC */
417 # define DEFINE_WITH_MUTEXES
418 # endif /* G_ATOMIC */
419 #else /* !__GNUC__ */
420 # ifdef G_PLATFORM_WIN32
421 # define DEFINE_WITH_WIN32_INTERLOCKED
423 # define DEFINE_WITH_MUTEXES
425 #endif /* __GNUC__ */
427 #ifdef DEFINE_WITH_WIN32_INTERLOCKED
428 # include <windows.h>
430 g_atomic_int_exchange_and_add (gint32 *atomic,
433 return InterlockedExchangeAdd (atomic, val);
437 g_atomic_int_add (gint32 *atomic,
440 InterlockedExchangeAdd (atomic, val);
444 g_atomic_int_compare_and_exchange (gint32 *atomic,
448 return (guint32)InterlockedCompareExchange ((PVOID*)atomic,
450 (PVOID)oldval) == oldval;
454 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
458 # if GLIB_SIZEOF_VOID_P != 4 /* no 32-bit system */
459 # error "InterlockedCompareExchangePointer needed"
461 return InterlockedCompareExchange (atomic, newval, oldval) == oldval;
464 #endif /* DEFINE_WITH_WIN32_INTERLOCKED */
466 #ifdef DEFINE_WITH_MUTEXES
467 /* We have to use the slow, but safe locking method */
468 G_LOCK_DEFINE_STATIC (g_atomic_lock);
470 g_atomic_int_exchange_and_add (gint *atomic,
475 G_LOCK (g_atomic_lock);
478 G_UNLOCK (g_atomic_lock);
485 g_atomic_int_add (gint *atomic,
488 G_LOCK (g_atomic_lock);
490 G_UNLOCK (g_atomic_lock);
494 g_atomic_int_compare_and_exchange (gint *atomic,
500 G_LOCK (g_atomic_lock);
501 if (*atomic == oldval)
508 G_UNLOCK (g_atomic_lock);
514 g_atomic_pointer_compare_and_exchange (gpointer *atomic,
520 G_LOCK (g_atomic_lock);
521 if (*atomic == oldval)
528 G_UNLOCK (g_atomic_lock);
533 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
535 g_atomic_int_get (gint *atomic)
539 G_LOCK (g_atomic_lock);
541 G_UNLOCK (g_atomic_lock);
547 g_atomic_pointer_get (gpointer *atomic)
551 G_LOCK (g_atomic_lock);
553 G_UNLOCK (g_atomic_lock);
557 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
558 #elif defined (G_ATOMIC_OP_MEMORY_BARRIER_NEEDED)
560 g_atomic_int_get (gint *atomic)
562 gint result = *atomic;
564 G_ATOMIC_MEMORY_BARRIER;
570 g_atomic_pointer_get (gpointer *atomic)
572 gpointer result = *atomic;
574 G_ATOMIC_MEMORY_BARRIER;
578 #endif /* DEFINE_WITH_MUTEXES || G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
580 #ifdef ATOMIC_INT_CMP_XCHG
582 g_atomic_int_compare_and_exchange (gint *atomic,
586 return ATOMIC_INT_CMP_XCHG (atomic, oldval, newval);
590 g_atomic_int_exchange_and_add (gint *atomic,
596 while (!ATOMIC_INT_CMP_XCHG (atomic, result, result + val));
602 g_atomic_int_add (gint *atomic,
608 while (!ATOMIC_INT_CMP_XCHG (atomic, result, result + val));
610 #endif /* ATOMIC_INT_CMP_XCHG */