Add macro version for all atomic operations
[platform/upstream/glib.git] / glib / gatomic.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * g_atomic_*: atomic operations.
5  * Copyright (C) 2003 Sebastian Wilhelmi
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
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/.
28  */
29
30 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
32 #endif
33
34 #ifndef __G_ATOMIC_H__
35 #define __G_ATOMIC_H__
36
37 #include <glib/gtypes.h>
38
39 G_BEGIN_DECLS
40
41 gint     g_atomic_int_exchange_and_add         (volatile gint G_GNUC_MAY_ALIAS *atomic,
42                                                 gint          val);
43 void     g_atomic_int_add                      (volatile gint G_GNUC_MAY_ALIAS *atomic,
44                                                 gint          val);
45 gboolean g_atomic_int_compare_and_exchange     (volatile gint G_GNUC_MAY_ALIAS *atomic,
46                                                 gint          oldval,
47                                                 gint          newval);
48 gboolean g_atomic_pointer_compare_and_exchange (volatile gpointer G_GNUC_MAY_ALIAS *atomic,
49                                                 gpointer      oldval,
50                                                 gpointer      newval);
51
52 gint     g_atomic_int_get                      (volatile gint G_GNUC_MAY_ALIAS *atomic);
53 void     g_atomic_int_set                      (volatile gint G_GNUC_MAY_ALIAS *atomic,
54                                                 gint          newval);
55 gpointer g_atomic_pointer_get                  (volatile gpointer G_GNUC_MAY_ALIAS *atomic);
56 void     g_atomic_pointer_set                  (volatile gpointer G_GNUC_MAY_ALIAS *atomic,
57                                                 gpointer      newval);
58
59 #if defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS)
60
61 #define g_atomic_int_exchange_and_add(atomic,val) \
62   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
63    __sync_fetch_and_add((atomic),(val)); })
64
65 #define g_atomic_int_add(atomic,val) \
66   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
67    __sync_fetch_and_add((atomic),(val)); })
68
69 #define g_atomic_int_compare_and_exchange(atomic,oldval,newval) \
70   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
71    __sync_bool_compare_and_swap((atomic),(oldval),(newval)); })
72
73 #define g_atomic_int_get(atomic) \
74   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
75    __sync_synchronize(); *(atomic); })
76
77 #define g_atomic_int_set(atomic,newval) \
78   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
79     *(atomic) = (newval); __sync_synchronize(); })
80
81 #define g_atomic_pointer_compare_and_exchange(atomic,oldval,newval) \
82   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)); \
83     __sync_bool_compare_and_swap((atomic),(oldval),(newval)); })
84
85 #define g_atomic_pointer_get(atomic) \
86   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)); \
87     __sync_synchronize(); *(atomic); })
88
89 #define g_atomic_pointer_set(atomic,newval) \
90   __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)); \
91     *(atomic) = (newval); __sync_synchronize(); })
92
93 #elif !defined(G_ATOMIC_OP_MEMORY_BARRIER_NEEDED)
94
95 # define g_atomic_int_get(atomic)               ((gint)*(atomic))
96 # define g_atomic_int_set(atomic, newval)       ((void) (*(atomic) = (newval)))
97 # define g_atomic_pointer_get(atomic)           ((gpointer)*(atomic))
98 # define g_atomic_pointer_set(atomic, newval)   ((void) (*(atomic) = (newval)))
99
100 #else
101
102 #define g_atomic_int_exchange_and_add(atomic,val) \
103  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
104   (g_atomic_int_exchange_and_add) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (val)))
105 #define g_atomic_int_add(atomic,val) \
106  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
107   (g_atomic_int_add) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (val)))
108 #define g_atomic_int_compare_and_exchange(atomic,oldval,newval) \
109  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
110   (g_atomic_int_compare_and_exchange) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (oldval), (newval)))
111 #define g_atomic_pointer_compare_and_exchange(atomic,oldval,newval) \
112  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)), \
113   (g_atomic_pointer_compare_and_exchange) ((volatile gpointer G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (oldval), (newval)))
114 #define g_atomic_int_get(atomic) \
115  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
116   (g_atomic_int_get) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic)))
117 #define g_atomic_int_set(atomic, newval) \
118  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
119   (g_atomic_int_set) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (newval)))
120 #define g_atomic_pointer_get(atomic) \
121  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)), \
122   (g_atomic_pointer_get) ((volatile gpointer G_GNUC_MAY_ALIAS *) (volatile void *) (atomic)))
123 #define g_atomic_pointer_set(atomic, newval) \
124  (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)), \
125   (g_atomic_pointer_set) ((volatile gpointer G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (newval)))
126
127 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
128
129 /**
130  * g_atomic_int_inc:
131  * @atomic: a pointer to an integer.
132  *
133  * Atomically increments the integer pointed to by @atomic by 1.
134  *
135  * Since: 2.4
136  */
137 #define g_atomic_int_inc(atomic) (g_atomic_int_add ((atomic), 1))
138
139 /**
140  * g_atomic_int_dec_and_test:
141  * @atomic: a pointer to an integer
142  *
143  * Atomically decrements the integer pointed to by @atomic by 1.
144  *
145  * Returns: %TRUE if the integer pointed to by @atomic is 0
146  *     after decrementing it
147  *
148  * Since: 2.4
149  */
150 #define g_atomic_int_dec_and_test(atomic) \
151   (g_atomic_int_exchange_and_add ((atomic), -1) == 1)
152
153 G_END_DECLS
154
155 #endif /* __G_ATOMIC_H__ */