aboutsummaryrefslogtreecommitdiffstats
path: root/include/atomic.h
blob: 174aebe991b50727120d7e12d83b08566899fba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#ifndef AL_ATOMIC_H
#define AL_ATOMIC_H

#include "static_assert.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef void *volatile XchgPtr;

typedef unsigned int uint;
typedef union {
    uint value;
} RefCount;

#define STATIC_REFCOUNT_INIT(V)  {(V)}

#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined(__QNXNTO__)

inline void InitRef(volatile RefCount *ptr, uint value)
{ ptr->value = value; }
inline uint ReadRef(volatile RefCount *ptr)
{ __sync_synchronize(); return ptr->value; }
inline uint IncrementRef(volatile RefCount *ptr)
{ return __sync_add_and_fetch(&ptr->value, 1); }
inline uint DecrementRef(volatile RefCount *ptr)
{ return __sync_sub_and_fetch(&ptr->value, 1); }
inline uint ExchangeRef(volatile RefCount *ptr, uint newval)
{ return __sync_lock_test_and_set(&ptr->value, newval); }
inline uint CompExchangeRef(volatile RefCount *ptr, uint oldval, uint newval)
{ return __sync_val_compare_and_swap(&ptr->value, oldval, newval); }

inline int ExchangeInt(volatile int *ptr, int newval)
{ return __sync_lock_test_and_set(ptr, newval); }
inline void *ExchangePtr(XchgPtr *ptr, void *newval)
{ return __sync_lock_test_and_set(ptr, newval); }
inline int CompExchangeInt(volatile int *ptr, int oldval, int newval)
{ return __sync_val_compare_and_swap(ptr, oldval, newval); }
inline void *CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
{ return __sync_val_compare_and_swap(ptr, oldval, newval); }


#define ATOMIC(T)  struct { T volatile value; }

#define ATOMIC_INIT_STATIC(_newval) {(_newval)}

#define ATOMIC_LOAD_UNSAFE(_val)  ((_val).value)
#define ATOMIC_STORE_UNSAFE(_val, _newval)  do {  \
    (_val).value = (_newval);                     \
} while(0)

#define ATOMIC_LOAD(_val)  (__sync_synchronize(),(_val).value)
#define ATOMIC_STORE(_val, _newval)  do {  \
    (_val).value = (_newval);              \
    __sync_synchronize();                  \
} while(0)

#define ATOMIC_EXCHANGE(T, _val, _newval)  __extension__({                    \
    static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
    T _r = __sync_lock_test_and_set(&(_val).value, (_newval));                \
    _r;                                                                       \
})
#define ATOMIC_COMPARE_EXCHANGE(T, _val, _oldval, _newval) __extension__({    \
    static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
    T _r = __sync_val_compare_and_swap(&(_val).value, (_oldval), (_newval));  \
    _r;                                                                       \
})

#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))

inline uint xaddl(volatile uint *dest, int incr)
{
    unsigned int ret;
    __asm__ __volatile__("lock; xaddl %0,(%1)"
                         : "=r" (ret)
                         : "r" (dest), "0" (incr)
                         : "memory");
    return ret;
}

inline void InitRef(volatile RefCount *ptr, uint value)
{ ptr->value = value; }
inline uint ReadRef(volatile RefCount *ptr)
{ __asm__ __volatile__("" ::: "memory"); return ptr->value; }
inline uint IncrementRef(volatile RefCount *ptr)
{ return xaddl(&ptr->value, 1)+1; }
inline uint DecrementRef(volatile RefCount *ptr)
{ return xaddl(&ptr->value, -1)-1; }
inline uint ExchangeRef(volatile RefCount *ptr, uint newval)
{
    int ret;
    __asm__ __volatile__("lock; xchgl %0,(%1)"
                         : "=r" (ret)
                         : "r" (&ptr->value), "0" (newval)
                         : "memory");
    return ret;
}
inline uint CompExchangeRef(volatile RefCount *ptr, uint oldval, uint newval)
{
    int ret;
    __asm__ __volatile__("lock; cmpxchgl %2,(%1)"
                         : "=a" (ret)
                         : "r" (&ptr->value), "r" (newval), "0" (oldval)
                         : "memory");
    return ret;
}

