-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgasnet_atomic_bits.h
More file actions
2702 lines (2529 loc) · 135 KB
/
gasnet_atomic_bits.h
File metadata and controls
2702 lines (2529 loc) · 135 KB
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Source: github.com:BerkeleyLab/gasnet.git/gasnet_atomic_bits.h $
* Description: GASNet header for platform-specific parts of atomic operations
* Copyright 2002, Dan Bonachea <bonachea@cs.berkeley.edu>
* Terms of use are as specified in license.txt
*/
#if !defined(_IN_GASNET_TOOLS_H) && !defined(_IN_GASNETEX_H)
#error This file is not meant to be included directly- clients should include gasnetex.h or gasnet_tools.h
#endif
#ifndef _GASNET_ATOMIC_BITS_H
#define _GASNET_ATOMIC_BITS_H
// NOTE:
// This header provides only a portion of the atomics implementation.
// It is CRUCIAL that any changes/additions to support for any cpu/os/compiler
// combination also be reflected in gasnet_atomic_fwd.h
/* ------------------------------------------------------------------------------------ */
/* Atomics HOW TO:
This file implements the platform-specific aspects of GASNet's atomic
operations, where "platform" is defined as (at least) OS, ABI and compiler.
The GASNet atomics provide a set of operations on 32-bit and 64-bit types,
which are implemented using mutexes if a platform cannot implement all of the
required operations "natively". If that is the case, then a "private" atomic
type can be defined which may have either a reduced set of operations, a size
other than 4 or 8 bytes, or both.
If a platform can implement the 32- and/or 64-bit operations (enumerated
below) then it must provide the following items, where "??" is to be
replaced by "32" or "64".
+ #define GASNETI_HAVE_ATOMIC??_T 1
This indicates that the given type is implemented natively, suppressing the
generation of the corresponding mutex-based version.
+ typedef ... gasneti_atomic??_t;
This is most often implemented as
struct { volatile uint??_t gasneti_ctr; }
Use of a struct prevent client code from accidentally treating it as a
scalar, while the volatile qualifier helps ensure correct code in the
implementation.
+ #define gasneti_atomic??_init(v) ...
Static initializer for gasneti_atomic??_t.
+ One must #define EITHER gasneti_atomic??_[OP] or _gasneti_atomic??_[OP],
for each OP in {set, read, compare_and_swap, swap}.
The first (no "_" prefix) form takes a "const int flags" argument as
described in README-tools and must implement the fences it requests. In
the case of implementations of the atomic operations which include both a
compiler fence and full memory barrier, one can simply ignore the argument.
At present there are no platforms using this non-prefixed option EXCEPT
for fully-fenced ones which ignore the flags argument.
The form with the "_" prefix does NOT take a "flags" argument and the
atomics subsystem automatically implements the fences to produce the
non-prefixed version. Because of the way these are used, it is safe to
define these as macros which evaluate their arguments multiple times
because the arguments will never have side-effects.
A #define must be used to allow the rest of the atomics subsystem to know
which is available. However, the OP itself is often implemented as an
inline function and its name is #define'd to itself.
+ The arithmetic operations {increment, decrement, add, subtract} can be
provided either explicitly or implicitly. The choice between explicit or
implicit definition is independent - one may mix explicit and implicit
definitions of these four operations freely.
To provide the operations explicitly one #define's them, with or without a
"_" prefix as above. If neither form is defined, the atomics subsystem
will construct them automatically provided one #defines any ONE of the
following:
uint??_t gasneti_atomic??_fetchadd(ptr, op, flags)
uint??_t gasneti_atomic??_addfetch(ptr, op, flags)
uint??_t _gasneti_atomic??_fetchadd(ptr, op)
uint??_t _gasneti_atomic??_addfetch(ptr, op)
A "fetchadd" returns the value before addition, while a "addfetch" returns
the value after. Note that the "op" has an unsigned type (uint??_t) rather
than signed (int??_t) as in the add and subtract operations. This does not
impact the results since we perform 2s-complement arithmetic on the 32- and
64-bit types.
+ The decrement_and_test operation can either be #define'd explicitly (with
or without a "_" prefix) OR it can be constructed automatically from either
a "_"-prefixed fetchadd or addfetch. However, it will NOT be constructed
automatically from the corresponding non-prefixed (fenced) operations
because there is currently no suitable mechanism to reconcile the fencing.
+ Fences:
If you define any of the operations above with the "_" prefix, then the
non-prefixed version (w/ flags argument) will be constructed automatically,
and this construction will implement the fences requested by this flags
argument. By default this construction assumes that there are no fencing
side-effects (compiler fence or memory barriers) in the "_"-prefixed
operations. When that is NOT the case, one can override this default
behavior by defining the appropriate fencing macros. At present this is
done only for the x86/x86-64.
In the case of the x86/x86-64 all of the read-modify-write operations
include a full memory barrier but do NOT include a compiler fence. So
the following definitions are used to replace the defaults:
#define _gasneti_atomic??_prologue_rmw(p,f)
#define _gasneti_atomic??_fence_before_rmw(p,f) _gasneti_atomic_cf_before(f)
#define _gasneti_atomic??_fence_after_rmw(p,f) _gasneti_atomic_cf_after(f)
#define _gasneti_atomic??_fence_after_bool(p,f,v) _gasneti_atomic_cf_after(f)
where "cf" stands for compiler fence and _gasneti_atomic_cf_before() and
...after are defined in gasneti_atomicops.h along with several other macros
used to construct the default fences.
+ Native atomics:
The term "native atomics" is used to refer to the implementation of
atomic operations via inline assembly.
+ Compiler atomics and OS atomics:
These terms are used to refer to the implementation of atomic operations by
a third-party such as a compiler or a system header.
+ Generic atomics:
The term "generic atomics" is used here and in gasnet_atomicops.h to refer
to the implementation of atomic operations via gasnett_mutex_t.
+ Hybrid 64-bit atomics:
With an ABI for which the alignment of 64-bit types is NOT sufficient to
allow free use of the CPU's native atomic operations, we have a "hybrid"
mechanism in which a run-time branch selects between native atomics for the
properly-aligned case and generic atomics for unaligned values. A platform
requiring the hybrid implementation need only
#define GASNETI_HYBRID_ATOMIC64 1
and provide exactly the operations expected by the corresponding code in
gasnet_atomicops.h (search for GASNETI_HYBRID_ATOMIC64).
+ Special atomics:
The term "special atomics" describes the case of native atomics for C
compilers which require use of "out-of-line" asm support (receiving args
and returning a value using the ABI-defined function calling convention).
In this case the implementation defines the necessary operations through
any combination of macros, inline functions and "special" functions.
Special functions are defined via GASNETI_ATOMIC{32,64}_[OPNAME]_BODY
macros, each providing the assembly to be expanded as the body of a
library function (and triggering generation of the necessary wrappers).
The macros GASNETI_ATOMIC{32,64}_SPECIALS collect the BODY macros to be
expanded in gasnet_tools.c.
+ Slow atomics:
The term "slow atomics" is for when the C compiler which built the gasnet
library used an implementation which the current compiler cannot be relied
upon to also generate. This can occur (1) when the C++ compiler lacks the
same asm support as the C compiler, or (2) when the compiler used at client
compile time does not match the one used to build the library. All that is
required to use slow atomics is to define GASNETI_USING_SLOW_ATOMIC{32,64}
where one would otherwise define the atomic functions. These cause atomic
operations to resolve function calls to the library.
Note: The CC probed at configure time cannot use slow atomics.
Note: The generic and slow atomics are mutually exclusive for a
given bit-width.
+ Differing implementations of 32-bit and 64-bit atomics:
There are assumptions in how atomicsops are constructed for compilers other
than the configure-time CC which limit which 64-bit atomics implementations
can be mixed with a given 32-bit implementation. The main limitation is
that the 64-bit atomics are never "better" than the 32-bit. The
assumptions are listed (and enforced) at the end of gasnet_atomic_fwd.h.
TODO: Fully document the macros used to override the default fences.
For now, the code for x86/x86_64 is the only decent example.
TODO: Document definition of a "private" atomic type.
There are no surviving examples, however the SPARC7 and
PA-RISC code (removed after GASNet-1.22.0) were good examples.
SEE ALSO: https://gasnet-bugs.lbl.gov/bugzilla/show_bug.cgi?id=1607
*/
/* ------------------------------------------------------------------------------------ */
/* Determine which atomic implementations are appropriate to the current compiler */
#include "gasnet_atomic_fwd.h"
/* ------------------------------------------------------------------------------------ */
/* Work-arounds and special cases for various platforms */
/* Cannot always use "register" (bug 3528): */
#ifdef GASNETI_ASM_REGISTER_KEYWORD
/* Preserve any override */
#elif PLATFORM_ARCH_ARM && defined(GASNETI_HAVE_ARM_CMPXCHG) && \
PLATFORM_OS_LINUX && (PLATFORM_COMPILER_GNU || PLATFORM_COMPILER_CLANG)
/* This target *must* use the "register" keyword.
* Leaving GASNETI_ASM_REGISTER_KEYWORD undefined will catch any mis-guided use.
*/
#elif (__cplusplus >= 201103L)
/* C++11 deprecated the "register" keyword, and C++17 makes it a reserved word. */
#define GASNETI_ASM_REGISTER_KEYWORD /* empty */
#else
#define GASNETI_ASM_REGISTER_KEYWORD register
#endif
#if PLATFORM_ARCH_X86_64 || PLATFORM_ARCH_X86 || PLATFORM_ARCH_MIC
#ifdef GASNETI_UNI_BUILD
#define GASNETI_X86_LOCK_PREFIX ""
#else
#define GASNETI_X86_LOCK_PREFIX "lock\n\t"
#endif
/* Partial x86 solution(s) to bug 1718 (-fPIC support when configure-time check was non-PIC).
* AUTOMATIC WORK AROUND:
* + gcc: on most platforms (including Linux, Darwin and Solaris) defines __PIC__ when building
* position independent code (e.g. -fPIC or -fpic; not passed -mdynamic-no-pic on Darwin).
* + pathcc: same as gcc
* MANUAL WORK AROUND:
* + pgcc: no distinguishing macro when passed -fPIC, so no automatic work-around available
* JUST WORKS:
* + icc: mimics gcc, but is able to schedule %ebx so no work-around is needed
* + open64: mimics gcc, but is able to schedule %ebx so no work-around is needed
* + llvm-gcc: mimics gcc, but is able to schedule %ebx so no work-around is needed
* + Sun cc: use of specials doesn't encounter the problem
* NOT APPLICABLE:
* + nvhpc: only generates LP64 ABI (PLATFORM_ARCH_64), not ILP32 (PLATFORM_ARCH_32)
*
* Bottom line is that we recommend YOUR_PIC_CFLAGS="-fPIC -DGASNETI_FORCE_PIC",
* replacing "-fPIC" with your compiler-specific flag(s) as needed.
*/
#if ((PLATFORM_COMPILER_GNU && !defined(__llvm__)) || \
PLATFORM_COMPILER_PATHSCALE || PLATFORM_COMPILER_PGI) && \
((defined(__PIC__) && !defined(GASNETI_CONFIGURED_PIC)) || defined(GASNETI_FORCE_PIC))
/* Disable use of %ebx when building PIC, but only on affected compilers. */
#define GASNETI_USE_X86_EBX 0
#endif
/* By default use the configure-probed result */
#ifndef GASNETI_USE_X86_EBX
#define GASNETI_USE_X86_EBX GASNETI_HAVE_X86_EBX
#endif
#endif
/* ------------------------------------------------------------------------------------ */
/* ABI properties, independent of implementation */
// Exceptional cases
#if PLATFORM_ARCH_X86
#define gasneti_atomic64_align 4
#endif
// Defaults
#ifndef gasneti_atomic32_align
#define gasneti_atomic32_align 4
#endif
#ifndef gasneti_atomic64_align
#define gasneti_atomic64_align 8
#endif
/* ------------------------------------------------------------------------------------ */
/* Helpers for "special" call-based atomics on platforms w/ crippled inline asm support. */
#define GASNETI_SPECIAL_ASM_DECL(name) \
extern void name(void)
#define GASNETI_SPECIAL_ASM_DEFN(name, body) \
GASNETI_NEVER_INLINE(name, extern void name(void)) { body; }
/* ------------------------------------------------------------------------------------ */
// Logic to handle compilers other than the configured CC, including:
// + CXX, if any
// + MPI_CC, if any and it differs from CC
// + compilers not probed by configure
// The first two cases (CXX and MPI_CC) differ from the last in the use of
// configure probed information which may be more precise than what can be
// determined from the preprocess environment alone.
//
// NOTE: probably incomplete with respect to a "private" atomic type
#if !GASNETI_COMPILER_IS_CC
// The current compiler is not the C compiler used to build GASNet libraries.
// So, we must decide between allowing the compiler to generate either the
// actual atomicops or library calls.
// Notes on COMPATIBLE implementations
//
// Two implementations are considered "compatible" if they interoperate
// (linking and producing correct results). The logic below determines if
// the current compiler would normally use implementations of the 32- and
// 64-bit atomic operations that are compatible with those in the GASNet
// libraries. If not, then later logic makes the necessary definitions to
// ensure force use of compatible implementations. In some cases this
// means use of "SLOW" atomics - calling GASNet library functions.
//
// GENERIC, OS and HYBRID implementations are each compatible only with
// themselves. However, each is compatible across all supported compiler
// families.
//
// NATIVE and SYNC implementations are currently all mutually compatible.
// This is true because we currently only enable __sync atomics which we
// believe to be compatible with the native inline asm. In particular we
// assume compilers do not use a mutex-based implementation which might not
// be compatible with native atomics or between distinct compiler families.
// If these assumptions are ever broken, then additional logic will be
// needed to sort out which pairs are compatible.
//
// The compatibility of SPECIAL implementations is asymmetric. A library
// built with SPECIAL atomics is compatible with any compiler's use of SYNC
// or NATIVE atomics implementations for the same reasons those two can be
// mixed. However, a SPECIAL implementation uses compiler-specific support
// routines in the library. Therefore a compiler's SPECIAL implementation
// is compatible only with a library built with SPECIAL by the same compiler
// family (all other cases will lack the appropriate support routines).
#if (GASNETI_ATOMIC32_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_SPECIAL)
#if (GASNETI_ATOMIC32_IMPL == GASNETI_ATOMIC_IMPL_SPECIAL) && \
(PLATFORM_COMPILER_FAMILYID == GASNETI_PLATFORM_COMPILER_FAMILYID)
// SPECIAL is self-COMPATIBLE only within a compiler family
#elif (GASNETI_ATOMIC32_IMPL == GASNETI_ATOMIC_IMPL_NATIVE) || \
(GASNETI_ATOMIC32_IMPL == GASNETI_ATOMIC_IMPL_SYNC)
// SPECIAL library is assumed COMPATIBLE with NATIVE and SYNC
// Exceptions should be added here
#else
#define GASNETI_ATOMIC32_WANT_SLOW 1
#endif
#elif (GASNETI_ATOMIC32_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_GENERIC)
#define GASNETI_ATOMIC32_WANT_GENERIC 1
#elif (GASNETI_ATOMIC32_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_OS)
#define GASNETI_ATOMIC32_WANT_OS 1
#elif (GASNETI_ATOMIC32_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_NATIVE) || \
(GASNETI_ATOMIC32_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_SYNC)
#if (GASNETI_ATOMIC32_IMPL == GASNETI_ATOMIC_IMPL_NATIVE) || \
(GASNETI_ATOMIC32_IMPL == GASNETI_ATOMIC_IMPL_SYNC)
// All NATIVE and SYNC are assumed to be COMPATIBLE (see note above)
// Exceptions should be added here
#else
#define GASNETI_ATOMIC32_WANT_SLOW 1
#endif
#else
// NOTE 32-bit atomics are never "hybrid"
#error Internal error - unexpected atomics configuration
#endif
#if (GASNETI_ATOMIC64_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_SPECIAL)
#if (GASNETI_ATOMIC64_IMPL == GASNETI_ATOMIC_IMPL_SPECIAL) && \
(PLATFORM_COMPILER_FAMILYID == GASNETI_PLATFORM_COMPILER_FAMILYID)
// SPECIAL is self-COMPATIBLE only within a compiler family
#elif (GASNETI_ATOMIC64_IMPL == GASNETI_ATOMIC_IMPL_NATIVE) || \
(GASNETI_ATOMIC64_IMPL == GASNETI_ATOMIC_IMPL_SYNC)
// SPECIAL library is assumed COMPATIBLE with NATIVE and SYNC
// Exceptions should be added here
#else
#define GASNETI_ATOMIC64_WANT_SLOW 1
#endif
#elif (GASNETI_ATOMIC64_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_GENERIC)
#define GASNETI_ATOMIC64_WANT_GENERIC 1
#elif (GASNETI_ATOMIC64_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_OS)
#define GASNETI_ATOMIC64_WANT_OS 1
#elif (GASNETI_ATOMIC64_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_NATIVE) || \
(GASNETI_ATOMIC64_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_SYNC)
#if (GASNETI_ATOMIC64_IMPL == GASNETI_ATOMIC_IMPL_NATIVE) || \
(GASNETI_ATOMIC64_IMPL == GASNETI_ATOMIC_IMPL_SYNC)
// All NATIVE and SYNC are assumed to be COMPATIBLE (see note above)
// Exceptions should be added here
#else
#define GASNETI_ATOMIC64_WANT_SLOW 1
#endif
#elif (GASNETI_ATOMIC64_IMPL_CONFIGURE == GASNETI_ATOMIC_IMPL_HYBRID)
// Note that hybrid is not "compatible" with anything but itself
#define GASNETI_ATOMIC64_WANT_SLOW 1
#else
#error Internal error - unexpected atomics configuration
#endif
// As checked by the logic at the end of gasnet_atomic_fwd.h, the
// implementation of 32-bit atomics strongly limits which implementations
// may be used for 64-bit atomics. As a result, the disposition of 32-bit
// atomics is the sole determining factor in how the main implementation
// ladder will be traversed.
#if GASNETI_ATOMIC32_WANT_GENERIC
#define GASNETI_USE_GENERIC_ATOMICOPS 1
#elif GASNETI_ATOMIC32_WANT_OS
#define GASNETI_USE_OS_ATOMICOPS 1
#elif GASNETI_ATOMIC32_WANT_SLOW
#define GASNETI_USING_SLOW_ATOMIC32 1
#define GASNETI_ATOMIC32_NOINLINE 1 // Suppress OS, SYNC and NATIVE
#define GASNETI_HAVE_ATOMIC32_T 1 // Suppress GENERIC
#endif
// Disposition of 64-bit atomics must suppress cases which could be
// reachable in the main ladder, but are undesired.
#if GASNETI_ATOMIC64_WANT_GENERIC
#define GASNETI_ATOMIC64_NOINLINE 1 // Suppress OS, SYNC, NATIVE, HYBRID
#elif GASNETI_ATOMIC64_WANT_SLOW
#define GASNETI_USING_SLOW_ATOMIC64 1
#define GASNETI_ATOMIC64_NOINLINE 1 // Suppress OS, SYNC, NATIVE, HYBRID
#define GASNETI_HAVE_ATOMIC64_T 1 // Suppress GENERIC
#endif
// Check for cases in which desired 64-bit atomics are unreachable.
// Any #error here which does not first trip an #error at the end of
// gasnet_atomic_fwd.h indicates a flaw in the logic above.
#if (GASNETI_ATOMIC32_NOINLINE && !GASNETI_ATOMIC64_NOINLINE) || \
(GASNETI_ATOMIC64_WANT_OS && !GASNETI_USE_OS_ATOMICOPS)
#error Internal error - unexpected atomics configuration
#endif
// For local use only
#undef GASNETI_ATOMIC32_WANT_GENERIC
#undef GASNETI_ATOMIC64_WANT_GENERIC
#undef GASNETI_ATOMIC32_WANT_OS
#undef GASNETI_ATOMIC64_WANT_OS
#undef GASNETI_ATOMIC32_WANT_SLOW
#undef GASNETI_ATOMIC64_WANT_SLOW
#endif
/* ------------------------------------------------------------------------------------ */
#if defined(GASNETI_ATOMIC32_NOINLINE)
/* Logic above has determined current compiler cannot safely use these implementations. */
/* This case exists only to prevent the following cases from matching. */
#elif defined(GASNETI_USE_GENERIC_ATOMICOPS)
/* Use a very slow but portable implementation of atomic ops using mutexes */
/* This case exists only to prevent the following cases from matching. */
#elif defined(GASNETI_USE_COMPILER_ATOMICOPS)
#if GASNETI_HAVE_SYNC_ATOMICS_32
/* Generic implementation in terms of GCC's __sync atomics */
#if 0 // Update if/when using 32-bit __sync atomic where they are not signal-safe
#define GASNETI_ATOMIC32_NOT_SIGNALSAFE 1
#endif
/* GCC documentation promises a full memory barrier */
#define _gasneti_atomic32_prologue_rmw(p,f) /*empty*/
#define _gasneti_atomic32_fence_before_rmw(p,f) /*empty*/
#define _gasneti_atomic32_fence_after_rmw(p,f) /*empty*/
#define _gasneti_atomic32_fence_after_bool(p,f,v) /*empty*/
#define GASNETI_HAVE_ATOMIC32_T 1
typedef struct { volatile uint32_t gasneti_ctr; } gasneti_atomic32_t;
#define gasneti_atomic32_init(v) { (v) }
#define _gasneti_atomic32_read(p) ((p)->gasneti_ctr)
#define _gasneti_atomic32_set(p,v) do { (p)->gasneti_ctr = (v); } while(0)
/* Default impls of inc, dec, add and sub */
#define gasneti_atomic32_fetchadd(p,op,f) (__sync_fetch_and_add(&(p)->gasneti_ctr, (uint32_t)(op)))
#define gasneti_atomic32_decrement_and_test(p,f) (1==gasneti_atomic32_fetchadd((p),-1,(f)))
#define gasneti_atomic32_compare_and_swap(p,oval,nval,f) \
(__sync_bool_compare_and_swap(&((p)->gasneti_ctr), (oval), (nval)))
#if __GCC_ATOMIC_TEST_AND_SET_TRUEVAL // means test_and_set is actually a swap
// Unlike the other __sync*(), __sync_lock_test_and_set() is documented as
// having only an ACQ fence. So, we *may* need to make up the difference.
#if PLATFORM_ARCH_X86 || PLATFORM_ARCH_X86_64 || PLATFORM_ARCH_MIC
// Nothing to do: SWAP must be lock-prefix instruction (or mutex cycle)
// TODO: other ARCHs w/ this property?
#define _gasneti_atomic_fence_before_swap(f) ((void)0)
#define _gasneti_atomic_fence_after_swap(f) ((void)0)
#elif (GASNETI_RMB_IS_MB && 0)
// TODO: disabled since GASNETI_RMB_IS_MB could reflect *our* choice
// of RMB and not the implementation of __sync_lock_test_and_set().
// Example: on PPC we use lwsync but compiler might use isync.
#define _gasneti_atomic_fence_before_swap(f) _gasneti_atomic_fence_before(f)
#define _gasneti_atomic_fence_after_swap() ((void)0)
#else
#define _gasneti_atomic_fence_before_swap(f) _gasneti_atomic_fence_before(f)
#define _gasneti_atomic_fence_after_swap(f) _gasneti_atomic_fence_after((f) & ~GASNETI_ATOMIC_ACQ)
#endif
#endif
// Two implementations of SWAP are possible:
// 1) __sync_lock_test_and_set() is often a swap, and modern GCC will
// `#define __GCC_ATOMIC_TEST_AND_SET_TRUEVAL 1` on such platforms.
// Since it has only an acquire fence, some additional work is required.
// 2) Loop on (fully-fenced) __sync_val_compare_and_swap().
GASNETI_INLINE(gasneti_atomic32_swap)
uint32_t gasneti_atomic32_swap(gasneti_atomic32_t *_p, uint32_t _nval, const int _flags) {
GASNETI_ASM_REGISTER_KEYWORD volatile uint32_t *_p32 = &(_p->gasneti_ctr);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _oval;
#if __GCC_ATOMIC_TEST_AND_SET_TRUEVAL
_gasneti_atomic_fence_before_swap(_flags);
_oval = __sync_lock_test_and_set(_p32,_nval);
_gasneti_atomic_fence_after_swap(_flags);
#else
GASNETI_ASM_REGISTER_KEYWORD uint32_t _tmp = *_p32;
do {
_oval = _tmp;
} while (_oval != (_tmp = __sync_val_compare_and_swap(_p32,_oval,_nval)));
#endif
return _oval;
}
// TODO: use 64-bit sync atomics on ILP32 iff signal safe (otherwise probably equivalent to generics)
#if GASNETI_ATOMIC64_NOINLINE
// Using SLOW or GENERIC alternative
#elif PLATFORM_ARCH_64 && GASNETI_HAVE_SYNC_ATOMICS_64
#if 0 // Update if/when using 64-bit __sync atomic where they are not signal-safe
#define GASNETI_ATOMIC64_NOT_SIGNALSAFE 1
#endif
#define _gasneti_atomic64_prologue_rmw(p,f) /*empty*/
#define _gasneti_atomic64_fence_before_rmw(p,f) /*empty*/
#define _gasneti_atomic64_fence_after_rmw(p,f) /*empty*/
#define _gasneti_atomic64_fence_after_bool(p,f,v) /*empty*/
#define GASNETI_HAVE_ATOMIC64_T 1
typedef struct { volatile uint64_t gasneti_ctr; } gasneti_atomic64_t;
#define gasneti_atomic64_init(v) { (v) }
#define _gasneti_atomic64_read(p) ((p)->gasneti_ctr)
#define _gasneti_atomic64_set(p,v) do { (p)->gasneti_ctr = (v); } while(0)
#define gasneti_atomic64_fetchadd(p,op,f) (__sync_fetch_and_add(&(p)->gasneti_ctr, (uint64_t)(op)))
#define gasneti_atomic64_decrement_and_test(p,f) (1==gasneti_atomic64_fetchadd((p),-1,(f)))
#define gasneti_atomic64_compare_and_swap(p,oval,nval,f) \
(__sync_bool_compare_and_swap(&((p)->gasneti_ctr), (oval), (nval)))
// See comments w/ gasneti_atomic64_swap
GASNETI_INLINE(gasneti_atomic64_swap)
uint64_t gasneti_atomic64_swap(gasneti_atomic64_t *_p, uint64_t _nval, const int _flags) {
GASNETI_ASM_REGISTER_KEYWORD volatile uint64_t *_p64 = &(_p->gasneti_ctr);
GASNETI_ASM_REGISTER_KEYWORD uint64_t _oval;
#if __GCC_ATOMIC_TEST_AND_SET_TRUEVAL
_gasneti_atomic_fence_before_swap(_flags);
_oval = __sync_lock_test_and_set(_p64,_nval);
_gasneti_atomic_fence_after_swap(_flags);
#else
GASNETI_ASM_REGISTER_KEYWORD uint64_t _tmp = *_p64;
do {
_oval = _tmp;
} while (_oval != (_tmp = __sync_val_compare_and_swap(_p64,_oval,_nval)));
#endif
return _oval;
}
#endif /* GASNETI_HAVE_SYNC_ATOMICS_64 */
#undef _gasneti_atomic_fence_before_swap
#undef _gasneti_atomic_fence_after_swap
#else
#error "GASNETI_USE_COMPILER_ATOMICOPS for unknown or unsupported compiler"
#endif
#elif defined(GASNETI_USE_OS_ATOMICOPS)
/* ------------------------------------------------------------------------------------
* Use OS-provided atomics, which should be CPU-independent and
* which should work regardless of the compiler's inline assembly support.
* ------------------------------------------------------------------------------------ */
#if PLATFORM_OS_CYGWIN
/* These are *NOT* Cygwin calls, but Windows API calls that may actually
* be intrinsics in the MS compilers on 64-bit systems.
* Intrinsics below incur full memory barriers. Win8+ offer finer-grained
* control over fencing, but we don't currently use those. */
#include <windows.h>
#define GASNETI_HAVE_ATOMIC32_T 1
typedef struct { volatile uint32_t gasneti_ctr; } gasneti_atomic32_t;
#define gasneti_atomic32_init(v) { (v) }
#define gasneti_atomic32_increment(p,f) InterlockedIncrement((LONG *)&((p)->gasneti_ctr))
#define gasneti_atomic32_decrement(p,f) InterlockedDecrement((LONG *)&((p)->gasneti_ctr))
#define _gasneti_atomic32_read(p) ((p)->gasneti_ctr)
#define _gasneti_atomic32_set(p,v) ((p)->gasneti_ctr = (v))
#define gasneti_atomic32_decrement_and_test(p,f) \
(InterlockedDecrement((LONG *)&((p)->gasneti_ctr)) == 0)
#define gasneti_atomic32_compare_and_swap(p,oval,nval,f) \
(InterlockedCompareExchange((LONG *)&((p)->gasneti_ctr),nval,oval) == (LONG)(oval))
#define gasneti_atomic32_fetchadd(p,op,f) InterlockedExchangeAdd((LONG *)&((p)->gasneti_ctr), op)
#define gasneti_atomic32_swap(p,op,f) InterlockedExchange((LONG *)&((p)->gasneti_ctr), op)
#if GASNETI_ATOMIC64_NOINLINE
// Using SLOW or GENERIC alternative
#elif PLATFORM_ARCH_64 /* TODO: Identify ILP32 running on 64-bit CPU */
#define GASNETI_HAVE_ATOMIC64_T 1
typedef struct { volatile uint64_t gasneti_ctr; } gasneti_atomic64_t;
#define gasneti_atomic64_init(v) { (v) }
#define gasneti_atomic64_increment(p,f) InterlockedIncrement64((LONGLONG *)&((p)->gasneti_ctr))
#define gasneti_atomic64_decrement(p,f) InterlockedDecrement64((LONGLONG *)&((p)->gasneti_ctr))
#define _gasneti_atomic64_read(p) ((p)->gasneti_ctr)
#define _gasneti_atomic64_set(p,v) ((p)->gasneti_ctr = (v))
#define gasneti_atomic64_decrement_and_test(p,f) \
(InterlockedDecrement64((LONGLONG *)&((p)->gasneti_ctr)) == 0)
#define gasneti_atomic64_compare_and_swap(p,oval,nval,f) \
(InterlockedCompareExchange64((LONGLONG *)&((p)->gasneti_ctr),nval,oval) == (LONGLONG)(oval))
#define gasneti_atomic64_fetchadd(p,op,f) InterlockedExchangeAdd64((LONGLONG *)&((p)->gasneti_ctr), op)
#define gasneti_atomic64_swap(p,op,f) InterlockedExchange64((LONGLONG *)&((p)->gasneti_ctr), op)
#endif
/* ------------------------------------------------------------------------------------ */
#else
#error GASNETI_USE_OS_ATOMICOPS defined on unsupported OS - need to implement GASNet atomics (or #define GASNETI_USE_GENERIC_ATOMICOPS)
#endif
#else
/* ------------------------------------------------------------------------------------
* Not using GENERIC (mutex), compiler-provided or OS-provided atomics, so
* provide our own based on the CPU and compiler support for inline assembly code
* ------------------------------------------------------------------------------------ */
#if PLATFORM_ARCH_X86 || PLATFORM_ARCH_X86_64 || PLATFORM_ARCH_MIC /* x86 and Athlon64/Opteron and MIC */
/* We have a full memory barrier in all read-modify-write operations,
* but NOT a compiler fence. */
#define _gasneti_atomic32_prologue_rmw(p,f) /*empty*/
#define _gasneti_atomic32_fence_before_rmw(p,f) _gasneti_atomic_cf_before(f)
#define _gasneti_atomic32_fence_after_rmw(p,f) _gasneti_atomic_cf_after(f)
#define _gasneti_atomic32_fence_after_bool(p,f,v) _gasneti_atomic_cf_after(f)
#define _gasneti_atomic64_prologue_rmw(p,f) /*empty*/
#define _gasneti_atomic64_fence_before_rmw(p,f) _gasneti_atomic_cf_before(f)
#define _gasneti_atomic64_fence_after_rmw(p,f) _gasneti_atomic_cf_after(f)
#define _gasneti_atomic64_fence_after_bool(p,f,v) _gasneti_atomic_cf_after(f)
/* The odd-ball is 64-bit read/set on ILP32, for which we have
* fully fenced read and set. */
#if PLATFORM_ARCH_32
#define _gasneti_atomic64_prologue_set(p,f) /*empty*/
#define _gasneti_atomic64_fence_before_set(p,f) _gasneti_atomic_cf_before(f)
#define _gasneti_atomic64_fence_after_set(p,f) _gasneti_atomic_cf_after(f)
#define _gasneti_atomic64_prologue_read(p,f) /*empty*/
#define _gasneti_atomic64_fence_before_read(p,f) _gasneti_atomic_cf_before(f)
#define _gasneti_atomic64_fence_after_read(p,f) _gasneti_atomic_cf_after(f)
#endif
#if PLATFORM_COMPILER_GNU || PLATFORM_COMPILER_INTEL || \
PLATFORM_COMPILER_PATHSCALE || PLATFORM_COMPILER_PGI || \
PLATFORM_COMPILER_OPEN64 || \
PLATFORM_COMPILER_CLANG || \
PLATFORM_COMPILER_NVHPC || \
(PLATFORM_COMPILER_SUN && GASNETI_HAVE_GCC_ASM)
#if PLATFORM_COMPILER_SUN_C
#pragma error_messages(off, E_ASM_UNUSED_PARAM)
#elif PLATFORM_COMPILER_SUN_CXX
#pragma error_messages(off, inlasmpnu)
#endif
#define GASNETI_HAVE_ATOMIC32_T 1
typedef struct { volatile uint32_t gasneti_ctr; } gasneti_atomic32_t;
#define gasneti_atomic32_init(v) { (v) }
#if !(GASNETI_ATOMIC64_NOINLINE || (PLATFORM_COMPILER_OPEN64 && PLATFORM_ARCH_32))
#define GASNETI_HAVE_ATOMIC64_T 1
typedef struct { volatile uint64_t gasneti_ctr; } gasneti_atomic64_t;
#define gasneti_atomic64_init(v) { (v) }
#endif
#if PLATFORM_COMPILER_PATHSCALE || PLATFORM_COMPILER_OPEN64
/* Pathscale optimizer is buggy and fails to clobber memory output location correctly
unless we include an extraneous full memory clobber
*/
#define GASNETI_ATOMIC_MEM_CLOBBER ,"memory"
#else
#define GASNETI_ATOMIC_MEM_CLOBBER
#endif
GASNETI_INLINE(_gasneti_atomic32_swap)
uint32_t _gasneti_atomic32_swap(gasneti_atomic32_t *_v, uint32_t _value) {
GASNETI_ASM_REGISTER_KEYWORD uint32_t _x = _value;
__asm__ __volatile__(
GASNETI_X86_LOCK_PREFIX /* 'lock' is implied, but is the fence? */
"xchgl %0, %1"
: "=r" (_x)
: "m" (_v->gasneti_ctr), "0" (_x)
: "cc", "memory" /* instead of listing (v->gasneti_ctr) as an output */ );
return _x;
}
#define _gasneti_atomic32_swap _gasneti_atomic32_swap
#if GASNETI_PGI_ASM_BUG3674
#define _gasneti_atomic32_read(p) (*(uint32_t volatile *)&((p)->gasneti_ctr))
#define _gasneti_atomic32_set(p,v) (*(uint32_t volatile *)&((p)->gasneti_ctr) = (v))
#else
#define _gasneti_atomic32_read(p) ((p)->gasneti_ctr)
#define _gasneti_atomic32_set(p,v) ((p)->gasneti_ctr = (v))
#endif
#if PLATFORM_COMPILER_SUN
// Sun compiler warns if any %n fails to appear in the template.
// Suppression of the warning via #pragma is only partially effective.
// Fortunately, "appearance" in comments is sufficient.
#define GASNETI_ASM_USED(n) "# arg %" #n " is used \n\t"
#else
#define GASNETI_ASM_USED(n)
#endif
GASNETI_INLINE(_gasneti_atomic32_increment)
void _gasneti_atomic32_increment(gasneti_atomic32_t *_v) {
__asm__ __volatile__(
GASNETI_ASM_USED(1)
GASNETI_X86_LOCK_PREFIX
"incl %0"
: "=m" (_v->gasneti_ctr)
: "m" (_v->gasneti_ctr)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
}
#define _gasneti_atomic32_increment _gasneti_atomic32_increment
GASNETI_INLINE(_gasneti_atomic32_decrement)
void _gasneti_atomic32_decrement(gasneti_atomic32_t *_v) {
__asm__ __volatile__(
GASNETI_ASM_USED(1)
GASNETI_X86_LOCK_PREFIX
"decl %0"
: "=m" (_v->gasneti_ctr)
: "m" (_v->gasneti_ctr)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
}
#define _gasneti_atomic32_decrement _gasneti_atomic32_decrement
GASNETI_INLINE(_gasneti_atomic32_decrement_and_test)
int _gasneti_atomic32_decrement_and_test(gasneti_atomic32_t *_v) {
#if PLATFORM_COMPILER_SUN
unsigned char _retval;
#else
GASNETI_ASM_REGISTER_KEYWORD unsigned char _retval;
#endif
__asm__ __volatile__(
GASNETI_ASM_USED(2)
GASNETI_X86_LOCK_PREFIX
"decl %0 \n\t"
"sete %1"
: "=m" (_v->gasneti_ctr), "=qm" (_retval)
: "m" (_v->gasneti_ctr)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
#if GASNETI_PGI_ASM_BUG1754
return _retval & 0xFF;
#else
return _retval;
#endif
}
#define _gasneti_atomic32_decrement_and_test _gasneti_atomic32_decrement_and_test
GASNETI_INLINE(_gasneti_atomic32_compare_and_swap)
int _gasneti_atomic32_compare_and_swap(gasneti_atomic32_t *_v, uint32_t _oldval, uint32_t _newval) {
#if PLATFORM_COMPILER_SUN
unsigned char _retval;
#else
GASNETI_ASM_REGISTER_KEYWORD unsigned char _retval;
#endif
GASNETI_ASM_REGISTER_KEYWORD uint32_t _readval;
__asm__ __volatile__ (
GASNETI_ASM_USED(4)
GASNETI_X86_LOCK_PREFIX
"cmpxchgl %3, %1 \n\t"
#if GASNETI_PGI_ASM_BUG2294 /* Sensitive to output constraint order */
"sete %2"
: "=a" (_readval), "=m" (_v->gasneti_ctr), "=qm" (_retval)
#else /* The version that has always worked everywhere else */
"sete %0"
: "=qm" (_retval), "=m" (_v->gasneti_ctr), "=a" (_readval)
#endif
: "r" (_newval), "m" (_v->gasneti_ctr), "a" (_oldval)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
#if GASNETI_PGI_ASM_BUG1754
return _retval & 0xFF;
#else
return _retval;
#endif
}
#define _gasneti_atomic32_compare_and_swap _gasneti_atomic32_compare_and_swap
GASNETI_INLINE(_gasneti_atomic32_fetchadd)
uint32_t _gasneti_atomic32_fetchadd(gasneti_atomic32_t *_v, uint32_t _op) {
/* CAUTION: Both PathScale and Intel compilers have been seen to be
* rather fragile with respect to this asm template (bug 1563).
* Change this at your own risk!
*/
uint32_t _retval = _op;
__asm__ __volatile__(
GASNETI_ASM_USED(3)
GASNETI_X86_LOCK_PREFIX
"xaddl %0, %1"
: "=&r" (_retval), "=m" (_v->gasneti_ctr)
: "0" (_retval), "m" (_v->gasneti_ctr)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return _retval;
}
#define _gasneti_atomic32_fetchadd _gasneti_atomic32_fetchadd
/* 64-bit differ between x86 and x86-64: */
#if GASNETI_ATOMIC64_NOINLINE
// Using SLOW or GENERIC alternative
#elif PLATFORM_ARCH_64
#if GASNETI_PGI_ASM_BUG3674
#define _gasneti_atomic64_read(p) (*(uint64_t volatile *)&((p)->gasneti_ctr))
#define _gasneti_atomic64_set(p,v) (*(uint64_t volatile *)&((p)->gasneti_ctr) = (v))
#else
#define _gasneti_atomic64_read(p) ((p)->gasneti_ctr)
#define _gasneti_atomic64_set(p,v) ((p)->gasneti_ctr = (v))
#endif
GASNETI_INLINE(_gasneti_atomic64_compare_and_swap)
int _gasneti_atomic64_compare_and_swap(gasneti_atomic64_t *_p, uint64_t _oldval, uint64_t _newval) {
#if GASNETI_PGI_ASM_BUG2843 && GASNET_NDEBUG
#pragma routine opt 2 /* Bug 2843 - pgcc miscompiles this code at -O1, so force -O2 */
#endif
GASNETI_ASM_REGISTER_KEYWORD unsigned char _retval;
GASNETI_ASM_REGISTER_KEYWORD uint64_t _readval = _oldval;
__asm__ __volatile__ (
GASNETI_ASM_USED(4)
GASNETI_X86_LOCK_PREFIX
"cmpxchgq %3, %1 \n\t"
"sete %0"
: "=q" (_retval), "=m" (_p->gasneti_ctr), "=a" (_readval)
: "r" (_newval), "m" (_p->gasneti_ctr), "a" (_oldval)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
#if GASNETI_PGI_ASM_BUG1754
return _retval & 0xFF;
#else
return _retval;
#endif
}
#define _gasneti_atomic64_compare_and_swap _gasneti_atomic64_compare_and_swap
GASNETI_INLINE(_gasneti_atomic64_swap)
uint64_t _gasneti_atomic64_swap(gasneti_atomic64_t *_v, uint64_t _value) {
#if GASNETI_PGI_ASM_BUG2843 && GASNET_DEBUG
#pragma routine opt 1 /* pgcc miscompiles this code at -O0, so force -O1 */
#endif
GASNETI_ASM_REGISTER_KEYWORD uint64_t _retval;
__asm__ __volatile__(
GASNETI_ASM_USED(2)
GASNETI_X86_LOCK_PREFIX /* 'lock' is implied, but is the fence? */
"xchgq %0, %1"
: "=r" (_retval), "=m" (_v->gasneti_ctr)
: "m" (_v->gasneti_ctr), "0" (_value)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return _retval;
}
#define _gasneti_atomic64_swap _gasneti_atomic64_swap
GASNETI_INLINE(_gasneti_atomic64_fetchadd)
uint64_t _gasneti_atomic64_fetchadd(gasneti_atomic64_t *_v, uint64_t _op) {
/* CAUTION: see atomic32_fetchadd for note about PathScale and Intel compilers */
#if GASNETI_PGI_ASM_BUG2843 && GASNET_DEBUG
#pragma routine opt 1 /* pgcc miscompiles this code at -O0, so force -O1 */
#endif
GASNETI_ASM_REGISTER_KEYWORD uint64_t _retval;
__asm__ __volatile__(
GASNETI_ASM_USED(2)
GASNETI_X86_LOCK_PREFIX
"xaddq %0, %1"
: "=r" (_retval), "=m" (_v->gasneti_ctr)
: "m" (_v->gasneti_ctr), "0" (_op)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return _retval;
}
#define _gasneti_atomic64_fetchadd _gasneti_atomic64_fetchadd
#elif PLATFORM_COMPILER_OPEN64
/* No known working 64-bit atomics for this compiler on ILP32. See bug 2725. */
#elif GASNETI_USE_X86_EBX && \
!(__APPLE_CC__ && defined(__llvm__)) /* bug 3071 */
/* "Normal" ILP32 case:
*
* To perform read and set atomically on x86 requires use of the locked
* 8-byte c-a-s instruction. This is the only atomic 64-bit operation
* available on this architecture. Note that we need the lock prefix
* even on a uniprocessor to ensure that we are signal safe.
*
* See the following #elif/#else clauses for slight variants.
*/
GASNETI_INLINE(_gasneti_atomic64_compare_and_swap)
int _gasneti_atomic64_compare_and_swap(gasneti_atomic64_t *_p, uint64_t _oldval, uint64_t _newval) {
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_newval);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_newval);
__asm__ __volatile__ (
"lock; "
"cmpxchg8b %0 \n\t"
"sete %b1 "
: "=m" (_p->gasneti_ctr), "+&A" (_oldval)
: "m" (_p->gasneti_ctr), "b" (_newlo), "c" (_newhi)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return (uint8_t)_oldval;
}
#define _gasneti_atomic64_compare_and_swap _gasneti_atomic64_compare_and_swap
GASNETI_INLINE(_gasneti_atomic64_swap)
uint64_t _gasneti_atomic64_swap(gasneti_atomic64_t *_p, uint64_t _v) {
GASNETI_ASM_REGISTER_KEYWORD uint64_t _oldval = _p->gasneti_ctr;
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_v);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_v);
_GASNETI_ATOMIC_CHECKALIGN(gasneti_atomic64_align, _p);
__asm__ __volatile__ (
"0: \n\t"
"lock; "
"cmpxchg8b %0 \n\t"
"jnz 0b \n\t"
: "=m" (_p->gasneti_ctr), "+&A" (_oldval)
: "m" (_p->gasneti_ctr), "b" (_newlo), "c" (_newhi)
: "cc", "memory");
return _oldval;
}
#define _gasneti_atomic64_swap _gasneti_atomic64_swap
#define _gasneti_atomic64_set(p,v) ((void)_gasneti_atomic64_swap(p,v))
GASNETI_INLINE(_gasneti_atomic64_read)
uint64_t _gasneti_atomic64_read(gasneti_atomic64_t *_p) {
GASNETI_ASM_REGISTER_KEYWORD uint64_t _retval;
_GASNETI_ATOMIC_CHECKALIGN(gasneti_atomic64_align, _p);
__asm__ __volatile__ (
/* Set [a:d] = [b:c], thus preserving b and c */
"movl %%ebx, %%eax \n\t"
"movl %%ecx, %%edx \n\t"
"lock; "
"cmpxchg8b %0 "
: "=m" (_p->gasneti_ctr), "=&A" (_retval)
: "m" (_p->gasneti_ctr)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return _retval;
}
#define _gasneti_atomic64_read _gasneti_atomic64_read
GASNETI_INLINE(_gasneti_atomic64_cas_val) /* for 64-bit FETCHADD */
uint64_t _gasneti_atomic64_cas_val(gasneti_atomic64_t *_p, uint64_t _oldval, uint64_t _newval) {
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_newval);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_newval);
__asm__ __volatile__ (
GASNETI_ASM_USED(2)
"lock; "
"cmpxchg8b %0 "
: "=m" (_p->gasneti_ctr), "+&A" (_oldval)
: "m" (_p->gasneti_ctr), "b" (_newlo), "c" (_newhi)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return _oldval;
}
#define _gasneti_atomic64_cas_val _gasneti_atomic64_cas_val
#elif (__APPLE_CC__ && defined(__llvm__)) /* bug 3071 */
/* "Normal" ILP32 case except w/o "m" inputs or outputs to CAS and Set.
* Such operands lead to "Ran out of registers during register allocation!"
* Instead a "memory" clobber is used.
* Read is identical to the Normal case.
*/
GASNETI_INLINE(_gasneti_atomic64_compare_and_swap)
int _gasneti_atomic64_compare_and_swap(gasneti_atomic64_t *_p, uint64_t _oldval, uint64_t _newval) {
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_newval);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_newval);
__asm__ __volatile__ (
"lock; "
"cmpxchg8b (%1) \n\t"
"sete %b0 "
: "+&A" (_oldval)
: "r" (&_p->gasneti_ctr), "b" (_newlo), "c" (_newhi)
: "cc", "memory");
return (uint8_t)_oldval;
}
#define _gasneti_atomic64_compare_and_swap _gasneti_atomic64_compare_and_swap
GASNETI_INLINE(_gasneti_atomic64_swap)
uint64_t _gasneti_atomic64_swap(gasneti_atomic64_t *_p, uint64_t _v) {
GASNETI_ASM_REGISTER_KEYWORD uint64_t _oldval = _p->gasneti_ctr;
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_v);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_v);
_GASNETI_ATOMIC_CHECKALIGN(gasneti_atomic64_align, _p);
__asm__ __volatile__ (
GASNETI_ASM_USED(2)
"0: \n\t"
"lock; "
"cmpxchg8b (%1) \n\t"
"jnz 0b "
: "+&A" (_oldval)
: "r" (&_p->gasneti_ctr), "b" (_newlo), "c" (_newhi)
: "cc", "memory");
return _oldval;
}
#define _gasneti_atomic64_swap _gasneti_atomic64_swap
#define _gasneti_atomic64_set(p,v) ((void)_gasneti_atomic64_swap(p,v))
GASNETI_INLINE(_gasneti_atomic64_read)
uint64_t _gasneti_atomic64_read(gasneti_atomic64_t *_p) {
GASNETI_ASM_REGISTER_KEYWORD uint64_t _retval;
_GASNETI_ATOMIC_CHECKALIGN(gasneti_atomic64_align, _p);
__asm__ __volatile__ (
/* Set [a:d] = [b:c], thus preserving b and c */
"movl %%ebx, %%eax \n\t"
"movl %%ecx, %%edx \n\t"
"lock; "
"cmpxchg8b %0 "
: "=m" (_p->gasneti_ctr), "=&A" (_retval)
: "m" (_p->gasneti_ctr)
: "cc" GASNETI_ATOMIC_MEM_CLOBBER);
return _retval;
}
#define _gasneti_atomic64_read _gasneti_atomic64_read
GASNETI_INLINE(_gasneti_atomic64_cas_val) /* for 64-bit FETCHADD */
uint64_t _gasneti_atomic64_cas_val(gasneti_atomic64_t *_p, uint64_t _oldval, uint64_t _newval) {
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_newval);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_newval);
__asm__ __volatile__ (
"lock; "
"cmpxchg8b (%1) "
: "+&A" (_oldval)
: "r" (&_p->gasneti_ctr), "b" (_newlo), "c" (_newhi)
: "cc", "memory");
return _oldval;
}
#define _gasneti_atomic64_cas_val _gasneti_atomic64_cas_val
#elif !GASNETI_USE_X86_EBX
/* Much the same as the "normal" ILP32 case, but w/ save and restore of EBX.
* This is achieved by passing the "other" 64-bit value in ECX and a second
* register of the compiler's choosing, which is then swapped w/ EBX.
*
* We also need to take care that the cmpxchg8b intruction won't get a
* GOT-relative address argument - since EBX doesn't hold the GOT pointer
* at the time it is executed. This is done by loading the address into
* an available register (but not EBX) rather than giving it as an "m".
*
* Alas, if we try to add an "m" output for the target location, gcc thinks
* it needs to allocate another register for it. Having none left, it gives
* up at this point. So, we need to list "memory" in the clobbers instead.
*/
GASNETI_INLINE(_gasneti_atomic64_compare_and_swap)
int _gasneti_atomic64_compare_and_swap(gasneti_atomic64_t *_p, uint64_t _oldval, uint64_t _newval) {
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newlo = GASNETI_LOWORD(_newval);
GASNETI_ASM_REGISTER_KEYWORD uint32_t _newhi = GASNETI_HIWORD(_newval);
__asm__ __volatile__ (
"xchgl %1, %%ebx \n\t"
"lock; "
"cmpxchg8b (%3) \n\t"
"sete %b0 \n\t"