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.
25 #ifdef G_THREADS_ENABLED
26 # if !defined (G_ATOMIC_USE_FALLBACK_IMPLEMENTATION)
27 /* We have an inline implementation, which we can now use for the
28 * fallback implementation. This fallback implementation is only used by
29 * modules, which are not compliled with gcc
33 g_atomic_int_exchange_and_add_fallback (gint32 *atomic,
36 return g_atomic_int_exchange_and_add (atomic, val);
41 g_atomic_int_add_fallback (gint32 *atomic,
44 g_atomic_int_add (atomic, val);
48 g_atomic_int_compare_and_exchange_fallback (gint32 *atomic,
52 return g_atomic_int_compare_and_exchange (atomic, oldval, newval);
56 g_atomic_pointer_compare_and_exchange_fallback (gpointer *atomic,
60 return g_atomic_pointer_compare_and_exchange (atomic, oldval, newval);
64 g_atomic_int_get_fallback (gint32 *atomic)
66 return g_atomic_int_get (atomic);
70 g_atomic_pointer_get_fallback (gpointer *atomic)
72 return g_atomic_int_get (atomic);
75 # else /* !G_ATOMIC_USE_FALLBACK_IMPLEMENTATION */
76 /* We have to use the slow, but safe locking method */
77 G_LOCK_DEFINE_STATIC (g_atomic_lock);
80 g_atomic_int_exchange_and_add_fallback (gint32 *atomic,
85 G_LOCK (g_atomic_lock);
88 G_UNLOCK (g_atomic_lock);
95 g_atomic_int_add_fallback (gint32 *atomic,
98 G_LOCK (g_atomic_lock);
100 G_UNLOCK (g_atomic_lock);
104 g_atomic_int_compare_and_exchange_fallback (gint32 *atomic,
110 G_LOCK (g_atomic_lock);
111 if (*atomic == oldval)
118 G_UNLOCK (g_atomic_lock);
124 g_atomic_pointer_compare_and_exchange_fallback (gpointer *atomic,
130 G_LOCK (g_atomic_lock);
131 if (*atomic == oldval)
138 G_UNLOCK (g_atomic_lock);
144 g_atomic_int_get_fallback (gint32 *atomic)
148 G_LOCK (g_atomic_lock);
150 G_UNLOCK (g_atomic_lock);
155 static inline gpointer
156 g_atomic_pointer_get_fallback (gpointer *atomic)
160 G_LOCK (g_atomic_lock);
162 G_UNLOCK (g_atomic_lock);
168 # endif /* G_ATOMIC_USE_FALLBACK_IMPLEMENTATION */
169 #else /* !G_THREADS_ENABLED */
170 gint32 g_atomic_int_exchange_and_add (gint32 *atomic,
173 gint32 result = *atomic;
177 #endif /* G_THREADS_ENABLED */