#define EXCHANGE(S, ret, dest, newval) __asm__ __volatile__(                  \
    "lock; xchg"S" %0,(%1)"                                                   \
    : "=r" (ret)                                                              \
    : "r" (dest), "0" (newval)                                                \
    : "memory"                                                                \
)
#define COMP_EXCHANGE(S, ret, dest, oldval, newval) __asm__ __volatile__(     \
    "lock; cmpxchg"S" %2,(%1)"                                                \
    : "=a" (ret)                                                              \
    : "r" (dest), "r" (newval), "0" (oldval)                                  \
    : "memory"                                                                \
)


inline int ExchangeInt(volatile int *dest, int newval)
{
    int ret;
    EXCHANGE("l", ret, dest, newval);
    return ret;
}
inline int CompExchangeInt(volatile int *dest, int oldval, int newval)
{
    int ret;
    COMP_EXCHANGE("l", ret, dest, oldval, newval);
    return ret;
}

#ifdef __i386__
inline void *ExchangePtr(XchgPtr *dest, void *newval)
{
    void *ret;
    EXCHANGE("l", ret, dest, newval);
    return ret;
}
inline void *CompExchangePtr(XchgPtr *dest, void *oldval, void *newval)
{
    void *ret;
    COMP_EXCHANGE("l", ret, dest, oldval, newval);
    return ret;
}
#else
inline void *ExchangePtr(XchgPtr *dest, void *newval)
{
    void *ret;
    EXCHANGE("q", ret, dest, newval);
    return ret;
}
inline void *CompExchangePtr(XchgPtr *dest, void *oldval, void *newval)
{
    void *ret;
    COMP_EXCHANGE("q", ret, dest, oldval, newval);
    return ret;
}
#endif


#define ATOMIC(T)  struct { T volatile value; }

#define ATOMIC_INIT_STATIC(_newval) {(_newval)}

#define ATOMIC_LOAD_UNSAFE(_val)  ((_val).value)
#define ATOMIC_STORE_UNSAFE(_val, _newval)  do {  \
    (_val).value = (_newval);                     \
} while(0)

#define ATOMIC_LOAD(_val)  (__asm__ __volatile__("" ::: "memory"),(_val).value)
#define ATOMIC_STORE(_val, _newval)  do {  \
    (_val).value = (_newval);              \
    __asm__ __volatile__("" ::: "memory"); \
} while(0)

#define ATOMIC_EXCHANGE(T, _val, _newval)  __extension__({                    \
    T _r;                                                                     \
    static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
    if(sizeof(T) == 4) EXCHANGE("l", _r, &(_val).value, (_newval));           \
    else if(sizeof(T) == 8) EXCHANGE("q", _r, &(_val).value, (_newval));      \
    _r;                                                                       \
})
#define ATOMIC_COMPARE_EXCHANGE(T, _val, _oldval, _newval) __extension__({    \
    T _r;                                                                     \
    static_assert(sizeof(T)==sizeof((_val).value), "Type "#T" has incorrect size!"); \
    if(sizeof(T) == 4) COMP_EXCHANGE("l", _r, &(_val).value, (_oldval), (_newval)); \
    else if(sizeof(T) == 8) COMP_EXCHANGE("q", _r, &(_val).value, (_oldval), (_newval)); \
    _r;                                                                       \
})

#elif defined(_WIN32)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

static_assert(sizeof(LONG)==sizeof(uint), "sizeof LONG does not match sizeof uint");

