2 * Many similar implementations exist. See for example libwsbm
3 * or the linux kernel include/atomic.h
5 * No copyright claimed on this file.
9 #include "no_extern_c.h"
16 /* Favor OS-provided implementations.
18 * Where no OS-provided implementation is available, fall back to
19 * locally coded assembly, compiler intrinsic or ultimately a
20 * mutex-based implementation.
23 #define PIPE_ATOMIC_OS_SOLARIS
24 #elif defined(_MSC_VER)
25 #define PIPE_ATOMIC_MSVC_INTRINSIC
26 #elif defined(__GNUC__)
27 #define PIPE_ATOMIC_GCC_INTRINSIC
29 #error "Unsupported platform"
33 /* Implementation using GCC-provided synchronization intrinsics
35 #if defined(PIPE_ATOMIC_GCC_INTRINSIC)
37 #define PIPE_ATOMIC "GCC Sync Intrinsics"
39 #define p_atomic_set(_v, _i) (*(_v) = (_i))
40 #define p_atomic_read(_v) (*(_v))
41 #define p_atomic_dec_zero(v) (__sync_sub_and_fetch((v), 1) == 0)
42 #define p_atomic_inc(v) (void) __sync_add_and_fetch((v), 1)
43 #define p_atomic_dec(v) (void) __sync_sub_and_fetch((v), 1)
44 #define p_atomic_add(v, i) (void) __sync_add_and_fetch((v), (i))
45 #define p_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
46 #define p_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
47 #define p_atomic_cmpxchg(v, old, _new) \
48 __sync_val_compare_and_swap((v), (old), (_new))
54 /* Unlocked version for single threaded environments, such as some
55 * windows kernel modules.
57 #if defined(PIPE_ATOMIC_OS_UNLOCKED)
59 #define PIPE_ATOMIC "Unlocked"
61 #define p_atomic_set(_v, _i) (*(_v) = (_i))
62 #define p_atomic_read(_v) (*(_v))
63 #define p_atomic_dec_zero(_v) (p_atomic_dec_return(_v) == 0)
64 #define p_atomic_inc(_v) ((void) p_atomic_inc_return(_v))
65 #define p_atomic_dec(_v) ((void) p_atomic_dec_return(_v))
66 #define p_atomic_add(_v, _i) (*(_v) = *(_v) + (_i))
67 #define p_atomic_inc_return(_v) (++(*(_v)))
68 #define p_atomic_dec_return(_v) (--(*(_v)))
69 #define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v))
74 #if defined(PIPE_ATOMIC_MSVC_INTRINSIC)
76 #define PIPE_ATOMIC "MSVC Intrinsics"
78 /* We use the Windows header's Interlocked*64 functions instead of the
79 * _Interlocked*64 intrinsics wherever we can, as support for the latter varies
80 * with target CPU, whereas Windows headers take care of all portability
81 * issues: using intrinsics where available, falling back to library
82 * implementations where not.
84 #ifndef WIN32_LEAN_AND_MEAN
85 #define WIN32_LEAN_AND_MEAN 1
93 /* Implement _InterlockedCompareExchange8 in terms of _InterlockedCompareExchange16 */
95 _InterlockedCompareExchange8(char volatile *destination8
, char exchange8
, char comparand8
)
97 INT_PTR destinationAddr
= (INT_PTR
)destination8
;
98 short volatile *destination16
= (short volatile *)(destinationAddr
& ~1);
99 const short shift8
= (destinationAddr
& 1) * 8;
100 const short mask8
= 0xff << shift8
;
101 short initial16
= *destination16
;
102 char initial8
= initial16
>> shift8
;
103 while (initial8
== comparand8
) {
104 /* initial *destination8 matches, so try exchange it while keeping the
105 * neighboring byte untouched */
106 short exchange16
= (initial16
& ~mask8
) | ((short)exchange8
<< shift8
);
107 short comparand16
= initial16
;
108 short initial16
= _InterlockedCompareExchange16(destination16
, exchange16
, comparand16
);
109 if (initial16
== comparand16
) {
113 /* something changed, retry with the new initial value */
114 initial8
= initial16
>> shift8
;
119 /* Implement _InterlockedExchangeAdd16 in terms of _InterlockedCompareExchange16 */
120 static __inline
short
121 _InterlockedExchangeAdd16(short volatile *addend
, short value
)
123 short initial
= *addend
;
126 short exchange
= initial
+ value
;
128 /* if *addend==comparand then *addend=exchange, return original *addend */
129 initial
= _InterlockedCompareExchange16(addend
, exchange
, comparand
);
130 } while(initial
!= comparand
);
134 /* Implement _InterlockedExchangeAdd8 in terms of _InterlockedCompareExchange8 */
136 _InterlockedExchangeAdd8(char volatile *addend
, char value
)
138 char initial
= *addend
;
141 char exchange
= initial
+ value
;
143 initial
= _InterlockedCompareExchange8(addend
, exchange
, comparand
);
144 } while(initial
!= comparand
);
148 #endif /* _MSC_VER < 1600 */
150 /* MSVC supports decltype keyword, but it's only supported on C++ and doesn't
151 * quite work here; and if a C++-only solution is worthwhile, then it would be
152 * better to use templates / function overloading, instead of decltype magic.
153 * Therefore, we rely on implicit casting to LONGLONG for the functions that return
156 #define p_atomic_set(_v, _i) (*(_v) = (_i))
157 #define p_atomic_read(_v) (*(_v))
159 #define p_atomic_dec_zero(_v) \
160 (p_atomic_dec_return(_v) == 0)
162 #define p_atomic_inc(_v) \
163 ((void) p_atomic_inc_return(_v))
165 #define p_atomic_inc_return(_v) (\
166 sizeof *(_v) == sizeof(short) ? _InterlockedIncrement16((short *) (_v)) : \
167 sizeof *(_v) == sizeof(long) ? _InterlockedIncrement ((long *) (_v)) : \
168 sizeof *(_v) == sizeof(__int64) ? InterlockedIncrement64 ((__int64 *)(_v)) : \
169 (assert(!"should not get here"), 0))
171 #define p_atomic_dec(_v) \
172 ((void) p_atomic_dec_return(_v))
174 #define p_atomic_dec_return(_v) (\
175 sizeof *(_v) == sizeof(short) ? _InterlockedDecrement16((short *) (_v)) : \
176 sizeof *(_v) == sizeof(long) ? _InterlockedDecrement ((long *) (_v)) : \
177 sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \
178 (assert(!"should not get here"), 0))
180 #define p_atomic_add(_v, _i) (\
181 sizeof *(_v) == sizeof(char) ? _InterlockedExchangeAdd8 ((char *) (_v), (_i)) : \
182 sizeof *(_v) == sizeof(short) ? _InterlockedExchangeAdd16((short *) (_v), (_i)) : \
183 sizeof *(_v) == sizeof(long) ? _InterlockedExchangeAdd ((long *) (_v), (_i)) : \
184 sizeof *(_v) == sizeof(__int64) ? InterlockedExchangeAdd64((__int64 *)(_v), (_i)) : \
185 (assert(!"should not get here"), 0))
187 #define p_atomic_cmpxchg(_v, _old, _new) (\
188 sizeof *(_v) == sizeof(char) ? _InterlockedCompareExchange8 ((char *) (_v), (char) (_new), (char) (_old)) : \
189 sizeof *(_v) == sizeof(short) ? _InterlockedCompareExchange16((short *) (_v), (short) (_new), (short) (_old)) : \
190 sizeof *(_v) == sizeof(long) ? _InterlockedCompareExchange ((long *) (_v), (long) (_new), (long) (_old)) : \
191 sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \
192 (assert(!"should not get here"), 0))
196 #if defined(PIPE_ATOMIC_OS_SOLARIS)
198 #define PIPE_ATOMIC "Solaris OS atomic functions"
203 #define p_atomic_set(_v, _i) (*(_v) = (_i))
204 #define p_atomic_read(_v) (*(_v))
206 #define p_atomic_dec_zero(v) (\
207 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) == 0 : \
208 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \
209 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \
210 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \
211 (assert(!"should not get here"), 0))
213 #define p_atomic_inc(v) (void) (\
214 sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8 ((uint8_t *)(v)) : \
215 sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \
216 sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \
217 sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \
218 (assert(!"should not get here"), 0))
220 #define p_atomic_inc_return(v) ((__typeof(*v)) \
221 sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8_nv ((uint8_t *)(v)) : \
222 sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \
223 sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \
224 sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \
225 (assert(!"should not get here"), 0))
227 #define p_atomic_dec(v) ((void) \
228 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8 ((uint8_t *)(v)) : \
229 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \
230 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \
231 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \
232 (assert(!"should not get here"), 0))
234 #define p_atomic_dec_return(v) ((__typeof(*v)) \
235 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) : \
236 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \
237 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \
238 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \
239 (assert(!"should not get here"), 0))
241 #define p_atomic_add(v, i) ((void) \
242 sizeof(*v) == sizeof(uint8_t) ? atomic_add_8 ((uint8_t *)(v), (i)) : \
243 sizeof(*v) == sizeof(uint16_t) ? atomic_add_16((uint16_t *)(v), (i)) : \
244 sizeof(*v) == sizeof(uint32_t) ? atomic_add_32((uint32_t *)(v), (i)) : \
245 sizeof(*v) == sizeof(uint64_t) ? atomic_add_64((uint64_t *)(v), (i)) : \
246 (assert(!"should not get here"), 0))
248 #define p_atomic_cmpxchg(v, old, _new) ((__typeof(*v)) \
249 sizeof(*v) == sizeof(uint8_t) ? atomic_cas_8 ((uint8_t *)(v), (uint8_t )(old), (uint8_t )(_new)) : \
250 sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \
251 sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new)) : \
252 sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \
253 (assert(!"should not get here"), 0))
258 #error "No pipe_atomic implementation selected"
263 #endif /* U_ATOMIC_H */