7b33b192f7de979ad34899c621ee7bb70452654e
[platform/upstream/glib2.0.git] / glib / gatomic-gcc.c
1 /* GLIB - Library of useful routines for C programming
2  * gatomic-gcc.c: atomic operations using GCC builtins.
3  * Copyright (C) 2009 Hiroyuki Ikezoe
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "glib.h"
24 #include "galias.h"
25
26 gint
27 g_atomic_int_exchange_and_add (volatile gint G_GNUC_MAY_ALIAS *atomic,
28                                gint           val)
29 {
30   return __sync_fetch_and_add (atomic, val);
31 }
32
33 void
34 g_atomic_int_add (volatile gint G_GNUC_MAY_ALIAS *atomic,
35                   gint val)
36 {
37   __sync_fetch_and_add (atomic, val);
38 }
39
40 gboolean
41 g_atomic_int_compare_and_exchange (volatile gint G_GNUC_MAY_ALIAS *atomic,
42                                    gint           oldval,
43                                    gint           newval)
44 {
45   return __sync_bool_compare_and_swap (atomic, oldval, newval);
46 }
47
48 gboolean
49 g_atomic_pointer_compare_and_exchange (volatile gpointer G_GNUC_MAY_ALIAS *atomic,
50                                        gpointer           oldval,
51                                        gpointer           newval)
52 {
53   return __sync_bool_compare_and_swap (atomic, oldval, newval);
54 }
55
56 void
57 _g_atomic_thread_init (void)
58 {
59 }
60
61 gint
62 (g_atomic_int_get) (volatile gint G_GNUC_MAY_ALIAS *atomic)
63 {
64   __sync_synchronize ();
65   return *atomic;
66 }
67
68 void
69 (g_atomic_int_set) (volatile gint G_GNUC_MAY_ALIAS *atomic,
70                     gint           newval)
71 {
72   *atomic = newval;
73   __sync_synchronize ();
74 }
75
76 gpointer
77 (g_atomic_pointer_get) (volatile gpointer G_GNUC_MAY_ALIAS *atomic)
78 {
79   __sync_synchronize ();
80   return *atomic;
81 }
82
83 void
84 (g_atomic_pointer_set) (volatile gpointer G_GNUC_MAY_ALIAS *atomic,
85                         gpointer           newval)
86 {
87   *atomic = newval;
88   __sync_synchronize ();
89 }
90
91 #define __G_ATOMIC_C__
92 #include "galiasdef.c"