util/u_atomic: Remove MSVC 2008 support.
[mesa.git] / src / util / u_atomic.h
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
16 /* Favor OS-provided implementations.
17 *
18 * Where no OS-provided implementation is available, fall back to
19 * locally coded assembly, compiler intrinsic or ultimately a
20 * mutex-based implementation.
21 */
22 #if defined(__sun)
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
28 #else
29 #error "Unsupported platform"
30 #endif
31
32
33 /* Implementation using GCC-provided synchronization intrinsics
34 */
35 #if defined(PIPE_ATOMIC_GCC_INTRINSIC)
36
37 #define PIPE_ATOMIC "GCC Sync Intrinsics"
38
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))
49
50 #endif
51
52
53
54 /* Unlocked version for single threaded environments, such as some
55 * windows kernel modules.
56 */
57 #if defined(PIPE_ATOMIC_OS_UNLOCKED)
58
59 #define PIPE_ATOMIC "Unlocked"
60
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))
70
71 #endif
72
73
74 #if defined(PIPE_ATOMIC_MSVC_INTRINSIC)
75
76 #define PIPE_ATOMIC "MSVC Intrinsics"
77
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.
83 */
84 #ifndef WIN32_LEAN_AND_MEAN
85 #define WIN32_LEAN_AND_MEAN 1
86 #endif
87 #include <windows.h>
88 #include <intrin.h>
89 #include <assert.h>
90
91 /* MSVC supports decltype keyword, but it's only supported on C++ and doesn't
92 * quite work here; and if a C++-only solution is worthwhile, then it would be
93 * better to use templates / function overloading, instead of decltype magic.
94 * Therefore, we rely on implicit casting to LONGLONG for the functions that return
95 */
96
97 #define p_atomic_set(_v, _i) (*(_v) = (_i))
98 #define p_atomic_read(_v) (*(_v))
99
100 #define p_atomic_dec_zero(_v) \
101 (p_atomic_dec_return(_v) == 0)
102
103 #define p_atomic_inc(_v) \
104 ((void) p_atomic_inc_return(_v))
105
106 #define p_atomic_inc_return(_v) (\
107 sizeof *(_v) == sizeof(short) ? _InterlockedIncrement16((short *) (_v)) : \
108 sizeof *(_v) == sizeof(long) ? _InterlockedIncrement ((long *) (_v)) : \
109 sizeof *(_v) == sizeof(__int64) ? InterlockedIncrement64 ((__int64 *)(_v)) : \
110 (assert(!"should not get here"), 0))
111
112 #define p_atomic_dec(_v) \
113 ((void) p_atomic_dec_return(_v))
114
115 #define p_atomic_dec_return(_v) (\
116 sizeof *(_v) == sizeof(short) ? _InterlockedDecrement16((short *) (_v)) : \
117 sizeof *(_v) == sizeof(long) ? _InterlockedDecrement ((long *) (_v)) : \
118 sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \
119 (assert(!"should not get here"), 0))
120
121 #define p_atomic_add(_v, _i) (\
122 sizeof *(_v) == sizeof(char) ? _InterlockedExchangeAdd8 ((char *) (_v), (_i)) : \
123 sizeof *(_v) == sizeof(short) ? _InterlockedExchangeAdd16((short *) (_v), (_i)) : \
124 sizeof *(_v) == sizeof(long) ? _InterlockedExchangeAdd ((long *) (_v), (_i)) : \
125 sizeof *(_v) == sizeof(__int64) ? InterlockedExchangeAdd64((__int64 *)(_v), (_i)) : \
126 (assert(!"should not get here"), 0))
127
128 #define p_atomic_cmpxchg(_v, _old, _new) (\
129 sizeof *(_v) == sizeof(char) ? _InterlockedCompareExchange8 ((char *) (_v), (char) (_new), (char) (_old)) : \
130 sizeof *(_v) == sizeof(short) ? _InterlockedCompareExchange16((short *) (_v), (short) (_new), (short) (_old)) : \
131 sizeof *(_v) == sizeof(long) ? _InterlockedCompareExchange ((long *) (_v), (long) (_new), (long) (_old)) : \
132 sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \
133 (assert(!"should not get here"), 0))
134
135 #endif
136
137 #if defined(PIPE_ATOMIC_OS_SOLARIS)
138
139 #define PIPE_ATOMIC "Solaris OS atomic functions"
140
141 #include <atomic.h>
142 #include <assert.h>
143
144 #define p_atomic_set(_v, _i) (*(_v) = (_i))
145 #define p_atomic_read(_v) (*(_v))
146
147 #define p_atomic_dec_zero(v) (\
148 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) == 0 : \
149 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \
150 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \
151 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \
152 (assert(!"should not get here"), 0))
153
154 #define p_atomic_inc(v) (void) (\
155 sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8 ((uint8_t *)(v)) : \
156 sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \
157 sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \
158 sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \
159 (assert(!"should not get here"), 0))
160
161 #define p_atomic_inc_return(v) ((__typeof(*v)) \
162 sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8_nv ((uint8_t *)(v)) : \
163 sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \
164 sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \
165 sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \
166 (assert(!"should not get here"), 0))
167
168 #define p_atomic_dec(v) ((void) \
169 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8 ((uint8_t *)(v)) : \
170 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \
171 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \
172 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \
173 (assert(!"should not get here"), 0))
174
175 #define p_atomic_dec_return(v) ((__typeof(*v)) \
176 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) : \
177 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \
178 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \
179 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \
180 (assert(!"should not get here"), 0))
181
182 #define p_atomic_add(v, i) ((void) \
183 sizeof(*v) == sizeof(uint8_t) ? atomic_add_8 ((uint8_t *)(v), (i)) : \
184 sizeof(*v) == sizeof(uint16_t) ? atomic_add_16((uint16_t *)(v), (i)) : \
185 sizeof(*v) == sizeof(uint32_t) ? atomic_add_32((uint32_t *)(v), (i)) : \
186 sizeof(*v) == sizeof(uint64_t) ? atomic_add_64((uint64_t *)(v), (i)) : \
187 (assert(!"should not get here"), 0))
188
189 #define p_atomic_cmpxchg(v, old, _new) ((__typeof(*v)) \
190 sizeof(*v) == sizeof(uint8_t) ? atomic_cas_8 ((uint8_t *)(v), (uint8_t )(old), (uint8_t )(_new)) : \
191 sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \
192 sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new)) : \
193 sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \
194 (assert(!"should not get here"), 0))
195
196 #endif
197
198 #ifndef PIPE_ATOMIC
199 #error "No pipe_atomic implementation selected"
200 #endif
201
202
203
204 #endif /* U_ATOMIC_H */