1 /**
2  * Many similar implementations exist. See for example libwsbm
3  * or the linux kernel include/atomic.h
4  *
5  * No copyright claimed on this file.
6  *
7  */
8 
9 #include "no_extern_c.h"
10 
11 #ifndef U_ATOMIC_H
12 #define U_ATOMIC_H
13 
14 #include <stdbool.h>
15 #include <stdint.h>
16 
17 /* Favor OS-provided implementations.
18  *
19  * Where no OS-provided implementation is available, fall back to
20  * locally coded assembly, compiler intrinsic or ultimately a
21  * mutex-based implementation.
22  */
23 #if defined(__sun)
24 #define PIPE_ATOMIC_OS_SOLARIS
25 #elif defined(_MSC_VER)
26 #define PIPE_ATOMIC_MSVC_INTRINSIC
27 #elif defined(__GNUC__)
28 #define PIPE_ATOMIC_GCC_INTRINSIC
29 #else
30 #error "Unsupported platform"
31 #endif
32 
33 
34 /* Implementation using GCC-provided synchronization intrinsics
35  */
36 #if defined(PIPE_ATOMIC_GCC_INTRINSIC)
37 
38 #define PIPE_ATOMIC "GCC Sync Intrinsics"
39 
40 #if defined(USE_GCC_ATOMIC_BUILTINS)
41 
42 /* The builtins with explicit memory model are available since GCC 4.7. */
43 #define p_atomic_set(_v, _i) __atomic_store_n((_v), (_i), __ATOMIC_RELEASE)
44 #define p_atomic_read(_v) __atomic_load_n((_v), __ATOMIC_ACQUIRE)
45 #define p_atomic_read_relaxed(_v) __atomic_load_n((_v), __ATOMIC_RELAXED)
46 #define p_atomic_dec_zero(v) (__atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL) == 0)
47 #define p_atomic_inc(v) (void) __atomic_add_fetch((v), 1, __ATOMIC_ACQ_REL)
48 #define p_atomic_dec(v) (void) __atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL)
49 #define p_atomic_add(v, i) (void) __atomic_add_fetch((v), (i), __ATOMIC_ACQ_REL)
50 #define p_atomic_inc_return(v) __atomic_add_fetch((v), 1, __ATOMIC_ACQ_REL)
51 #define p_atomic_dec_return(v) __atomic_sub_fetch((v), 1, __ATOMIC_ACQ_REL)
52 #define p_atomic_add_return(v, i) __atomic_add_fetch((v), (i), __ATOMIC_ACQ_REL)
53 #define p_atomic_xchg(v, i) __atomic_exchange_n((v), (i), __ATOMIC_ACQ_REL)
54 #define PIPE_NATIVE_ATOMIC_XCHG
55 
56 #else
57 
58 #define p_atomic_set(_v, _i) (*(_v) = (_i))
59 #define p_atomic_read(_v) (*(_v))
60 #define p_atomic_read_relaxed(_v) (*(_v))
61 #define p_atomic_dec_zero(v) (__sync_sub_and_fetch((v), 1) == 0)
62 #define p_atomic_inc(v) (void) __sync_add_and_fetch((v), 1)
63 #define p_atomic_dec(v) (void) __sync_sub_and_fetch((v), 1)
64 #define p_atomic_add(v, i) (void) __sync_add_and_fetch((v), (i))
65 #define p_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
66 #define p_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
67 #define p_atomic_add_return(v, i) __sync_add_and_fetch((v), (i))
68 
69 #endif
70 
71 /* There is no __atomic_* compare and exchange that returns the current value.
72  * Also, GCC 5.4 seems unable to optimize a compound statement expression that
73  * uses an additional stack variable with __atomic_compare_exchange[_n].
74  */
75 #define p_atomic_cmpxchg(v, old, _new) \
76    __sync_val_compare_and_swap((v), (old), (_new))
77 
78 #endif
79 
80 
81 
82 /* Unlocked version for single threaded environments, such as some
83  * windows kernel modules.
84  */
85 #if defined(PIPE_ATOMIC_OS_UNLOCKED)
86 
87 #define PIPE_ATOMIC "Unlocked"
88 
89 #define p_atomic_set(_v, _i) (*(_v) = (_i))
90 #define p_atomic_read(_v) (*(_v))
91 #define p_atomic_read_relaxed(_v) (*(_v))
92 #define p_atomic_dec_zero(_v) (p_atomic_dec_return(_v) == 0)
93 #define p_atomic_inc(_v) ((void) p_atomic_inc_return(_v))
94 #define p_atomic_dec(_v) ((void) p_atomic_dec_return(_v))
95 #define p_atomic_add(_v, _i) ((void) p_atomic_add_return((_v), (_i)))
96 #define p_atomic_inc_return(_v) (++(*(_v)))
97 #define p_atomic_dec_return(_v) (--(*(_v)))
98 #define p_atomic_add_return(_v, _i) (*(_v) = *(_v) + (_i))
99 #define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v))
100 
101 #endif
102 
103 
104 #if defined(PIPE_ATOMIC_MSVC_INTRINSIC)
105 
106 #define PIPE_ATOMIC "MSVC Intrinsics"
107 
108 /* We use the Windows header's Interlocked*64 functions instead of the
109  * _Interlocked*64 intrinsics wherever we can, as support for the latter varies
110  * with target CPU, whereas Windows headers take care of all portability
111  * issues: using intrinsics where available, falling back to library
112  * implementations where not.
113  */
114 #ifndef WIN32_LEAN_AND_MEAN
115 #define WIN32_LEAN_AND_MEAN 1
116 #endif
117 #include <windows.h>
118 #include <intrin.h>
119 #include <assert.h>
120 
121 /* MSVC supports decltype keyword, but it's only supported on C++ and doesn't
122  * quite work here; and if a C++-only solution is worthwhile, then it would be
123  * better to use templates / function overloading, instead of decltype magic.
124  * Therefore, we rely on implicit casting to LONGLONG for the functions that return
125  */
126 
127 #define p_atomic_set(_v, _i) (*(_v) = (_i))
128 #define p_atomic_read(_v) (*(_v))
129 #define p_atomic_read_relaxed(_v) (*(_v))
130 
131 #define p_atomic_dec_zero(_v) \
132    (p_atomic_dec_return(_v) == 0)
133 
134 #define p_atomic_inc(_v) \
135    ((void) p_atomic_inc_return(_v))
136 
137 #define p_atomic_inc_return(_v) (\
138    sizeof *(_v) == sizeof(short)   ? _InterlockedIncrement16((short *)  (_v)) : \
139    sizeof *(_v) == sizeof(long)    ? _InterlockedIncrement  ((long *)   (_v)) : \
140    sizeof *(_v) == sizeof(__int64) ? InterlockedIncrement64 ((__int64 *)(_v)) : \
141                                      (assert(!"should not get here"), 0))
142 
143 #define p_atomic_dec(_v) \
144    ((void) p_atomic_dec_return(_v))
145 
146 #define p_atomic_dec_return(_v) (\
147    sizeof *(_v) == sizeof(short)   ? _InterlockedDecrement16((short *)  (_v)) : \
148    sizeof *(_v) == sizeof(long)    ? _InterlockedDecrement  ((long *)   (_v)) : \
149    sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \
150                                      (assert(!"should not get here"), 0))
151 
152 #define p_atomic_add(_v, _i) \
153    ((void) p_atomic_add_return((_v), (_i)))
154 
155 #define p_atomic_add_return(_v, _i) (\
156    sizeof *(_v) == sizeof(char)    ? _InterlockedExchangeAdd8 ((char *)   (_v), (_i)) : \
157    sizeof *(_v) == sizeof(short)   ? _InterlockedExchangeAdd16((short *)  (_v), (_i)) : \
158    sizeof *(_v) == sizeof(long)    ? _InterlockedExchangeAdd  ((long *)   (_v), (_i)) : \
159    sizeof *(_v) == sizeof(__int64) ? InterlockedExchangeAdd64((__int64 *)(_v), (_i)) : \
160                                      (assert(!"should not get here"), 0))
161 
162 #define p_atomic_cmpxchg(_v, _old, _new) (\
163    sizeof *(_v) == sizeof(char)    ? _InterlockedCompareExchange8 ((char *)   (_v), (char)   (_new), (char)   (_old)) : \
164    sizeof *(_v) == sizeof(short)   ? _InterlockedCompareExchange16((short *)  (_v), (short)  (_new), (short)  (_old)) : \
165    sizeof *(_v) == sizeof(long)    ? _InterlockedCompareExchange  ((long *)   (_v), (long)   (_new), (long)   (_old)) : \
166    sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \
167                                      (assert(!"should not get here"), 0))
168 
169 #endif
170 
171 #if defined(PIPE_ATOMIC_OS_SOLARIS)
172 
173 #define PIPE_ATOMIC "Solaris OS atomic functions"
174 
175 #include <atomic.h>
176 #include <assert.h>
177 
178 #define p_atomic_set(_v, _i) (*(_v) = (_i))
179 #define p_atomic_read(_v) (*(_v))
180 
181 #define p_atomic_dec_zero(v) (\
182    sizeof(*v) == sizeof(uint8_t)  ? atomic_dec_8_nv ((uint8_t  *)(v)) == 0 : \
183    sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \
184    sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \
185    sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \
186                                     (assert(!"should not get here"), 0))
187 
188 #define p_atomic_inc(v) (void) (\
189    sizeof(*v) == sizeof(uint8_t)  ? atomic_inc_8 ((uint8_t  *)(v)) : \
190    sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \
191    sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \
192    sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \
193                                     (assert(!"should not get here"), 0))
194 
195 #define p_atomic_inc_return(v) (__typeof(*v))( \
196    sizeof(*v) == sizeof(uint8_t)  ? atomic_inc_8_nv ((uint8_t  *)(v)) : \
197    sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \
198    sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \
199    sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \
200                                     (assert(!"should not get here"), 0))
201 
202 #define p_atomic_dec(v) (void) ( \
203    sizeof(*v) == sizeof(uint8_t)  ? atomic_dec_8 ((uint8_t  *)(v)) : \
204    sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \
205    sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \
206    sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \
207                                     (assert(!"should not get here"), 0))
208 
209 #define p_atomic_dec_return(v) (__typeof(*v))( \
210    sizeof(*v) == sizeof(uint8_t)  ? atomic_dec_8_nv ((uint8_t  *)(v)) : \
211    sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \
212    sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \
213    sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \
214                                     (assert(!"should not get here"), 0))
215 
216 #define p_atomic_add(v, i) (void) ( \
217    sizeof(*v) == sizeof(uint8_t)  ? atomic_add_8 ((uint8_t  *)(v), (i)) : \
218    sizeof(*v) == sizeof(uint16_t) ? atomic_add_16((uint16_t *)(v), (i)) : \
219    sizeof(*v) == sizeof(uint32_t) ? atomic_add_32((uint32_t *)(v), (i)) : \
220    sizeof(*v) == sizeof(uint64_t) ? atomic_add_64((uint64_t *)(v), (i)) : \
221                                     (assert(!"should not get here"), 0))
222 
223 #define p_atomic_add_return(v, i) (__typeof(*v)) ( \
224    sizeof(*v) == sizeof(uint8_t)  ? atomic_add_8_nv ((uint8_t  *)(v), (i)) : \
225    sizeof(*v) == sizeof(uint16_t) ? atomic_add_16_nv((uint16_t *)(v), (i)) : \
226    sizeof(*v) == sizeof(uint32_t) ? atomic_add_32_nv((uint32_t *)(v), (i)) : \
227    sizeof(*v) == sizeof(uint64_t) ? atomic_add_64_nv((uint64_t *)(v), (i)) : \
228                                     (assert(!"should not get here"), 0))
229 
230 #define p_atomic_cmpxchg(v, old, _new) (__typeof(*v))( \
231    sizeof(*v) == sizeof(uint8_t)  ? atomic_cas_8 ((uint8_t  *)(v), (uint8_t )(old), (uint8_t )(_new)) : \
232    sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \
233    sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new)) : \
234    sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \
235                                     (assert(!"should not get here"), 0))
236 
237 #endif
238 
239 #ifndef PIPE_ATOMIC
240 #error "No pipe_atomic implementation selected"
241 #endif
242 
243 #ifndef PIPE_NATIVE_ATOMIC_XCHG
p_atomic_xchg_32(uint32_t * v,uint32_t i)244 static inline uint32_t p_atomic_xchg_32(uint32_t *v, uint32_t i)
245 {
246    uint32_t actual = p_atomic_read(v);
247    uint32_t expected;
248    do {
249       expected = actual;
250       actual = p_atomic_cmpxchg(v, expected, i);
251    } while (expected != actual);
252    return actual;
253 }
254 
p_atomic_xchg_64(uint64_t * v,uint64_t i)255 static inline uint64_t p_atomic_xchg_64(uint64_t *v, uint64_t i)
256 {
257    uint64_t actual = p_atomic_read(v);
258    uint64_t expected;
259    do {
260       expected = actual;
261       actual = p_atomic_cmpxchg(v, expected, i);
262    } while (expected != actual);
263    return actual;
264 }
265 
266 #define p_atomic_xchg(v, i) (__typeof(*(v)))( \
267    sizeof(*(v)) == sizeof(uint32_t) ? p_atomic_xchg_32((uint32_t *)(v), (uint32_t)(i)) : \
268    sizeof(*(v)) == sizeof(uint64_t) ? p_atomic_xchg_64((uint64_t *)(v), (uint64_t)(i)) : \
269                                       (assert(!"should not get here"), 0))
270 #endif
271 
272 #endif /* U_ATOMIC_H */
273