-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic.h
451 lines (335 loc) · 11.8 KB
/
atomic.h
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#ifndef RADIUM_COMMON_ATOMIC_H
#define RADIUM_COMMON_ATOMIC_H
#include <stdbool.h>
#include <stdint.h>
#ifndef __ANDROID__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#define ATOMIC_NAME(name) \
name##_atomic
#define DEFINE_ATOMIC(type, name) \
type ATOMIC_NAME(name)
#define ATOMIC_SET(name, val) \
__atomic_store_n (&(ATOMIC_NAME(name)), (val), __ATOMIC_SEQ_CST)
#define ATOMIC_SET_RELAXED(name, val) \
__atomic_store_n (&(ATOMIC_NAME(name)), (val), __ATOMIC_RELAXED)
#define ATOMIC_GET(name) \
__atomic_load_n (&(ATOMIC_NAME(name)), __ATOMIC_SEQ_CST)
/*
#define ATOMIC_GET2(name) \
__atomic_load_n (&(name), __ATOMIC_SEQ_CST)
*/
#define ATOMIC_GET_ARRAY(name,pos) \
__atomic_load_n (&(ATOMIC_NAME(name)[pos]), __ATOMIC_SEQ_CST)
#define ATOMIC_GET_RELAXED(name) \
__atomic_load_n (&(ATOMIC_NAME(name)), __ATOMIC_RELAXED)
#define ATOMIC_SET_ARRAY(name, pos, val) \
__atomic_store_n (&(ATOMIC_NAME(name)[pos]), (val), __ATOMIC_SEQ_CST)
/*
__atomic_compare_exchange_n(type *ptr,
type *expected,
type desired,
bool weak,
int success_memorder,
int failure_memorder
);
works like this:
if (ptr==expected) {
ptr = desired;
return true;
} else {
expected = ptr;
return false
}
*/
/*
atomic_compare_and_set_bool(bool *variable,
bool old_value,
bool new_value
);
works like this:
if (variable==old_value) {
variable = new_value;
return true;
} else {
return false;
}
*/
static inline bool atomic_compare_and_set_bool(bool *variable, bool old_value, bool new_value){
return __atomic_compare_exchange_n (variable, &old_value, new_value, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
}
static inline bool atomic_compare_and_set_int(int *variable, int old_value, int new_value){
return __atomic_compare_exchange_n (variable, &old_value, new_value, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
}
static inline bool atomic_compare_and_set_uint32(uint32_t *variable, uint32_t old_value, uint32_t new_value){
return __atomic_compare_exchange_n (variable, &old_value, new_value, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
}
static inline bool atomic_compare_and_set_pointer(void **variable, void *old_value, void *new_value){
return __atomic_compare_exchange_n (variable, &old_value, new_value, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
}
#define ATOMIC_COMPARE_AND_SET_BOOL(name, old_value, new_value) \
atomic_compare_and_set_bool(&ATOMIC_NAME(name), old_value, new_value)
#define ATOMIC_COMPARE_AND_SET_INT(name, old_value, new_value) \
atomic_compare_and_set_int(&ATOMIC_NAME(name), old_value, new_value)
#define ATOMIC_COMPARE_AND_SET_UINT32(name, old_value, new_value) \
atomic_compare_and_set_uint32(&ATOMIC_NAME(name), old_value, new_value)
#define ATOMIC_COMPARE_AND_SET_POINTER(name, old_value, new_value) \
atomic_compare_and_set_pointer(&ATOMIC_NAME(name), old_value, new_value)
#define ATOMIC_COMPARE_AND_SET_POINTER_ARRAY(name, pos, old_value, new_value) \
atomic_compare_and_set_pointer(&ATOMIC_NAME(name)[pos], old_value, new_value)
#define ATOMIC_SET_RETURN_OLD(name, val) \
__atomic_exchange_n (&ATOMIC_NAME(name), val, __ATOMIC_SEQ_CST)
#define ATOMIC_ADD_RETURN_OLD(name, how_much) \
__atomic_fetch_add (&ATOMIC_NAME(name), how_much, __ATOMIC_SEQ_CST)
#define ATOMIC_ADD(name, how_much) ATOMIC_ADD_RETURN_OLD(name, how_much)
/*
#define ATOMIC_ADD_RETURN_OLD2(name, how_much) \
__atomic_fetch_add (&(name), how_much, __ATOMIC_SEQ_CST)
#define ATOMIC_ADD2(name, how_much) ATOMIC_ADD_RETURN_OLD2(name, how_much)
*/
// doesn't work with bool!
#define ATOMIC_ADD_RETURN_NEW(name, how_much) \
(__atomic_fetch_add (&ATOMIC_NAME(name), how_much, __ATOMIC_SEQ_CST) + how_much)
#define DEFINE_SPINLOCK_NOINIT(name) \
DEFINE_ATOMIC(bool, name)
#define INIT_SPINLOCK(name) \
ATOMIC_SET(name, false)
#define DEFINE_SPINLOCK(name) \
DEFINE_SPINLOCK_NOINIT(name) = false
#define SPINLOCK_OBTAIN(name) \
while(atomic_compare_and_set_bool(&ATOMIC_NAME(name), false, true)==false)
#define SPINLOCK_RELEASE(name) \
ATOMIC_SET(name, false)
#define SPINLOCK_IS_OBTAINED(name) \
ATOMIC_GET(spinlock)==true
/************** float ******************/
// These functions are suppressed from tsan
static inline void safe_float_write(float *pos, float value){
*pos = value;
}
static inline void safe_volatile_float_write(volatile float *pos, float value){
*pos = value;
}
static inline float safe_volatile_float_read(volatile const float *pos){
return *pos;
}
static inline float safe_float_read(const float *pos){
return *pos;
}
static inline void safe_int_write(int *pos, int value){
*pos = value;
}
static inline int safe_int_read(const int *pos){
return *pos;
}
//#define ATOMIC_RELAXED_WRITE(var, value) __atomic_store_n (&var,value, __ATOMIC_RELAXED) // careful. Probably never any point using.
//#define ATOMIC_RELAXED_READ(var) __atomic_load_n (&var, __ATOMIC_RELAXED) // careful. Probably never any point using.
#define ATOMIC_WRITE(var, value) __atomic_store_n (&var,value, __ATOMIC_SEQ_CST)
#define ATOMIC_INC(var, how_much) __atomic_fetch_add (&var, how_much, __ATOMIC_SEQ_CST)
#define ATOMIC_READ(var) __atomic_load_n (&var, __ATOMIC_SEQ_CST)
/************** pointers ******************/
#if 0
static inline void *safe_pointer_read(void **p){
return __atomic_load_n(p, __ATOMIC_RELAXED);
}
#endif
static inline void *atomic_pointer_read(void **p){
return __atomic_load_n(p, __ATOMIC_SEQ_CST);
}
static inline void atomic_pointer_write(void **p, void *v){
__atomic_store_n(p, v, __ATOMIC_SEQ_CST);
}
/************** doubles ******************/
// Is this really working? (Think I spent a long time investigating it, and found out that it was. Double is just 8 bytes, so it shouldn't be different to int64_t)
/*
double dasdouble;
void set_das_double(void){
double new_value_variable = 5.0;
__atomic_store(&dasdouble, &new_value_variable, __ATOMIC_SEQ_CST);
}
=>
64 bit
======
set_das_double:
.LFB0:
.cfi_startproc
movabsq $4617315517961601024, %rax
movq %rax, dasdouble(%rip)
mfence
ret
.cfi_endproc
32 bit
======
set_das_double:
.LFB0:
.cfi_startproc
subl $12, %esp
.cfi_def_cfa_offset 16
xorl %eax, %eax
movl $1075052544, %edx
movl %eax, (%esp)
movl %edx, 4(%esp)
movq (%esp), %xmm0
movq %xmm0, dasdouble
mfence
addl $12, %esp
.cfi_def_cfa_offset 4
ret
.cfi_endproc
----------------------------------------------------------
double get_das_double(void){
double result;
__atomic_load (&dasdouble, &result, __ATOMIC_SEQ_CST);
return result;
}
=>
64 bit
======
get_das_double:
.LFB1:
.cfi_startproc
movq dasdouble(%rip), %rax
movq %rax, -8(%rsp)
movsd -8(%rsp), %xmm0
ret
.cfi_endproc
32 bit
======
get_das_double:
.LFB1:
.cfi_startproc
subl $20, %esp
.cfi_def_cfa_offset 24
movq dasdouble, %xmm0
movsd %xmm0, (%esp)
fldl (%esp)
addl $20, %esp
.cfi_def_cfa_offset 4
ret
.cfi_endproc
*/
typedef double atomic_double_t;
#define ATOMIC_DOUBLE_GET(name) ({ \
double result; \
__atomic_load (&ATOMIC_NAME(name), &result, __ATOMIC_SEQ_CST); \
result; \
})
#define ATOMIC_DOUBLE_SET(name,new_value) ({ \
double new_value_variable = new_value; \
__atomic_store (&ATOMIC_NAME(name), &new_value_variable, __ATOMIC_SEQ_CST); \
})
/*
// redhat gcc 5.3.1: "warning: parameter ‘atomic_double’ set but not used [-Wunused-but-set-parameter]"
static inline double atomic_double_read(const atomic_double_t *atomic_double){
double result;
__atomic_load(atomic_double, &result, __ATOMIC_SEQ_CST);
return result;
}
// redhat gcc 5.3.1: "warning: parameter ‘atomic_double’ set but not used [-Wunused-but-set-parameter]"
static inline void atomic_double_write(atomic_double_t *atomic_double, double new_value){
__atomic_store(atomic_double, &new_value, __ATOMIC_SEQ_CST);
}
*/
#ifdef __cplusplus
namespace radium{
// Can be used if one thread set a set of variables, while another thread read the set of variables
// The writing thread will not block, while the reading thread might block.
// Note: I'm not 100% sure the code is correct, but it probably protects more than if it had not been used.
// Class should not be used if it is extremely important that it works correctly.
//
class SetSeveralAtomicVariables{
DEFINE_ATOMIC(int, generation);
DEFINE_ATOMIC(bool, is_writing);
public:
SetSeveralAtomicVariables(){
ATOMIC_SET(generation, 0);
ATOMIC_SET(is_writing, false);
}
void write_start(void){
ATOMIC_ADD(generation, 1);
ATOMIC_SET(is_writing, true);
ATOMIC_ADD(generation, 1);
}
void write_end(void){
ATOMIC_ADD(generation, 1);
ATOMIC_SET(is_writing, false);
ATOMIC_ADD(generation, 1);
}
int read_start(void){
while(ATOMIC_GET(is_writing)==true);
return ATOMIC_GET(generation);
}
bool read_end(int read_start_generation){
while(ATOMIC_GET(is_writing)==true);
if (ATOMIC_GET(generation) == read_start_generation)
return true;
else
return false;
}
};
// Class to store a pointer.
// The main thread can set, replace and free the pointer at any time.
// A realtime thread can access the pointer at any time by using the ScopedUsage class.
//
class AtomicPointerStorage{
friend class RT_AtomicPointerStorage_ScopedUsage;
private:
DEFINE_ATOMIC(void *, _pointer) = NULL;
DEFINE_ATOMIC(void *, _old_pointer_to_be_freed) = NULL;
void (*_free_pointer_function)(void *);
void maybe_free_something(void *a, void *b){
if (_free_pointer_function != NULL){
if (a!=NULL)
_free_pointer_function(a);
if (b!=NULL)
_free_pointer_function(b);
}
}
public:
AtomicPointerStorage(void (*free_pointer_function)(void *))
: _free_pointer_function(free_pointer_function)
{
}
~AtomicPointerStorage(){
maybe_free_something(ATOMIC_GET(_pointer), ATOMIC_GET(_old_pointer_to_be_freed));
}
// May be called at any time. 'free_pointer_function' may be called 0, 1, or 2 times. (usually 1 time)
void set_new_pointer(void *new_pointer){
void *old_pointer_to_be_freed = ATOMIC_SET_RETURN_OLD(_old_pointer_to_be_freed, NULL);
void *old = ATOMIC_SET_RETURN_OLD(_pointer, new_pointer);
//printf("Has set. new: %p, old: %p, curr: %p\n", new_pointer, old, ATOMIC_GET(_pointer));
maybe_free_something(old, old_pointer_to_be_freed);
}
};
// Create an instance of this class to access pointer from a realtime thread.
// I don't think it works to create more than one instance of this at the same time.
class RT_AtomicPointerStorage_ScopedUsage{
AtomicPointerStorage *_storage;
void *_pointer;
public:
void *get_pointer(void){
return _pointer;
}
RT_AtomicPointerStorage_ScopedUsage(AtomicPointerStorage *storage)
:_storage(storage)
{
_pointer = ATOMIC_SET_RETURN_OLD(storage->_pointer, NULL);
}
~RT_AtomicPointerStorage_ScopedUsage(){
if (ATOMIC_COMPARE_AND_SET_POINTER(_storage->_pointer, NULL, _pointer)) {
return;
} else {
#if !defined(RELEASE)
void *old_pointer = ATOMIC_GET(_storage->_old_pointer_to_be_freed);
if (old_pointer != NULL && old_pointer!=_pointer)
abort();
#endif
ATOMIC_SET(_storage->_old_pointer_to_be_freed, _pointer);
}
}
};
}
#endif
#endif