-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplasma_atomic.c
536 lines (469 loc) · 17.7 KB
/
plasma_atomic.c
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* plasma_atomic - portable macros for processor-specific atomic operations
*
* inline assembly and compiler intrinsics for atomic operations
*
*
* Copyright (c) 2012, Glue Logic LLC. All rights reserved. code()gluelogic.com
*
* This file is part of plasma.
*
* plasma is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* plasma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with plasma. If not, see <http://www.gnu.org/licenses/>.
*/
#define PLASMA_ATOMIC_C99INLINE_FUNCS
/* inlined functions defined in header
* (generate external linkage definition in GCC versions earlier than GCC 4.3)*/
#if defined(NO_C99INLINE) \
|| defined(__clang__) || (defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__))
#define PLASMA_ATOMIC_C99INLINE
#endif
#include "plasma_atomic.h"
/* inlined functions defined in header
* (generate external linkage definition in C99-compliant compilers)
* (need to -duplicate- definition from header for non-C99-compliant compilers)
*/
#if !defined(__GNUC__) || defined(__GNUC_STDC_INLINE__)
#ifdef __clang__
#undef __attribute_regparm__
#define __attribute_regparm__(x)
#endif
__attribute_regparm__((3))
extern inline
bool
plasma_atomic_CAS_ptr (void ** const restrict ptr,
void * restrict cmpval, void * const restrict newval);
__attribute_regparm__((3))
bool
plasma_atomic_CAS_ptr (void ** const restrict ptr,
void * restrict cmpval, void * const restrict newval);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
bool
plasma_atomic_CAS_64 (uint64_t * const restrict ptr,
uint64_t cmpval, const uint64_t newval);
__attribute_regparm__((3))
bool
plasma_atomic_CAS_64 (uint64_t * const restrict ptr,
uint64_t cmpval, const uint64_t newval);
#endif
__attribute_regparm__((3))
extern inline
bool
plasma_atomic_CAS_32 (uint32_t * const restrict ptr,
uint32_t cmpval, const uint32_t newval);
__attribute_regparm__((3))
bool
plasma_atomic_CAS_32 (uint32_t * const restrict ptr,
uint32_t cmpval, const uint32_t newval);
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_CAS_ptr_val (void ** const ptr,
void *cmpval, void * const newval);
__attribute_regparm__((3))
void *
plasma_atomic_CAS_ptr_val (void ** const ptr,
void *cmpval, void * const newval);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_CAS_64_val (uint64_t * const ptr,
uint64_t cmpval, const uint64_t newval);
__attribute_regparm__((3))
uint64_t
plasma_atomic_CAS_64_val (uint64_t * const ptr,
uint64_t cmpval, const uint64_t newval);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_CAS_32_val (uint32_t * const ptr,
uint32_t cmpval, const uint32_t newval);
__attribute_regparm__((3))
uint32_t
plasma_atomic_CAS_32_val (uint32_t * const ptr,
uint32_t cmpval, const uint32_t newval);
#ifdef plasma_atomic_load_explicit_szof
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_load_64_impl(const void * const restrict ptr,
const enum memory_order order,
const size_t bytes);
__attribute_regparm__((3))
uint64_t
plasma_atomic_load_64_impl(const void * const restrict ptr,
const enum memory_order order,
const size_t bytes);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_load_32_impl(const void * const restrict ptr,
const enum memory_order order,
const size_t bytes);
__attribute_regparm__((3))
uint32_t
plasma_atomic_load_32_impl(const void * const restrict ptr,
const enum memory_order order,
const size_t bytes);
#endif /* plasma_atomic_load_explicit_szof */
#if !(__has_builtin(__atomic_fetch_add) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_fetch_add_ptr (void ** const ptr, ptrdiff_t addval,
const enum memory_order memmodel);
__attribute_regparm__((3))
void *
plasma_atomic_fetch_add_ptr (void ** const ptr, ptrdiff_t addval,
const enum memory_order memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_fetch_add_u64 (uint64_t * const ptr, uint64_t addval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint64_t
plasma_atomic_fetch_add_u64 (uint64_t * const ptr, uint64_t addval,
const enum memory_order memmodel);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_fetch_add_u32 (uint32_t * const ptr, uint32_t addval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint32_t
plasma_atomic_fetch_add_u32 (uint32_t * const ptr, uint32_t addval,
const enum memory_order memmodel);
#endif /* !(__has_builtin(__atomic_fetch_add) || __GNUC_PREREQ(4,7)) */
#if !(__has_builtin(__atomic_fetch_sub) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_fetch_sub_ptr (void ** const ptr, ptrdiff_t subval,
const enum memory_order memmodel);
__attribute_regparm__((3))
void *
plasma_atomic_fetch_sub_ptr (void ** const ptr, ptrdiff_t subval,
const enum memory_order memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_fetch_sub_u64 (uint64_t * const ptr, uint64_t subval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint64_t
plasma_atomic_fetch_sub_u64 (uint64_t * const ptr, uint64_t subval,
const enum memory_order memmodel);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_fetch_sub_u32 (uint32_t * const ptr, uint32_t subval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint32_t
plasma_atomic_fetch_sub_u32 (uint32_t * const ptr, uint32_t subval,
const enum memory_order memmodel);
#endif /* !(__has_builtin(__atomic_fetch_sub) || __GNUC_PREREQ(4,7)) */
#if !(__has_builtin(__atomic_fetch_or) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_fetch_or_ptr (void ** const ptr, uintptr_t orval,
const enum memory_order memmodel);
__attribute_regparm__((3))
void *
plasma_atomic_fetch_or_ptr (void ** const ptr, uintptr_t orval,
const enum memory_order memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_fetch_or_u64 (uint64_t * const ptr, uint64_t orval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint64_t
plasma_atomic_fetch_or_u64 (uint64_t * const ptr, uint64_t orval,
const enum memory_order memmodel);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_fetch_or_u32 (uint32_t * const ptr, uint32_t orval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint32_t
plasma_atomic_fetch_or_u32 (uint32_t * const ptr, uint32_t orval,
const enum memory_order memmodel);
#endif /* !(__has_builtin(__atomic_fetch_or) || __GNUC_PREREQ(4,7)) */
#if !(__has_builtin(__atomic_fetch_and) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_fetch_and_ptr (void ** const ptr, uintptr_t andval,
const enum memory_order memmodel);
__attribute_regparm__((3))
void *
plasma_atomic_fetch_and_ptr (void ** const ptr, uintptr_t andval,
const enum memory_order memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_fetch_and_u64 (uint64_t * const ptr, uint64_t andval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint64_t
plasma_atomic_fetch_and_u64 (uint64_t * const ptr, uint64_t andval,
const enum memory_order memmodel);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_fetch_and_u32 (uint32_t * const ptr, uint32_t andval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint32_t
plasma_atomic_fetch_and_u32 (uint32_t * const ptr, uint32_t andval,
const enum memory_order memmodel);
#endif /* !(__has_builtin(__atomic_fetch_and) || __GNUC_PREREQ(4,7)) */
#if !(__has_builtin(__atomic_fetch_xor) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_fetch_xor_ptr (void ** const ptr, uintptr_t xorval,
const enum memory_order memmodel);
__attribute_regparm__((3))
void *
plasma_atomic_fetch_xor_ptr (void ** const ptr, uintptr_t xorval,
const enum memory_order memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_fetch_xor_u64 (uint64_t * const ptr, uint64_t xorval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint64_t
plasma_atomic_fetch_xor_u64 (uint64_t * const ptr, uint64_t xorval,
const enum memory_order memmodel);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_fetch_xor_u32 (uint32_t * const ptr, uint32_t xorval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint32_t
plasma_atomic_fetch_xor_u32 (uint32_t * const ptr, uint32_t xorval,
const enum memory_order memmodel);
#endif /* !(__has_builtin(__atomic_fetch_xor) || __GNUC_PREREQ(4,7)) */
#if !(__has_builtin(__atomic_compare_exchange_n) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
bool
plasma_atomic_compare_exchange_n_ptr (void ** const ptr,
void ** const cmpval,
void * const newval,
const bool weak,
const enum memory_order success_memmodel,
const enum memory_order failure_memmodel);
__attribute_regparm__((3))
bool
plasma_atomic_compare_exchange_n_ptr (void ** const ptr,
void ** const cmpval,
void * const newval,
const bool weak,
const enum memory_order success_memmodel,
const enum memory_order failure_memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
bool
plasma_atomic_compare_exchange_n_64 (uint64_t * const ptr,
uint64_t * const cmpval,
const uint64_t newval,
const bool weak,
const enum memory_order success_memmodel,
const enum memory_order failure_memmodel);
__attribute_regparm__((3))
bool
plasma_atomic_compare_exchange_n_64 (uint64_t * const ptr,
uint64_t * const cmpval,
const uint64_t newval,
const bool weak,
const enum memory_order success_memmodel,
const enum memory_order failure_memmodel);
#endif
__attribute_regparm__((3))
extern inline
bool
plasma_atomic_compare_exchange_n_32 (uint32_t * const ptr,
uint32_t * const cmpval,
const uint32_t newval,
const bool weak,
const enum memory_order success_memmodel,
const enum memory_order failure_memmodel);
__attribute_regparm__((3))
bool
plasma_atomic_compare_exchange_n_32 (uint32_t * const ptr,
uint32_t * const cmpval,
const uint32_t newval,
const bool weak,
const enum memory_order success_memmodel,
const enum memory_order failure_memmodel);
#endif /* !(__has_builtin(__atomic_compare_exchange_n) || __GNUC_PREREQ(4,7)) */
#if !(__has_builtin(__atomic_exchange_n) || __GNUC_PREREQ(4,7))
__attribute_regparm__((3))
extern inline
void *
plasma_atomic_exchange_n_ptr (void ** const ptr, void * const newval,
const enum memory_order memmodel);
__attribute_regparm__((3))
void *
plasma_atomic_exchange_n_ptr (void ** const ptr, void * const newval,
const enum memory_order memmodel);
#ifndef plasma_atomic_not_implemented_64
__attribute_regparm__((3))
extern inline
uint64_t
plasma_atomic_exchange_n_64 (uint64_t * const ptr, const uint64_t newval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint64_t
plasma_atomic_exchange_n_64 (uint64_t * const ptr, const uint64_t newval,
const enum memory_order memmodel);
#endif
__attribute_regparm__((3))
extern inline
uint32_t
plasma_atomic_exchange_n_32 (uint32_t * const ptr, const uint32_t newval,
const enum memory_order memmodel);
__attribute_regparm__((3))
uint32_t
plasma_atomic_exchange_n_32 (uint32_t * const ptr, const uint32_t newval,
const enum memory_order memmodel);
#endif /* !(__has_builtin(__atomic_exchange_n) || __GNUC_PREREQ(4,7)) */
__attribute_regparm__((1))
extern inline
void
plasma_atomic_lock_release (uint32_t * const ptr);
__attribute_regparm__((1))
void
plasma_atomic_lock_release (uint32_t * const ptr);
__attribute_regparm__((1))
extern inline
bool
plasma_atomic_lock_acquire (uint32_t * const ptr);
__attribute_regparm__((1))
bool
plasma_atomic_lock_acquire (uint32_t * const ptr);
#endif /* !defined(__GNUC__) || defined(__GNUC_STDC_INLINE__) */
#if !(__has_builtin(__atomic_fetch_add) || __GNUC_PREREQ(4,7))
#include <stdio.h>
int
plasma_atomic_fetch_op_notimpl (const char * restrict func, const size_t varsz)
{
fprintf(stderr,
"%s: plasma_atomic_fetch_<op> not implemented for size %zu bytes\n",
func ? func : "", varsz);
return -1;
}
#endif
/* mutex-based fallback implementation, enabled by preprocessor define */
#if defined(PLASMA_ATOMIC_MUTEX_FALLBACK)
#include <pthread.h>
static pthread_mutex_t plasma_atomic_global_mutex = PTHREAD_MUTEX_INITIALIZER;
bool
plasma_atomic_CAS_64_impl (uint64_t * const ptr,
const uint64_t cmpval, const uint64_t newval)
{
bool result;
pthread_mutex_lock(&plasma_atomic_global_mutex);
if ((result = (*ptr == cmpval)))
*ptr = newval;
pthread_mutex_unlock(&plasma_atomic_global_mutex);
return result;
}
bool
plasma_atomic_CAS_32_impl (uint32_t * const ptr,
const uint32_t cmpval, const uint32_t newval)
{
bool result;
pthread_mutex_lock(&plasma_atomic_global_mutex);
if ((result = (*ptr == cmpval)))
*ptr = newval;
pthread_mutex_unlock(&plasma_atomic_global_mutex);
return result;
}
uint64_t
plasma_atomic_CAS_64_val_impl (uint64_t * const ptr,
const uint64_t cmpval, const uint64_t newval)
{
uint64_t result;
pthread_mutex_lock(&plasma_atomic_global_mutex);
if ((result = *ptr) == cmpval)
*ptr = newval;
pthread_mutex_unlock(&plasma_atomic_global_mutex);
return result;
}
uint32_t
plasma_atomic_CAS_32_val_impl (uint32_t * const ptr,
const uint32_t cmpval, const uint32_t newval)
{
uint32_t result;
pthread_mutex_lock(&plasma_atomic_global_mutex);
if ((result = *ptr) == cmpval)
*ptr = newval;
pthread_mutex_unlock(&plasma_atomic_global_mutex);
return result;
}
#endif /* PLASMA_ATOMIC_MUTEX_FALLBACK */
#if defined(__sparc)
#if (defined(__SUNPRO_C) && __SUNPRO_C < 0x5100) \
|| (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x5100) /* Sun Studio < 12.1 */
void
plasma_atomic_SPARC_asm (void)
{
/* SPARC Assembly Language Reference Manual
* http://docs.oracle.com/cd/E26502_01/html/E28387/docinfo.html
* http://sparc.org/standards/SPARCV9.pdf
*/
__asm(".global plasma_atomic_load_8_SPARC\n"
".type plasma_atomic_load_8_SPARC,#function\n"
"plasma_atomic_load_8_SPARC:\n"
"ldx [%o0], %o1\n"
"srlx %o1, 32, %o0\n"
"retl\n"
"srl %o1, 0, %o1\n");
__asm(".global plasma_atomic_store_8_SPARC\n"
".type plasma_atomic_store_8_SPARC,#function\n"
"plasma_atomic_store_8_SPARC:\n"
"sllx %o1, 32, %o1\n"
"srl %o2, 0, %o2\n"
"or %o1, %o2, %o1\n"
"retl\n"
"stx %o1, [%o0]\n");
}
PLASMA_ATTR_Pragma_rarely_called(plasma_atomic_SPARC_asm)
#endif
#endif