Repository navigation
Expand file tree
/
Copy pathatomic_functions.h
More file actions
2809 lines (2219 loc) · 67.4 KB
/
Copy pathatomic_functions.h
File metadata and controls
2809 lines (2219 loc) · 67.4 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
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/synch/atomic_functions.h (originally MLAtomic.cpp, ::SynesisStd)
*
* Purpose: WinSTL atomic functions.
*
* Created: 23rd October 1997
* Updated: 31st December 2024
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2019-2024, Matthew Wilson and Synesis Information Systems
* Copyright (c) 1997-2019, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Information Systems
* nor the names of any contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file winstl/synch/atomic_functions.h
*
* \brief [C, C++] Definition of the atomic functions
* (\ref group__library__Synch "Synchronisation" Library).
*/
#ifndef WINSTL_INCL_WINSTL_SYNCH_H_ATOMIC_FUNCTIONS
#define WINSTL_INCL_WINSTL_SYNCH_H_ATOMIC_FUNCTIONS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_SYNCH_H_ATOMIC_FUNCTIONS_MAJOR 4
# define WINSTL_VER_WINSTL_SYNCH_H_ATOMIC_FUNCTIONS_MINOR 8
# define WINSTL_VER_WINSTL_SYNCH_H_ATOMIC_FUNCTIONS_REVISION 6
# define WINSTL_VER_WINSTL_SYNCH_H_ATOMIC_FUNCTIONS_EDIT 238
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifdef STLSOFT_TRACE_INCLUDE
# pragma message(__FILE__)
#endif /* STLSOFT_TRACE_INCLUDE */
#ifndef WINSTL_INCL_WINSTL_SYNCH_H_ATOMIC_TYPES
# include <winstl/synch/atomic_types.h>
#endif /* !WINSTL_INCL_WINSTL_SYNCH_H_ATOMIC_TYPES */
#ifndef WINSTL_INCL_WINSTL_API_external_h_Synchronization
# include <winstl/api/external/Synchronization.h>
#endif /* !WINSTL_INCL_WINSTL_API_external_h_Synchronization */
/* /////////////////////////////////////////////////////////////////////////
* compatibility
*/
#if 0
#elif defined(WINSTL_ARCH_IS_ARM64)
#elif defined(WINSTL_ARCH_IS_IA64)
#elif defined(WINSTL_ARCH_IS_X64)
#elif defined(WINSTL_ARCH_IS_X86)
#else
# error Not valid for processors other than ARM64, IA64, x64, x86
#endif /* arch */
#ifdef STLSOFT_ATOMIC_CALLCONV
# undef STLSOFT_ATOMIC_CALLCONV
#endif /* STLSOFT_ATOMIC_CALLCONV */
#ifdef WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL
# undef WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL
#endif /* WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL */
#ifdef WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL
# undef WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL
#endif /* WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL */
#ifndef STLSOFT_NO_FASTCALL
# if 0 ||\
defined(STLSOFT_COMPILER_IS_BORLAND) ||\
defined(STLSOFT_COMPILER_IS_DMC) ||\
defined(STLSOFT_COMPILER_IS_WATCOM) ||\
0
# define STLSOFT_NO_FASTCALL
# endif /* compiler */
#endif /* STLSOFT_NO_FASTCALL */
#if 0
#elif defined(WINSTL_ARCH_IS_X86)
# if 0
# elif defined(STLSOFT_CF_FASTCALL_SUPPORTED) && \
!defined(STLSOFT_NO_FASTCALL)
# define WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL
# define WINSTL_ATOMIC_FNS_CALLCONV __fastcall
# elif defined(STLSOFT_CF_STDCALL_SUPPORTED)
# define WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL
# define WINSTL_ATOMIC_FNS_CALLCONV __stdcall
# else
# error Need to define calling convention
# endif /* call-conv */
#elif 0 ||\
defined(WINSTL_ARCH_IS_ARM64) ||\
defined(WINSTL_ARCH_IS_IA64) ||\
defined(WINSTL_ARCH_IS_X64) ||\
0
# define WINSTL_ATOMIC_FNS_CALLCONV_IS_CDECL
# define WINSTL_ATOMIC_FNS_CALLCONV __cdecl
#else /* ? arch */
# error Only defined for the ARM64, IA64, x64, and x86 architectures
#endif /* arch */
/* /////////////////////////////////////////////////////////////////////////
* macros
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# if 1 &&\
!defined(_FILETIME_) &&\
!defined(EXCEPTION_POSSIBLE_DEADLOCK) &&\
!defined(FLAG_FILE_FIRST_PIPE_INSTANCE) &&\
!defined(COPY_FILE_ALLOW_DECRYPTED_DESTINATION) &&\
1
STLSOFT_INLINE
LONG*
winstl_C_internal_synch_atomic_ptrconv32_(
WINSTL_NS_QUAL(atomic_int32_t) volatile* pv
)
{
STLSOFT_STATIC_ASSERT(sizeof(LONG) == sizeof(WINSTL_NS_QUAL(atomic_int32_t)));
return STLSOFT_C_CAST(LONG*, STLSOFT_CONST_CAST(WINSTL_NS_QUAL(atomic_int32_t*), pv));
}
# if 0
# elif defined(WINSTL_OS_IS_WIN64)
STLSOFT_INLINE
LONGLONG*
winstl_C_internal_synch_atomic_ptrconv64_(
WINSTL_NS_QUAL(atomic_int64_t) volatile* pv
)
{
STLSOFT_STATIC_ASSERT(sizeof(LONGLONG) == sizeof(WINSTL_NS_QUAL(atomic_int64_t)));
return STLSOFT_C_CAST(LONGLONG*, STLSOFT_CONST_CAST(WINSTL_NS_QUAL(atomic_int64_t*), pv));
}
# endif
# else
STLSOFT_INLINE
LONG volatile*
winstl_C_internal_synch_atomic_ptrconv32_(
WINSTL_NS_QUAL(atomic_int32_t) volatile* pv
)
{
return STLSOFT_C_CAST(LONG volatile*, pv);
}
# if 0
# elif defined(WINSTL_OS_IS_WIN64)
STLSOFT_INLINE
LONGLONG volatile*
winstl_C_internal_synch_atomic_ptrconv64_(
WINSTL_NS_QUAL(atomic_int64_t) volatile* pv
)
{
STLSOFT_STATIC_ASSERT(sizeof(LONGLONG) == sizeof(WINSTL_NS_QUAL(atomic_int64_t)));
return STLSOFT_C_CAST(LONGLONG volatile*, pv);
}
# endif
# endif
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* namespace
*/
#ifndef WINSTL_NO_NAMESPACE
# if defined(STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */
namespace stlsoft
{
namespace winstl_project
{
# endif /* STLSOFT_NO_NAMESPACE */
#endif /* !WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* implementation options
*
* Because some compilers can make the code actually faster when the naked
* functions are not inline, we provide for that here. If you want to make
* out-of-line the functions, then you just need to define the symbol
* WINSTL_ATOMIC_FNS_DECLARATION_ONLY in the code that uses it, and define
* the symbol WINSTL_ATOMIC_FNS_DEFINITION in one implementation file.
*/
#ifdef WINSTL_ATOMIC_FNS_DECL_
# undef WINSTL_ATOMIC_FNS_DECL_
#endif /* WINSTL_ATOMIC_FNS_DECL_ */
#ifdef WINSTL_ATOMIC_FNS_IMPL_
# undef WINSTL_ATOMIC_FNS_IMPL_
#endif /* WINSTL_ATOMIC_FNS_IMPL_ */
#if 0
#elif defined(WINSTL_ATOMIC_FNS_DECLARATION_ONLY)
/* Only the function declarations are included */
# define WINSTL_ATOMIC_FNS_DECL_(type) type WINSTL_ATOMIC_FNS_CALLCONV
#elif defined(WINSTL_ATOMIC_FNS_DEFINITION)
/* Only the function definitions are included */
# ifdef STSLSOFT_INLINE_ASM_SUPPORTED
# define WINSTL_ATOMIC_FNS_IMPL_(type) __declspec(naked) type WINSTL_ATOMIC_FNS_CALLCONV
# else /* ? STSLSOFT_INLINE_ASM_SUPPORTED */
# define WINSTL_ATOMIC_FNS_IMPL_(type) type WINSTL_ATOMIC_FNS_CALLCONV
# endif /* STSLSOFT_INLINE_ASM_SUPPORTED */
#else /* ? declaration / definition */
# if defined(STLSOFT_COMPILER_IS_MWERKS) && \
(__MWERKS__ & 0xFF00) < 0x3000
# error CodeWarrior 7 and earlier does not generate correct code when inline naked functions are used
# endif /* compiler */
#if !defined(__cplusplus) && \
defined(STSLSOFT_INLINE_ASM_SUPPORTED)
/* Not currently supporting inline assembler for C compilation. It's perfectly possible, but need more work to sort out. */
# undef STSLSOFT_INLINE_ASM_SUPPORTED
#endif /* !__cplusplus && STSLSOFT_INLINE_ASM_SUPPORTED */
# ifdef STSLSOFT_INLINE_ASM_SUPPORTED
/* The default is to define them inline */
# ifdef STSLSOFT_ASM_IN_INLINE_SUPPORTED
# define WINSTL_ATOMIC_FNS_DECL_(type) STLSOFT_INLINE type WINSTL_ATOMIC_FNS_CALLCONV
# define WINSTL_ATOMIC_FNS_IMPL_(type) STLSOFT_INLINE __declspec(naked) type WINSTL_ATOMIC_FNS_CALLCONV
# else /* ? STSLSOFT_ASM_IN_INLINE_SUPPORTED */
# define WINSTL_ATOMIC_FNS_DECL_(type) type WINSTL_ATOMIC_FNS_CALLCONV
# define WINSTL_ATOMIC_FNS_IMPL_(type) static __declspec(naked) type WINSTL_ATOMIC_FNS_CALLCONV
# endif /* STSLSOFT_ASM_IN_INLINE_SUPPORTED */
# else /* ? STSLSOFT_INLINE_ASM_SUPPORTED */
/* ASM not supported, so we're using the Win32 functions */
# if defined(__cplusplus)
# define WINSTL_ATOMIC_FNS_DECL_(type) STLSOFT_INLINE type WINSTL_ATOMIC_FNS_CALLCONV
# define WINSTL_ATOMIC_FNS_IMPL_(type) STLSOFT_INLINE type WINSTL_ATOMIC_FNS_CALLCONV
# else /* ? __cplusplus */
# define WINSTL_ATOMIC_FNS_DECL_(type) STLSOFT_INLINE type WINSTL_ATOMIC_FNS_CALLCONV
# define WINSTL_ATOMIC_FNS_IMPL_(type) STLSOFT_INLINE type WINSTL_ATOMIC_FNS_CALLCONV
# endif /* __cplusplus */
# endif /* STSLSOFT_INLINE_ASM_SUPPORTED */
#endif /* declaration / definition */
/* /////////////////////////////////////////////////////////////////////////
* atomic function declarations
*/
#if !defined(WINSTL_ATOMIC_FNS_DEFINITION) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
# ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
/* Uni-processor variants */
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_preincrement_up(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_predecrement_up(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_postincrement_up(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_postdecrement_up(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(void) atomic_increment_up(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(void) atomic_decrement_up(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_exchange_up(atomic_int_t volatile* pl, atomic_int_t n);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_write_up(atomic_int_t volatile* pl, atomic_int_t n);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_read_up(atomic_int_t volatile const* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_postadd_up(atomic_int_t volatile* pl, atomic_int_t n);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_preadd_up(atomic_int_t volatile* pl, atomic_int_t n);
/* SMP variants */
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_preincrement_smp(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_predecrement_smp(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_postincrement_smp(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_postdecrement_smp(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(void) atomic_increment_smp(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(void) atomic_decrement_smp(atomic_int_t volatile* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_exchange_smp(atomic_int_t volatile* pl, atomic_int_t n);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_write_smp(atomic_int_t volatile* pl, atomic_int_t n);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_read_smp(atomic_int_t volatile const* pl);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_postadd_smp(atomic_int_t volatile* pl, atomic_int_t n);
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t) atomic_preadd_smp(atomic_int_t volatile* pl, atomic_int_t n);
# endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* Multi-processor detection variants */
/** Increments the variable atomically, and returns the result of the
* operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_preincrement(atomic_int_t volatile* pl);
/** Increments the (32-bit) variable atomically, and returns the result of
* the operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int32_t) atomic_preincrement32(atomic_int32_t volatile* pv);
/** Decrements the variable atomically, and returns the result of the
* operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_predecrement(atomic_int_t volatile* pl);
/** Decrements the (32-bit) variable atomically, and returns the result of
* the operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int32_t) atomic_predecrement32(atomic_int32_t volatile* pv);
/** Increments the variable atomically, and returns the value prior to the
* operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_postincrement(atomic_int_t volatile* pl);
/** Increments the (32-bit) variable atomically, and returns the value prior
* to the operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int32_t) atomic_postincrement32(atomic_int32_t volatile* pl);
/** Decrements the variable atomically, and returns the value prior to the
* operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_postdecrement(atomic_int_t volatile* pl);
/** Decrements the (32-bit) variable atomically, and returns the value prior
* to the operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int32_t) atomic_postdecrement32(atomic_int32_t volatile* pl);
/** Increments the variable atomically
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(void)
atomic_increment(atomic_int_t volatile* pl);
/** Increments the (32-bit) variable atomically
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(void) atomic_increment32(atomic_int32_t volatile* pl);
/** Decrements the variable atomically
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(void)
atomic_decrement(atomic_int_t volatile* pl);
/** Decrements the (32-bit) variable atomically
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(void) atomic_decrement32(atomic_int32_t volatile* pl);
/** Exchanges atomically a value with the variable
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_exchange(
atomic_int_t volatile* pl
, atomic_int_t n
);
/** Writes to the variable atomically
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_write(
atomic_int_t volatile* pl
, atomic_int_t n
);
/** Reads from the variable atomically
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_read(atomic_int_t volatile const* pl);
/** Add to the variable atomically, and returns the value prior to the
* operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_postadd(
atomic_int_t volatile* pl
, atomic_int_t n
);
/** Add to the variable atomically, and returns the result of the operation
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_DECL_(atomic_int_t)
atomic_preadd(
atomic_int_t volatile* pl
, atomic_int_t n
);
#endif /* !WINSTL_ATOMIC_FNS_DEFINITION */
/* /////////////////////////////////////////////////////////////////////////
* atomic function definitions
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# if !defined(WINSTL_ATOMIC_FNS_DECLARATION_ONLY)
# ifdef STSLSOFT_INLINE_ASM_SUPPORTED
/* Inline assembler versions */
#ifdef STLSOFT_COMPILER_IS_BORLAND
# pragma warn -8002 /* Suppresses: "Restarting compile using assembly" */
# pragma warn -8070 /* Suppresses: "Function should return a value" */
#endif /* compiler */
/* Uni-processor */
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_preincrement_up(atomic_int_t volatile* /* pl */)
{
_asm
{
/* pop 1 into eax, which can then be atomically added into *pl (held
* in ecx). Since it's an xadd it exchanges the previous value into eax
*/
mov eax, 1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
xadd dword ptr [ecx], eax
/* Since this is pre-increment, we need to inc eax to catch up with the
* real value
*/
inc eax
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_predecrement_up(atomic_int_t volatile* /* pl */)
{
_asm
{
/* pop 1 into eax, which can then be atomically added into *pl (held
* in ecx). Since it's an xadd it exchanges the previous value into eax
*/
mov eax, -1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
xadd dword ptr [ecx], eax
/* Since this is pre-decrement, we need to inc eax to catch up with the
* real value
*/
dec eax
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_postincrement_up(atomic_int_t volatile* /* pl */)
{
_asm
{
/* pop 1 into eax, which can then be atomically added into *pl (held
* in ecx). Since it's an xadd it exchanges the previous value into eax
*/
mov eax, 1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
xadd dword ptr [ecx], eax
/* Since this is post-increment, we need do nothing, since the previous
* value is in eax
*/
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_postdecrement_up(atomic_int_t volatile* /* pl */)
{
_asm
{
/* pop 1 into eax, which can then be atomically added into *pl (held
* in ecx). Since it's an xadd it exchanges the previous value into eax
*/
mov eax, -1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
xadd dword ptr [ecx], eax
/* Since this is post-decrement, we need do nothing, since the previous
* value is in eax
*/
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(void) atomic_increment_up(atomic_int_t volatile* /* pl */)
{
_asm
{
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
add dword ptr [ecx], 1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(void) atomic_decrement_up(atomic_int_t volatile* /* pl */)
{
_asm
{
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
sub dword ptr [ecx], 1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_exchange_up(atomic_int_t volatile* /* pl */, atomic_int_t /* n */)
{
_asm
{
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl, edx is n */
/* Just exchange *pl and n */
xchg dword ptr [ecx], edx
/* The previous value goes into edx, so me move it into eax for return */
mov eax, edx
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack: pl in esp+4, pl in esp+8 */
mov ecx, dword ptr [esp + 4] /* Load the address of pl into ecx */
mov eax, dword ptr [esp + 8] /* Load the value into eax, so the return value will be there waiting */
xchg dword ptr [ecx], eax
ret 8
#else
# error Need to define calling convention
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_read_up(atomic_int_t volatile const* /* pl */)
{
_asm
{
mov eax, 0
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
/* pop 0 into eax, which can then be atomically added into *pl (held
* in ecx), leaving the value unchanged.
*/
xadd dword ptr [ecx], eax
/* Since it's an xadd it exchanges the previous value into eax, which
* is exactly what's required
*/
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_write_up(atomic_int_t volatile* /* pl */, atomic_int_t /* n */)
{
_asm
{
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl, edx is n */
/* Just exchange *pl and n */
xchg dword ptr [ecx], edx
/* The previous value goes into edx, so me move it into eax for return */
mov eax, edx
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack: pl in esp+4, pl in esp+8 */
mov ecx, dword ptr [esp + 4] /* Load the address of pl into ecx */
mov eax, dword ptr [esp + 8] /* Load the value into eax, so the return value will be there waiting */
xchg dword ptr [ecx], eax
ret 8
#else
# error Need to define calling convention
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_postadd_up(atomic_int_t volatile* /* pl */, atomic_int_t /* n */)
{
/* Thanks to Eugene Gershnik for the fast-call implementation */
__asm
{
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl, edx is n */
/* Simply atomically add them, which will leave the previous value
* in edx
*/
xadd dword ptr [ecx], edx
/* Just need to move adx into eax to return it */
mov eax, edx
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack: pl in esp+4, pl in esp+8 */
/* Simply atomically add them, which will leave the previous value
* in edx
*/
mov ecx, dword ptr [esp + 4] /* Load the address of pl into ecx */
mov eax, dword ptr [esp + 8] /* Load the value into eax, so the return value will be there waiting */
xadd dword ptr [ecx], eax
/* Just need to move adx into eax to return it */
ret 8
#else
# error Need to define calling convention
#endif /* call-conv */
}
}
/* Symmetric multi-processor */
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_preincrement_smp(atomic_int_t volatile* /* pl */)
{
_asm
{
/* pop 1 into eax, which can then be atomically added into *pl (held
* in ecx). Since it's an xadd it exchanges the previous value into eax
*/
mov eax, 1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
lock xadd dword ptr [ecx], eax
/* Since this is pre-increment, we need to inc eax to catch up with the
* real value
*/
inc eax
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */
}
}
/**
*
* \ingroup group__library__Synch
*/
WINSTL_ATOMIC_FNS_IMPL_(atomic_int_t) atomic_predecrement_smp(atomic_int_t volatile* /* pl */)
{
_asm
{
/* pop 1 into eax, which can then be atomically added into *pl (held
* in ecx). Since it's an xadd it exchanges the previous value into eax
*/
mov eax, -1
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
/* __fastcall: ecx is pl */
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
/* __stdcall: arguments are on the stack */
mov ecx, dword ptr [esp + 4]
#else
# error Need to define calling convention
#endif /* call-conv */
lock xadd dword ptr [ecx], eax
/* Since this is pre-decrement, we need to inc eax to catch up with the
* real value
*/
dec eax
#if 0
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_FASTCALL)
ret
#elif defined(WINSTL_ATOMIC_FNS_CALLCONV_IS_STDCALL)
ret 4
#endif /* call-conv */