]> code.delx.au - pulseaudio/blob - src/pulsecore/atomic.h
if available, use native gcc atomicity builtins
[pulseaudio] / src / pulsecore / atomic.h
1 #ifndef foopulseatomichfoo
2 #define foopulseatomichfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 Copyright 2006 Lennart Poettering
10
11 PulseAudio is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 PulseAudio is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with PulseAudio; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 USA.
25 ***/
26
27 /*
28 * atomic_ops guarantees us that sizeof(AO_t) == sizeof(void*). It is
29 * not guaranteed however, that sizeof(AO_t) == sizeof(size_t).
30 * however very likely.
31 *
32 * For now we do only full memory barriers. Eventually we might want
33 * to support more elaborate memory barriers, in which case we will add
34 * suffixes to the function names.
35 *
36 * On gcc >= 4.1 we use the builtin atomic functions. otherwise we use
37 * libatomic_ops
38 */
39
40 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
41
42 /* gcc based implementation */
43
44 typedef struct pa_atomic {
45 volatile int value;
46 } pa_atomic_t;
47
48 #define PA_ATOMIC_INIT(v) { .value = (v) }
49
50 static inline int pa_atomic_load(const pa_atomic_t *a) {
51 __sync_synchronize();
52 return a->value;
53 }
54
55 static inline void pa_atomic_store(pa_atomic_t *a, int i) {
56 a->value = i;
57 __sync_synchronize();
58 }
59
60 /* Returns the previously set value */
61 static inline int pa_atomic_add(pa_atomic_t *a, int i) {
62 return __sync_fetch_and_add(&a->value, i);
63 }
64
65 /* Returns the previously set value */
66 static inline int pa_atomic_sub(pa_atomic_t *a, int i) {
67 return __sync_fetch_and_sub(&a->value, i);
68 }
69
70 /* Returns the previously set value */
71 static inline int pa_atomic_inc(pa_atomic_t *a) {
72 return pa_atomic_add(a, 1);
73 }
74
75 /* Returns the previously set value */
76 static inline int pa_atomic_dec(pa_atomic_t *a) {
77 return pa_atomic_sub(a, 1);
78 }
79
80 /* Returns non-zero when the operation was successful. */
81 static inline int pa_atomic_cmpxchg(pa_atomic_t *a, int old_i, int new_i) {
82 return __sync_bool_compare_and_swap(&a->value, old_i, new_i);
83 }
84
85 typedef struct pa_atomic_ptr {
86 volatile long value;
87 } pa_atomic_ptr_t;
88
89 #define PA_ATOMIC_PTR_INIT(v) { .value = (long) (v) }
90
91 static inline void* pa_atomic_ptr_load(const pa_atomic_ptr_t *a) {
92 __sync_synchronize();
93 return (void*) a->value;
94 }
95
96 static inline void pa_atomic_ptr_store(pa_atomic_ptr_t *a, void *p) {
97 a->value = (long) p;
98 __sync_synchronize();
99 }
100
101 static inline int pa_atomic_ptr_cmpxchg(pa_atomic_ptr_t *a, void *old_p, void* new_p) {
102 return __sync_bool_compare_and_swap(&a->value, (long) old_p, (long) new_p);
103 }
104
105 #else
106
107 /* libatomic_ops based implementation */
108
109 #include <atomic_ops.h>
110
111 typedef struct pa_atomic {
112 volatile AO_t value;
113 } pa_atomic_t;
114
115 #define PA_ATOMIC_INIT(v) { .value = (v) }
116
117 static inline int pa_atomic_load(const pa_atomic_t *a) {
118 return (int) AO_load_full((AO_t*) &a->value);
119 }
120
121 static inline void pa_atomic_store(pa_atomic_t *a, int i) {
122 AO_store_full(&a->value, (AO_t) i);
123 }
124
125 static inline int pa_atomic_add(pa_atomic_t *a, int i) {
126 return AO_fetch_and_add_full(&a->value, (AO_t) i);
127 }
128
129 static inline int pa_atomic_sub(pa_atomic_t *a, int i) {
130 return AO_fetch_and_add_full(&a->value, (AO_t) -i);
131 }
132
133 static inline int pa_atomic_inc(pa_atomic_t *a) {
134 return AO_fetch_and_add1_full(&a->value);
135 }
136
137 static inline int pa_atomic_dec(pa_atomic_t *a) {
138 return AO_fetch_and_sub1_full(&a->value);
139 }
140
141 static inline int pa_atomic_cmpxchg(pa_atomic_t *a, int old_i, int new_i) {
142 return AO_compare_and_swap_full(&a->value, old_i, new_i);
143 }
144
145 typedef struct pa_atomic_ptr {
146 volatile AO_t value;
147 } pa_atomic_ptr_t;
148
149 #define PA_ATOMIC_PTR_INIT(v) { .value = (AO_t) (v) }
150
151 static inline void* pa_atomic_ptr_load(const pa_atomic_ptr_t *a) {
152 return (void*) AO_load_full((AO_t*) &a->value);
153 }
154
155 static inline void pa_atomic_ptr_store(pa_atomic_ptr_t *a, void *p) {
156 AO_store_full(&a->value, (AO_t) p);
157 }
158
159 static inline int pa_atomic_ptr_cmpxchg(pa_atomic_ptr_t *a, void *old_p, void* new_p) {
160 return AO_compare_and_swap_full(&a->value, (AO_t) old_p, (AO_t) new_p);
161 }
162
163 #endif
164
165 #endif