forked from GerHobbelt/pthread-win32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catomic.h
2874 lines (2419 loc) · 146 KB
/
catomic.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
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
/*
C89 compatible atomics. Choice of public domain or MIT-0. See license statements at the end of this file.
David Reid - [email protected]
*/
#ifndef C_ATOMIC_H
#define C_ATOMIC_H
#include <stdint.h>
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wlong-long"
#if defined(__clang__)
#pragma GCC diagnostic ignored "-Wc++11-long-long"
#endif
#endif
#ifndef __ATOMIC_RELAXED
# define __ATOMIC_RELAXED 0
# define __ATOMIC_CONSUME 1
# define __ATOMIC_ACQUIRE 2
# define __ATOMIC_RELEASE 3
# define __ATOMIC_ACQ_REL 4
# define __ATOMIC_SEQ_CST 5
#endif
#ifndef _STDATOMIC_H
/* Memory ordering */
typedef enum {
memory_order_relaxed = __ATOMIC_RELAXED,
memory_order_consume = __ATOMIC_CONSUME,
memory_order_acquire = __ATOMIC_ACQUIRE,
memory_order_release = __ATOMIC_RELEASE,
memory_order_acq_rel = __ATOMIC_ACQ_REL,
memory_order_seq_cst = __ATOMIC_SEQ_CST,
} memory_order;
#endif
typedef int c89atomic_memory_order;
/* Sized Types */
typedef signed char c89atomic_int8;
typedef unsigned char c89atomic_uint8;
typedef signed short c89atomic_int16;
typedef unsigned short c89atomic_uint16;
typedef signed int c89atomic_int32;
typedef unsigned int c89atomic_uint32;
#if defined(_MSC_VER) && !defined(__clang__)
typedef signed __int64 c89atomic_int64;
typedef unsigned __int64 c89atomic_uint64;
#else
typedef signed long long c89atomic_int64;
typedef unsigned long long c89atomic_uint64;
#endif
typedef unsigned char c89atomic_bool;
/* End Sized Types */
#if defined(_WIN32) || defined(__APPLE__)
# define make_atomic(type, var) typedef volatile type var;
#else
# define make_atomic(type, var) typedef volatile _Atomic(type)var;
#endif
#ifndef _STDATOMIC_H
make_atomic(c89atomic_bool, atomic_bool)
make_atomic(c89atomic_int8, atomic_char)
make_atomic(c89atomic_int8, atomic_schar)
make_atomic(c89atomic_uint8, atomic_uchar)
make_atomic(c89atomic_int16, atomic_short)
make_atomic(c89atomic_uint16, atomic_ushort)
make_atomic(c89atomic_int32, atomic_int)
make_atomic(c89atomic_uint32, atomic_uint)
make_atomic(signed long, atomic_long)
make_atomic(unsigned long, atomic_ulong)
make_atomic(c89atomic_int64, atomic_llong)
make_atomic(c89atomic_uint64, atomic_ullong)
make_atomic(intptr_t, atomic_intptr_t)
make_atomic(uintptr_t, atomic_uintptr_t)
make_atomic(intmax_t, atomic_intmax_t)
make_atomic(uintmax_t, atomic_uintmax_t)
#endif
#if !defined(_STDATOMIC_H) && (defined(_WIN32) || defined(__APPLE__))
make_atomic(size_t, atomic_size_t)
make_atomic(ptrdiff_t, atomic_ptrdiff_t)
#elif !defined(_STDATOMIC_H)
make_atomic(__SIZE_TYPE__, atomic_size_t)
make_atomic(__PTRDIFF_TYPE__, atomic_ptrdiff_t)
#endif
make_atomic(void *, atomic_ptr_t)
#if defined(__TINYC__) || defined(_MSC_VER)
# define c89atomic_is_lock_free(obj) (sizeof((obj)) <= sizeof(void *))
#endif
/* Architecture Detection */
#if !defined(C89ATOMIC_64BIT) && !defined(C89ATOMIC_32BIT)
#ifdef _WIN32
#ifdef _WIN64
#define C89ATOMIC_64BIT
#else
#define C89ATOMIC_32BIT
#endif
#endif
#endif
#if !defined(C89ATOMIC_64BIT) && !defined(C89ATOMIC_32BIT)
#ifdef __GNUC__
#ifdef __LP64__
#define C89ATOMIC_64BIT
#else
#define C89ATOMIC_32BIT
#endif
#endif
#endif
#if !defined(C89ATOMIC_64BIT) && !defined(C89ATOMIC_32BIT)
#if INTPTR_MAX == INT64_MAX
#define C89ATOMIC_64BIT
#else
#define C89ATOMIC_32BIT
#endif
#endif
#if defined(__arm__) || defined(_M_ARM)
#define C89ATOMIC_ARM32
#endif
#if defined(__arm64) || defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
#define C89ATOMIC_ARM64
#endif
#if defined(__x86_64__) || defined(_M_X64)
#define C89ATOMIC_X64
#elif defined(__i386) || defined(_M_IX86)
#define C89ATOMIC_X86
#elif defined(C89ATOMIC_ARM32) || defined(C89ATOMIC_ARM64)
#define C89ATOMIC_ARM
#endif
/* End Architecture Detection */
/* Inline */
/* We want to encourage the compiler to inline. When adding support for a new compiler, make sure it's handled here. */
#if defined(_MSC_VER)
# define C89ATOMIC_INLINE __forceinline
#elif defined(__GNUC__)
/*
I've had a bug report where GCC is emitting warnings about functions possibly not being inlineable. This warning happens when
the __attribute__((always_inline)) attribute is defined without an "inline" statement. I think therefore there must be some
case where "__inline__" is not always defined, thus the compiler emitting these warnings. When using -std=c89 or -ansi on the
command line, we cannot use the "inline" keyword and instead need to use "__inline__". In an attempt to work around this issue
I am using "__inline__" only when we're compiling in strict ANSI mode.
*/
#if defined(__STRICT_ANSI__) || !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
#define C89ATOMIC_INLINE __inline__ __attribute__((always_inline))
#else
#define C89ATOMIC_INLINE inline __attribute__((always_inline))
#endif
#elif defined(__WATCOMC__) || defined(__DMC__)
#define C89ATOMIC_INLINE __inline
#else
#define C89ATOMIC_INLINE
#endif
/* End Inline */
/* Assume everything supports all standard sized atomics by default. We'll #undef these when not supported. */
#define C89ATOMIC_HAS_8
#define C89ATOMIC_HAS_16
#define C89ATOMIC_HAS_32
#define C89ATOMIC_HAS_64
#if (defined(_MSC_VER) /*&& !defined(__clang__)*/) || defined(__WATCOMC__) || defined(__DMC__)
/* Visual C++. */
#define C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, intrin, c89atomicType, msvcType) \
c89atomicType result; \
switch (order) \
{ \
case c89atomic_memory_order_relaxed: \
{ \
result = (c89atomicType)intrin##_nf((volatile msvcType*)dst, (msvcType)src); \
} break; \
case c89atomic_memory_order_consume: \
case c89atomic_memory_order_acquire: \
{ \
result = (c89atomicType)intrin##_acq((volatile msvcType*)dst, (msvcType)src); \
} break; \
case c89atomic_memory_order_release: \
{ \
result = (c89atomicType)intrin##_rel((volatile msvcType*)dst, (msvcType)src); \
} break; \
case c89atomic_memory_order_acq_rel: \
case c89atomic_memory_order_seq_cst: \
default: \
{ \
result = (c89atomicType)intrin((volatile msvcType*)dst, (msvcType)src); \
} break; \
} \
return result;
#define C89ATOMIC_MSVC_ARM_INTRINSIC_COMPARE_EXCHANGE(ptr, expected, desired, order, intrin, c89atomicType, msvcType) \
c89atomicType result; \
switch (order) \
{ \
case c89atomic_memory_order_relaxed: \
{ \
result = (c89atomicType)intrin##_nf((volatile msvcType*)ptr, (msvcType)expected, (msvcType)desired); \
} break; \
case c89atomic_memory_order_consume: \
case c89atomic_memory_order_acquire: \
{ \
result = (c89atomicType)intrin##_acq((volatile msvcType*)ptr, (msvcType)expected, (msvcType)desired); \
} break; \
case c89atomic_memory_order_release: \
{ \
result = (c89atomicType)intrin##_rel((volatile msvcType*)ptr, (msvcType)expected, (msvcType)desired); \
} break; \
case c89atomic_memory_order_acq_rel: \
case c89atomic_memory_order_seq_cst: \
default: \
{ \
result = (c89atomicType)intrin((volatile msvcType*)ptr, (msvcType)expected, (msvcType)desired); \
} break; \
} \
return result;
#define c89atomic_memory_order_relaxed 0
#define c89atomic_memory_order_consume 1
#define c89atomic_memory_order_acquire 2
#define c89atomic_memory_order_release 3
#define c89atomic_memory_order_acq_rel 4
#define c89atomic_memory_order_seq_cst 5
/*
Visual Studio 2003 (_MSC_VER 1300) and earlier have no support for sized atomic operations.
We'll need to use inlined assembly for these compilers.
I've also had a report that 8-bit and 16-bit interlocked intrinsics were only added in Visual
Studio 2010 (_MSC_VER 1600). We'll need to disable these on the 64-bit build because there's
no way to implement them with inlined assembly since Microsoft has decided to drop support for
it from their 64-bit compilers.
To simplify the implementation, any older MSVC compilers targeting 32-bit will use inlined
assembly for everything. I'm not going to use inlined assembly wholesale for all 32-bit builds
regardless of the age of the compiler because I don't trust the compiler will optimize the
inlined assembly properly.
The inlined assembly path supports both MSVC, Digital Mars and OpenWatcom. OpenWatcom is a little
bit too pedantic with it's warnings. A few notes:
- The return value of these functions are defined by the AL/AX/EAX/EAX:EDX registers which
means an explicit return statement is not actually necessary. This is helpful for performance
reasons because it means we can avoid the cost of a declaring a local variable and moving the
value in EAX into that variable, only to then return it. However, unfortunately OpenWatcom
thinks this is a mistake and tries to be helpful by throwing a warning. To work around we're
going to declare a "result" variable and incur this theoretical cost. Most likely the
compiler will optimize this away and make it a non-issue.
- Variables that are assigned within the inline assembly will not be detected as such, and
OpenWatcom will throw a warning about the variable being used without being assigned. To work
around this we just initialize our local variables to 0.
*/
#if _MSC_VER < 1600 && defined(C89ATOMIC_X86) /* 1600 = Visual Studio 2010 */
#define C89ATOMIC_MSVC_USE_INLINED_ASSEMBLY
#endif
#if _MSC_VER < 1600
#undef C89ATOMIC_HAS_8
#undef C89ATOMIC_HAS_16
#endif
/* We need <intrin.h>, but only if we're not using inlined assembly. */
#if !defined(C89ATOMIC_MSVC_USE_INLINED_ASSEMBLY)
#include <intrin.h>
#endif
/* atomic_compare_and_swap */
#if defined(C89ATOMIC_MSVC_USE_INLINED_ASSEMBLY)
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_compare_and_swap_8(volatile c89atomic_uint8* dst, c89atomic_uint8 expected, c89atomic_uint8 desired)
{
c89atomic_uint8 result = 0;
__asm {
mov ecx, dst
mov al, expected
mov dl, desired
lock cmpxchg [ecx], dl /* Writes to EAX which MSVC will treat as the return value. */
mov result, al
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_compare_and_swap_16(volatile c89atomic_uint16* dst, c89atomic_uint16 expected, c89atomic_uint16 desired)
{
c89atomic_uint16 result = 0;
__asm {
mov ecx, dst
mov ax, expected
mov dx, desired
lock cmpxchg [ecx], dx /* Writes to EAX which MSVC will treat as the return value. */
mov result, ax
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_compare_and_swap_32(volatile c89atomic_uint32* dst, c89atomic_uint32 expected, c89atomic_uint32 desired)
{
c89atomic_uint32 result = 0;
__asm {
mov ecx, dst
mov eax, expected
mov edx, desired
lock cmpxchg [ecx], edx /* Writes to EAX which MSVC will treat as the return value. */
mov result, eax
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_64)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_compare_and_swap_64(volatile c89atomic_uint64* dst, c89atomic_uint64 expected, c89atomic_uint64 desired)
{
c89atomic_uint32 resultEAX = 0;
c89atomic_uint32 resultEDX = 0;
__asm {
mov esi, dst /* From Microsoft documentation: "... you don't need to preserve the EAX, EBX, ECX, EDX, ESI, or EDI registers." Choosing ESI since it's the next available one in their list. */
mov eax, dword ptr expected
mov edx, dword ptr expected + 4
mov ebx, dword ptr desired
mov ecx, dword ptr desired + 4
lock cmpxchg8b qword ptr [esi] /* Writes to EAX:EDX which MSVC will treat as the return value. */
mov resultEAX, eax
mov resultEDX, edx
}
return ((c89atomic_uint64)resultEDX << 32) | resultEAX;
}
#endif
#else
#if defined(C89ATOMIC_HAS_8)
#define c89atomic_compare_and_swap_8( dst, expected, desired) (c89atomic_uint8 )_InterlockedCompareExchange8((volatile char*)dst, (char)desired, (char)expected)
#endif
#if defined(C89ATOMIC_HAS_16)
#define c89atomic_compare_and_swap_16(dst, expected, desired) (c89atomic_uint16)_InterlockedCompareExchange16((volatile short*)dst, (short)desired, (short)expected)
#endif
#if defined(C89ATOMIC_HAS_32)
#define c89atomic_compare_and_swap_32(dst, expected, desired) (c89atomic_uint32)_InterlockedCompareExchange((volatile long*)dst, (long)desired, (long)expected)
#endif
#if defined(C89ATOMIC_HAS_64)
#define c89atomic_compare_and_swap_64(dst, expected, desired) (c89atomic_uint64)_InterlockedCompareExchange64((volatile c89atomic_int64*)dst, (c89atomic_int64)desired, (c89atomic_int64)expected)
#endif
#endif
/* atomic_exchange_explicit */
#if defined(C89ATOMIC_MSVC_USE_INLINED_ASSEMBLY)
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_exchange_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
c89atomic_uint8 result = 0;
(void)order;
__asm {
mov ecx, dst
mov al, src
lock xchg [ecx], al
mov result, al
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_exchange_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
c89atomic_uint16 result = 0;
(void)order;
__asm {
mov ecx, dst
mov ax, src
lock xchg [ecx], ax
mov result, ax
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_exchange_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
c89atomic_uint32 result = 0;
(void)order;
__asm {
mov ecx, dst
mov eax, src
lock xchg [ecx], eax
mov result, eax
}
return result;
}
#endif
#else
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_exchange_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchange8, c89atomic_uint8, char);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint8)_InterlockedExchange8((volatile char*)dst, (char)src);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_exchange_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchange16, c89atomic_uint16, short);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint16)_InterlockedExchange16((volatile short*)dst, (short)src);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_exchange_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchange, c89atomic_uint32, long);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint32)_InterlockedExchange((volatile long*)dst, (long)src);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_64) && defined(C89ATOMIC_64BIT)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_exchange_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchange64, c89atomic_uint64, long long);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint64)_InterlockedExchange64((volatile long long*)dst, (long long)src);
#endif
}
#else
/* Implemented below. */
#endif
#endif
#if defined(C89ATOMIC_HAS_64) && !defined(C89ATOMIC_64BIT) /* atomic_exchange_explicit_64() must be implemented in terms of atomic_compare_and_swap() on 32-bit builds, no matter the version of Visual Studio. */
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_exchange_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
c89atomic_uint64 oldValue;
do {
oldValue = *dst;
} while (c89atomic_compare_and_swap_64(dst, oldValue, src) != oldValue);
(void)order; /* Always using the strongest memory order. */
return oldValue;
}
#endif
/* atomic_fetch_add */
#if defined(C89ATOMIC_MSVC_USE_INLINED_ASSEMBLY)
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_fetch_add_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
c89atomic_uint8 result = 0;
(void)order;
__asm {
mov ecx, dst
mov al, src
lock xadd [ecx], al
mov result, al
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_fetch_add_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
c89atomic_uint16 result = 0;
(void)order;
__asm {
mov ecx, dst
mov ax, src
lock xadd [ecx], ax
mov result, ax
}
return result;
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_fetch_add_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
c89atomic_uint32 result = 0;
(void)order;
__asm {
mov ecx, dst
mov eax, src
lock xadd [ecx], eax
mov result, eax
}
return result;
}
#endif
#else
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_fetch_add_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchangeAdd8, c89atomic_uint8, char);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint8)_InterlockedExchangeAdd8((volatile char*)dst, (char)src);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_fetch_add_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchangeAdd16, c89atomic_uint16, short);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint16)_InterlockedExchangeAdd16((volatile short*)dst, (short)src);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_fetch_add_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchangeAdd, c89atomic_uint32, long);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint32)_InterlockedExchangeAdd((volatile long*)dst, (long)src);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_64) && defined(C89ATOMIC_64BIT)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_fetch_add_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedExchangeAdd64, c89atomic_uint64, long long);
#else
(void)order; /* Always using the strongest memory order. */
return (c89atomic_uint64)_InterlockedExchangeAdd64((volatile long long*)dst, (long long)src);
#endif
}
#else
/* Implemented below. */
#endif
#endif
#if defined(C89ATOMIC_HAS_64) && !defined(C89ATOMIC_64BIT) /* 9atomic_fetch_add_explicit_64() must be implemented in terms of atomic_compare_and_swap() on 32-bit builds, no matter the version of Visual Studio. */
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_fetch_add_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
c89atomic_uint64 oldValue;
c89atomic_uint64 newValue;
do {
oldValue = *dst;
newValue = oldValue + src;
} while (c89atomic_compare_and_swap_64(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
}
#endif
/* atomic_thread_fence */
#if defined(C89ATOMIC_MSVC_USE_INLINED_ASSEMBLY)
static C89ATOMIC_INLINE void __stdcall c89atomic_thread_fence(c89atomic_memory_order order)
{
(void)order;
__asm {
lock add [esp], 0
}
}
#else
#define c89atomic_thread_fence(order) MemoryBarrier()
#endif
/*
I'm not sure how to implement a compiler barrier for old MSVC so I'm just making it a thread_fence() and hopefully the compiler will see the volatile and not do
any reshuffling. If anybody has a better idea on this please let me know! Cannot use _ReadWriteBarrier() as it has been marked as deprecated.
*/
#define c89atomic_compiler_fence() c89atomic_thread_fence(c89atomic_memory_order_seq_cst)
#define c89atomic_signal_fence(order) _ReadWriteBarrier()
/* Atomic loads can be implemented in terms of a compare-and-swap. Need to implement as functions to silence warnings about `order` being unused. */
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 c89atomic_load_explicit_8(volatile const c89atomic_uint8* ptr, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC_COMPARE_EXCHANGE(ptr, 0, 0, order, _InterlockedCompareExchange8, c89atomic_uint8, char);
#else
(void)order; /* Always using the strongest memory order. */
return c89atomic_compare_and_swap_8((volatile c89atomic_uint8*)ptr, 0, 0);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 c89atomic_load_explicit_16(volatile const c89atomic_uint16* ptr, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC_COMPARE_EXCHANGE(ptr, 0, 0, order, _InterlockedCompareExchange16, c89atomic_uint16, short);
#else
(void)order; /* Always using the strongest memory order. */
return c89atomic_compare_and_swap_16((volatile c89atomic_uint16*)ptr, 0, 0);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 c89atomic_load_explicit_32(volatile const c89atomic_uint32* ptr, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC_COMPARE_EXCHANGE(ptr, 0, 0, order, _InterlockedCompareExchange, c89atomic_uint32, long);
#else
(void)order; /* Always using the strongest memory order. */
return c89atomic_compare_and_swap_32((volatile c89atomic_uint32*)ptr, 0, 0);
#endif
}
#endif
#if defined(C89ATOMIC_HAS_64)
static C89ATOMIC_INLINE c89atomic_uint64 c89atomic_load_explicit_64(volatile const c89atomic_uint64* ptr, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC_COMPARE_EXCHANGE(ptr, 0, 0, order, _InterlockedCompareExchange64, c89atomic_uint64, long long);
#else
(void)order; /* Always using the strongest memory order. */
return c89atomic_compare_and_swap_64((volatile c89atomic_uint64*)ptr, 0, 0);
#endif
}
#endif
/* atomic_store() is the same as atomic_exchange() but returns void. */
#if defined(C89ATOMIC_HAS_8)
#define c89atomic_store_explicit_8( dst, src, order) (void)c89atomic_exchange_explicit_8 (dst, src, order)
#endif
#if defined(C89ATOMIC_HAS_16)
#define c89atomic_store_explicit_16(dst, src, order) (void)c89atomic_exchange_explicit_16(dst, src, order)
#endif
#if defined(C89ATOMIC_HAS_32)
#define c89atomic_store_explicit_32(dst, src, order) (void)c89atomic_exchange_explicit_32(dst, src, order)
#endif
#if defined(C89ATOMIC_HAS_64)
#define c89atomic_store_explicit_64(dst, src, order) (void)c89atomic_exchange_explicit_64(dst, src, order)
#endif
/* fetch_sub() */
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_fetch_sub_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
c89atomic_uint8 oldValue;
c89atomic_uint8 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint8)(oldValue - src);
} while (c89atomic_compare_and_swap_8(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_fetch_sub_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
c89atomic_uint16 oldValue;
c89atomic_uint16 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint16)(oldValue - src);
} while (c89atomic_compare_and_swap_16(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_fetch_sub_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
c89atomic_uint32 oldValue;
c89atomic_uint32 newValue;
do {
oldValue = *dst;
newValue = oldValue - src;
} while (c89atomic_compare_and_swap_32(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
}
#endif
#if defined(C89ATOMIC_HAS_64)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_fetch_sub_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
c89atomic_uint64 oldValue;
c89atomic_uint64 newValue;
do {
oldValue = *dst;
newValue = oldValue - src;
} while (c89atomic_compare_and_swap_64(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
}
#endif
/* fetch_and() */
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_fetch_and_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedAnd8, c89atomic_uint8, char);
#else
c89atomic_uint8 oldValue;
c89atomic_uint8 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint8)(oldValue & src);
} while (c89atomic_compare_and_swap_8(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_fetch_and_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedAnd16, c89atomic_uint16, short);
#else
c89atomic_uint16 oldValue;
c89atomic_uint16 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint16)(oldValue & src);
} while (c89atomic_compare_and_swap_16(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_fetch_and_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedAnd, c89atomic_uint32, long);
#else
c89atomic_uint32 oldValue;
c89atomic_uint32 newValue;
do {
oldValue = *dst;
newValue = oldValue & src;
} while (c89atomic_compare_and_swap_32(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_64)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_fetch_and_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedAnd64, c89atomic_uint64, long long);
#else
c89atomic_uint64 oldValue;
c89atomic_uint64 newValue;
do {
oldValue = *dst;
newValue = oldValue & src;
} while (c89atomic_compare_and_swap_64(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
/* fetch_xor() */
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_fetch_xor_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedXor8, c89atomic_uint8, char);
#else
c89atomic_uint8 oldValue;
c89atomic_uint8 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint8)(oldValue ^ src);
} while (c89atomic_compare_and_swap_8(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_fetch_xor_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedXor16, c89atomic_uint16, short);
#else
c89atomic_uint16 oldValue;
c89atomic_uint16 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint16)(oldValue ^ src);
} while (c89atomic_compare_and_swap_16(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_fetch_xor_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedXor, c89atomic_uint32, long);
#else
c89atomic_uint32 oldValue;
c89atomic_uint32 newValue;
do {
oldValue = *dst;
newValue = oldValue ^ src;
} while (c89atomic_compare_and_swap_32(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_64)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_fetch_xor_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedXor64, c89atomic_uint64, long long);
#else
c89atomic_uint64 oldValue;
c89atomic_uint64 newValue;
do {
oldValue = *dst;
newValue = oldValue ^ src;
} while (c89atomic_compare_and_swap_64(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
/* fetch_or() */
#if defined(C89ATOMIC_HAS_8)
static C89ATOMIC_INLINE c89atomic_uint8 __stdcall c89atomic_fetch_or_explicit_8(volatile c89atomic_uint8* dst, c89atomic_uint8 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedOr8, c89atomic_uint8, char);
#else
c89atomic_uint8 oldValue;
c89atomic_uint8 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint8)(oldValue | src);
} while (c89atomic_compare_and_swap_8(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_16)
static C89ATOMIC_INLINE c89atomic_uint16 __stdcall c89atomic_fetch_or_explicit_16(volatile c89atomic_uint16* dst, c89atomic_uint16 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedOr16, c89atomic_uint16, short);
#else
c89atomic_uint16 oldValue;
c89atomic_uint16 newValue;
do {
oldValue = *dst;
newValue = (c89atomic_uint16)(oldValue | src);
} while (c89atomic_compare_and_swap_16(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_32)
static C89ATOMIC_INLINE c89atomic_uint32 __stdcall c89atomic_fetch_or_explicit_32(volatile c89atomic_uint32* dst, c89atomic_uint32 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedOr, c89atomic_uint32, long);
#else
c89atomic_uint32 oldValue;
c89atomic_uint32 newValue;
do {
oldValue = *dst;
newValue = oldValue | src;
} while (c89atomic_compare_and_swap_32(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
#if defined(C89ATOMIC_HAS_64)
static C89ATOMIC_INLINE c89atomic_uint64 __stdcall c89atomic_fetch_or_explicit_64(volatile c89atomic_uint64* dst, c89atomic_uint64 src, c89atomic_memory_order order)
{
#if defined(C89ATOMIC_ARM)
C89ATOMIC_MSVC_ARM_INTRINSIC(dst, src, order, _InterlockedOr64, c89atomic_uint64, long long);
#else
c89atomic_uint64 oldValue;
c89atomic_uint64 newValue;
do {
oldValue = *dst;
newValue = oldValue | src;
} while (c89atomic_compare_and_swap_64(dst, oldValue, newValue) != oldValue);
(void)order;
return oldValue;
#endif
}
#endif
/* test_and_set() */
#if defined(C89ATOMIC_HAS_8)
#define c89atomic_test_and_set_explicit_8( dst, order) c89atomic_exchange_explicit_8 (dst, 1, order)
#endif
#if defined(C89ATOMIC_HAS_16)
#define c89atomic_test_and_set_explicit_16(dst, order) c89atomic_exchange_explicit_16(dst, 1, order)
#endif
#if defined(C89ATOMIC_HAS_32)
#define c89atomic_test_and_set_explicit_32(dst, order) c89atomic_exchange_explicit_32(dst, 1, order)
#endif
#if defined(C89ATOMIC_HAS_64)
#define c89atomic_test_and_set_explicit_64(dst, order) c89atomic_exchange_explicit_64(dst, 1, order)
#endif
/* clear() */
#if defined(C89ATOMIC_HAS_8)
#define c89atomic_clear_explicit_8( dst, order) c89atomic_store_explicit_8 (dst, 0, order)
#endif
#if defined(C89ATOMIC_HAS_16)
#define c89atomic_clear_explicit_16(dst, order) c89atomic_store_explicit_16(dst, 0, order)
#endif
#if defined(C89ATOMIC_HAS_32)
#define c89atomic_clear_explicit_32(dst, order) c89atomic_store_explicit_32(dst, 0, order)
#endif
#if defined(C89ATOMIC_HAS_64)
#define c89atomic_clear_explicit_64(dst, order) c89atomic_store_explicit_64(dst, 0, order)
#endif