inline void InitRef(volatile RefCount *ptr, uint value)
{ ptr->value = value; }
inline uint ReadRef(volatile RefCount *ptr)
{ _ReadBarrier(); return ptr->value; }
inline uint IncrementRef(volatile RefCount *ptr)
{
    union {
        volatile uint *u;
        volatile LONG *l;
    } u = { &ptr->value };
    return InterlockedIncrement(u.l);
}
inline uint DecrementRef(volatile RefCount *ptr)
{
    union {
        volatile uint *u;
        volatile LONG *l;
    } u = { &ptr->value };
    return InterlockedDecrement(u.l);
}
inline uint ExchangeRef(volatile RefCount *ptr, uint newval)
{
    union {
        volatile uint *i;
        volatile LONG *l;
    } u = { &ptr->value };
    return InterlockedExchange(u.l, newval);
}
inline uint CompExchangeRef(volatile RefCount *ptr, uint oldval, uint newval)
{
    union {
        volatile uint *i;
        volatile LONG *l;
    } u = { &ptr->value };
    return InterlockedCompareExchange(u.l, newval, oldval);
}

inline int ExchangeInt32(volatile int *ptr, int newval)
{
    union {
        volatile int *i;
        volatile LONG *l;
    } u = { ptr };
    return InterlockedExchange(u.l, newval);
}
inline int CompExchangeInt32(volatile int *ptr, int oldval, int newval)
{
    union {
        volatile int *i;
        volatile LONG *l;
    } u = { ptr };
    return InterlockedCompareExchange(u.l, newval, oldval);
}
inline __int64 ExchangeInt64(volatile __int64 *ptr, __int64 newval)
{
    union {
        volatile __int64 *i;
        volatile LONGLONG *l;
    } u = { ptr };
    return InterlockedExchange64(u.l, newval);
}
inline __int64 CompExchangeInt64(volatile __int64 *ptr, __int64 oldval, __int64 newval)
{
    union {
        volatile __int64 *i;
        volatile LONGLONG *l;
    } u = { ptr };
    return InterlockedCompareExchange64(u.l, newval, oldval);
}

inline int ExchangeInt(volatile int *ptr, int newval)
{ return ExchangeInt32(ptr, newval); }
inline int CompExchangeInt(volatile int *ptr, int oldval, int newval)
{ return CompExchangeInt32(ptr, oldval, newval); }

inline void *ExchangePtr(XchgPtr *ptr, void *newval)
{
    return InterlockedExchangePointer(ptr, newval);
}
inline void *CompExchangePtr(XchgPtr *ptr, void *oldval, void *newval)
{
    return InterlockedCompareExchangePointer(ptr, newval, oldval);
}


#define ATOMIC(T)  struct { T volatile value; }

#define ATOMIC_INIT_STATIC(_newval) {(_newval)}

#define ATOMIC_LOAD_UNSAFE(_val)  ((_val).value)
#define ATOMIC_STORE_UNSAFE(_val, _newval)  do {  \
    (_val).value = (_newval);                     \
} while(0)

#define ATOMIC_LOAD(_val)  (_ReadBarrier(),(_val).value)
#define ATOMIC_STORE(_val, _newval)  do {  \
    (_val).value = (_newval);              \
    _WriteBarrier();                       \
} while(0)

int _al_invalid_atomic_size(); /* not defined */

#define ATOMIC_FUNC_SELECT(T, C, F32, F64)  ((sizeof(T) == 4) ? (C)F32 : ((sizeof(T) == 8) ? (C)F64 : (C)_al_invalid_atomic_size))

#define ATOMIC_EXCHANGE(T, _val, _newval)  (ATOMIC_FUNC_SELECT(T, T(*)(volatile T*,T), ExchangeInt32, ExchangeInt64)(&(_val).value, (_newval)))
#define ATOMIC_COMPARE_EXCHANGE(T, _val, _oldval, _newval) (ATOMIC_FUNC_SELECT(T, T(*)(volatile T*,T,T), CompExchangeInt32, CompExchangeInt64)(&(_val).value, (_oldval), (_newval)))

#else
#error "No atomic functions available on this platform!"
#endif

#ifdef __cplusplus
}
#endif

#endif /* AL_ATOMIC_H */