util/u_atomic: Use _Interlocked* intrinsics for non 64bits.
[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 #ifndef U_ATOMIC_H
10 #define U_ATOMIC_H
11
12 #include <stdbool.h>
13
14 /* Favor OS-provided implementations.
15 *
16 * Where no OS-provided implementation is available, fall back to
17 * locally coded assembly, compiler intrinsic or ultimately a
18 * mutex-based implementation.
19 */
20 #if defined(__sun)
21 #define PIPE_ATOMIC_OS_SOLARIS
22 #elif defined(_MSC_VER)
23 #define PIPE_ATOMIC_MSVC_INTRINSIC
24 #elif defined(__GNUC__)
25 #define PIPE_ATOMIC_GCC_INTRINSIC
26 #else
27 #error "Unsupported platform"
28 #endif
29
30
31 /* Implementation using GCC-provided synchronization intrinsics
32 */
33 #if defined(PIPE_ATOMIC_GCC_INTRINSIC)
34
35 #define PIPE_ATOMIC "GCC Sync Intrinsics"
36
37 #define p_atomic_set(_v, _i) (*(_v) = (_i))
38 #define p_atomic_read(_v) (*(_v))
39 #define p_atomic_dec_zero(v) (__sync_sub_and_fetch((v), 1) == 0)
40 #define p_atomic_inc(v) (void) __sync_add_and_fetch((v), 1)
41 #define p_atomic_dec(v) (void) __sync_sub_and_fetch((v), 1)
42 #define p_atomic_inc_return(v) __sync_add_and_fetch((v), 1)
43 #define p_atomic_dec_return(v) __sync_sub_and_fetch((v), 1)
44 #define p_atomic_cmpxchg(v, old, _new) \
45 __sync_val_compare_and_swap((v), (old), (_new))
46
47 #endif
48
49
50
51 /* Unlocked version for single threaded environments, such as some
52 * windows kernel modules.
53 */
54 #if defined(PIPE_ATOMIC_OS_UNLOCKED)
55
56 #define PIPE_ATOMIC "Unlocked"
57
58 #define p_atomic_set(_v, _i) (*(_v) = (_i))
59 #define p_atomic_read(_v) (*(_v))
60 #define p_atomic_dec_zero(_v) (p_atomic_dec_return(_v) == 0)
61 #define p_atomic_inc(_v) ((void) p_atomic_inc_return(_v))
62 #define p_atomic_dec(_v) ((void) p_atomic_dec_return(_v))
63 #define p_atomic_inc_return(_v) (++(*(_v)))
64 #define p_atomic_dec_return(_v) (--(*(_v)))
65 #define p_atomic_cmpxchg(_v, _old, _new) (*(_v) == (_old) ? (*(_v) = (_new), (_old)) : *(_v))
66
67 #endif
68
69
70 #if defined(PIPE_ATOMIC_MSVC_INTRINSIC)
71
72 #define PIPE_ATOMIC "MSVC Intrinsics"
73
74 /* We use the Windows header's Interlocked*64 functions instead of the
75 * _Interlocked*64 intrinsics wherever we can, as support for the latter varies
76 * with target CPU, whereas Windows headers take care of all portability
77 * issues: using intrinsics where available, falling back to library
78 * implementations where not.
79 */
80 #ifndef WIN32_LEAN_AND_MEAN
81 #define WIN32_LEAN_AND_MEAN 1
82 #endif
83 #include <windows.h>
84 #include <intrin.h>
85 #include <assert.h>
86
87 #pragma intrinsic(_InterlockedCompareExchange8)
88
89 /* MSVC supports decltype keyword, but it's only supported on C++ and doesn't
90 * quite work here; and if a C++-only solution is worthwhile, then it would be
91 * better to use templates / function overloading, instead of decltype magic.
92 * Therefore, we rely on implicit casting to LONGLONG for the functions that return
93 */
94
95 #define p_atomic_set(_v, _i) (*(_v) = (_i))
96 #define p_atomic_read(_v) (*(_v))
97
98 #define p_atomic_dec_zero(_v) \
99 (p_atomic_dec_return(_v) == 0)
100
101 #define p_atomic_inc(_v) \
102 ((void) p_atomic_inc_return(_v))
103
104 #define p_atomic_inc_return(_v) (\
105 sizeof *(_v) == sizeof(short) ? _InterlockedIncrement16((short *) (_v)) : \
106 sizeof *(_v) == sizeof(long) ? _InterlockedIncrement ((long *) (_v)) : \
107 sizeof *(_v) == sizeof(__int64) ? InterlockedIncrement64 ((__int64 *)(_v)) : \
108 (assert(!"should not get here"), 0))
109
110 #define p_atomic_dec(_v) \
111 ((void) p_atomic_dec_return(_v))
112
113 #define p_atomic_dec_return(_v) (\
114 sizeof *(_v) == sizeof(short) ? _InterlockedDecrement16((short *) (_v)) : \
115 sizeof *(_v) == sizeof(long) ? _InterlockedDecrement ((long *) (_v)) : \
116 sizeof *(_v) == sizeof(__int64) ? InterlockedDecrement64 ((__int64 *)(_v)) : \
117 (assert(!"should not get here"), 0))
118
119 #define p_atomic_cmpxchg(_v, _old, _new) (\
120 sizeof *(_v) == sizeof(char) ? _InterlockedCompareExchange8 ((char *) (_v), (char) (_new), (char) (_old)) : \
121 sizeof *(_v) == sizeof(short) ? _InterlockedCompareExchange16((short *) (_v), (short) (_new), (short) (_old)) : \
122 sizeof *(_v) == sizeof(long) ? _InterlockedCompareExchange ((long *) (_v), (long) (_new), (long) (_old)) : \
123 sizeof *(_v) == sizeof(__int64) ? InterlockedCompareExchange64 ((__int64 *)(_v), (__int64)(_new), (__int64)(_old)) : \
124 (assert(!"should not get here"), 0))
125
126 #endif
127
128 #if defined(PIPE_ATOMIC_OS_SOLARIS)
129
130 #define PIPE_ATOMIC "Solaris OS atomic functions"
131
132 #include <atomic.h>
133 #include <assert.h>
134
135 #define p_atomic_set(_v, _i) (*(_v) = (_i))
136 #define p_atomic_read(_v) (*(_v))
137
138 #define p_atomic_dec_zero(v) (\
139 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) == 0 : \
140 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) == 0 : \
141 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) == 0 : \
142 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) == 0 : \
143 (assert(!"should not get here"), 0))
144
145 #define p_atomic_inc(v) (void) (\
146 sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8 ((uint8_t *)(v)) : \
147 sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16((uint16_t *)(v)) : \
148 sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32((uint32_t *)(v)) : \
149 sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64((uint64_t *)(v)) : \
150 (assert(!"should not get here"), 0))
151
152 #define p_atomic_inc_return(v) ((typeof(*v)) \
153 sizeof(*v) == sizeof(uint8_t) ? atomic_inc_8_nv ((uint8_t *)(v)) : \
154 sizeof(*v) == sizeof(uint16_t) ? atomic_inc_16_nv((uint16_t *)(v)) : \
155 sizeof(*v) == sizeof(uint32_t) ? atomic_inc_32_nv((uint32_t *)(v)) : \
156 sizeof(*v) == sizeof(uint64_t) ? atomic_inc_64_nv((uint64_t *)(v)) : \
157 (assert(!"should not get here"), 0))
158
159 #define p_atomic_dec(v) ((void) \
160 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8 ((uint8_t *)(v)) : \
161 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16((uint16_t *)(v)) : \
162 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32((uint32_t *)(v)) : \
163 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64((uint64_t *)(v)) : \
164 (assert(!"should not get here"), 0))
165
166 #define p_atomic_dec_return(v) ((typeof(*v)) \
167 sizeof(*v) == sizeof(uint8_t) ? atomic_dec_8_nv ((uint8_t *)(v)) : \
168 sizeof(*v) == sizeof(uint16_t) ? atomic_dec_16_nv((uint16_t *)(v)) : \
169 sizeof(*v) == sizeof(uint32_t) ? atomic_dec_32_nv((uint32_t *)(v)) : \
170 sizeof(*v) == sizeof(uint64_t) ? atomic_dec_64_nv((uint64_t *)(v)) : \
171 (assert(!"should not get here"), 0))
172
173 #define p_atomic_cmpxchg(v, old, _new) ((typeof(*v)) \
174 sizeof(*v) == sizeof(uint8_t) ? atomic_cas_8 ((uint8_t *)(v), (uint8_t )(old), (uint8_t )(_new)) : \
175 sizeof(*v) == sizeof(uint16_t) ? atomic_cas_16((uint16_t *)(v), (uint16_t)(old), (uint16_t)(_new)) : \
176 sizeof(*v) == sizeof(uint32_t) ? atomic_cas_32((uint32_t *)(v), (uint32_t)(old), (uint32_t)(_new)) : \
177 sizeof(*v) == sizeof(uint64_t) ? atomic_cas_64((uint64_t *)(v), (uint64_t)(old), (uint64_t)(_new)) : \
178 (assert(!"should not get here"), 0))
179
180 #endif
181
182 #ifndef PIPE_ATOMIC
183 #error "No pipe_atomic implementation selected"
184 #endif
185
186
187
188 #endif /* U_ATOMIC_H */