-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathNeslib.System.pas
More file actions
1030 lines (878 loc) · 28.2 KB
/
Neslib.System.pas
File metadata and controls
1030 lines (878 loc) · 28.2 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
unit Neslib.System;
{< System utilities }
{$INCLUDE 'Neslib.inc'}
interface
type
{ Base class for classes that can implement interfaces without reference
counting. }
TNonRefCountedObject = class
{$REGION 'Internal Declarations'}
protected
{ Prepare for interface implementations }
function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
function _AddRef: Integer; virtual; stdcall;
function _Release: Integer; virtual; stdcall;
{$ENDREGION 'Internal Declarations'}
end;
type
{ Represents a value type that can be assigned @bold(null) }
Nullable<T: record> = record
{$REGION 'Internal Declarations'}
private
FValue: T;
FHasValue: Boolean;
private
function GetValue: T; inline;
{$ENDREGION 'Internal Declarations'}
public
{ Creates a nullable with a given value.
Parameters:
AValue: the value to set the nullable to.
This will also set HasValue to True }
constructor Create(const AValue: T);
{ Creates a nullable with value null }
class function CreateNull: Nullable<T>; inline; static;
{ Sets the value to null }
procedure SetNull; inline;
{ Explicitly converts from a nullable to its underlying value.
Raises an EInvalidOperation if HasValue is False. }
class operator Explicit(const AValue: Nullable<T>): T; inline; static;
{ Implicitly converts from a value to a nullable. This sets the HasValue of
the nullable to True. }
class operator Implicit(const AValue: T): Nullable<T>; inline; static;
{ Compares two nullables for equality. }
class operator Equal(const ALeft, ARight: Nullable<T>): Boolean;
{ Compares two nullables for equality. }
class operator NotEqual(const ALeft, ARight: Nullable<T>): Boolean;
{ Gets the value, or the default value in case HasValue is False. }
function GetValueOrDefault: T; inline;
{ The value, if it has been assigned a valid underlying value.
Raises an EInvalidOperation if HasValue is False. }
property Value: T read GetValue;
{ A value indicating whether this nullable has a valid value of its
underlying type. }
property HasValue: Boolean read FHasValue;
end;
type
{ Represents a 2-tuple, or pair }
TTuple<T1, T2> = record
public
{ The first item }
Item1: T1;
{ The second item }
Item2: T2;
public
constructor Create(const AItem1: T1; const AItem2: T2);
end;
type
{ Represents a 3-tuple, or triple }
TTuple<T1, T2, T3> = record
public
{ The first item }
Item1: T1;
{ The second item }
Item2: T2;
{ The third item }
Item3: T3;
public
constructor Create(const AItem1: T1; const AItem2: T2; const AItem3: T3);
end;
type
{ Represents a 4-tuple, or quadruple }
TTuple<T1, T2, T3, T4> = record
public
{ The first item }
Item1: T1;
{ The second item }
Item2: T2;
{ The third item }
Item3: T3;
{ The fourth item }
Item4: T4;
public
constructor Create(const AItem1: T1; const AItem2: T2; const AItem3: T3;
const AItem4: T4);
end;
type
{ An atomic value. Can be read, written or updated atomically.
Modeled after std::atomic<>, supporting only relaxed memory ordering.
Only works with basic types that are 1, 2, 4 or 8 bytes in size (eg.
all integer types, Boolean, Char, Single, Double, TPoint etc.).
Uses assertions to check this.
For better performance, use one of the specialized version (like
TAtomicInteger) instead of this generic version. }
TAtomic<T: record> = record
{$REGION 'Internal Declarations'}
private
[volatile] FValue: T;
{$ENDREGION 'Internal Declarations'}
public
{ Creates an atomic value }
constructor Create(const AValue: T);
{ Atomically loads and returns the value.
On some platforms, this requires that this value is properly aligned (eg.
a TAtomic<Integer> should be aligned on a 4-byte boundary). When using
atomic values as fields in a class, Delphi will automatically align them
properly. }
function Load: T; inline;
{ Atomically replaces the current value with the given new value.
On some platforms, this requires that this value is properly aligned (eg.
a TAtomic<Integer> should be aligned on a 4-byte boundary). When using
atomic values as fields in a class, Delphi will automatically align them
properly. }
procedure Store(const ANewValue: T); inline;
{ Atomically replaces the current value with the given new value, and
returns the original value. }
function Exchange(const ANewValue: T): T; inline;
{ Compares this value to AExpected and, only if they are the same, sets
this value to ANewValue. Always returns the original value.
If the ASucceeded parameter is given, it will be set to True if the new
value was set (even if ANewValue is the same as the current value). }
function CompareExchange(const ANewValue, AExpected: T): T; overload; inline;
function CompareExchange(const ANewValue, AExpected: T;
out ASucceeded: Boolean): T; overload; inline;
{ Atomically increments the value (with an optional AIncrement) and returns
the new value.
Only meaningful when T is an integral type. Results will be undefined (and
may lead to crashes) if this is not the case). }
function Increment: T; overload; inline;
function Increment(const AIncrement: T): T; overload; inline;
{ Atomically decrements the value (with an optional ADecrement) and returns
the new value.
Only meaningful when T is an integral type. Results will be undefined (and
may lead to crashes) if this is not the case). }
function Decrement: T; overload; inline;
function Decrement(const ADecrement: T): T; overload; inline;
end;
type
{ Specialized version of TAtomic<Boolean>, providing better performance.
(although using the Atomic* intrinsics is still faster). }
TAtomicBoolean = record
{$REGION 'Internal Declarations'}
private
FValue: Integer;
{$ENDREGION 'Internal Declarations'}
public
constructor Create(const AValue: Boolean);
function Load: Boolean; inline;
procedure Store(const ANewValue: Boolean); inline;
function Exchange(const ANewValue: Boolean): Boolean; inline;
function CompareExchange(const ANewValue, AExpected: Boolean): Boolean; overload; inline;
function CompareExchange(const ANewValue, AExpected: Boolean;
out ASucceeded: Boolean): Boolean; overload; inline;
end;
type
{ Specialized version of TAtomic<Integer>, providing better performance.
(although using the Atomic* intrinsics is still faster). }
TAtomicInteger = record
{$REGION 'Internal Declarations'}
private
FValue: Integer;
{$ENDREGION 'Internal Declarations'}
public
constructor Create(const AValue: Integer);
function Load: Integer; inline;
procedure Store(const ANewValue: Integer); inline;
function Exchange(const ANewValue: Integer): Integer; inline;
function CompareExchange(const ANewValue, AExpected: Integer): Integer; overload; inline;
function CompareExchange(const ANewValue, AExpected: Integer;
out ASucceeded: Boolean): Integer; overload; inline;
function Increment: Integer; overload; inline;
function Increment(const AIncrement: Integer): Integer; overload; inline;
function Decrement: Integer; overload; inline;
function Decrement(const ADecrement: Integer): Integer; overload; inline;
end;
type
{ Specialized version of TAtomic<Int64>, providing better performance.
(although using the Atomic* intrinsics is still faster). }
TAtomicInt64 = record
{$REGION 'Internal Declarations'}
private
FValue: Int64;
{$ENDREGION 'Internal Declarations'}
public
constructor Create(const AValue: Int64);
function Load: Int64; inline;
procedure Store(const ANewValue: Int64); inline;
function Exchange(const ANewValue: Int64): Int64; inline;
function CompareExchange(const ANewValue, AExpected: Int64): Int64; overload; inline;
function CompareExchange(const ANewValue, AExpected: Int64;
out ASucceeded: Boolean): Int64; overload; inline;
function Increment: Int64; overload; inline;
function Increment(const AIncrement: Int64): Int64; overload; inline;
function Decrement: Int64; overload; inline;
function Decrement(const ADecrement: Int64): Int64; overload; inline;
end;
type
{ Specialized version of TAtomic<NativeInt>, providing better performance
(although using the Atomic* intrinsics is still faster). }
TAtomicNativeInt = record
{$REGION 'Internal Declarations'}
private
FValue: NativeInt;
{$ENDREGION 'Internal Declarations'}
public
constructor Create(const AValue: NativeInt);
function Load: NativeInt; inline;
procedure Store(const ANewValue: NativeInt); inline;
function Exchange(const ANewValue: NativeInt): NativeInt; inline;
function CompareExchange(const ANewValue, AExpected: NativeInt): NativeInt; overload; inline;
function CompareExchange(const ANewValue, AExpected: NativeInt;
out ASucceeded: Boolean): NativeInt; overload; inline;
function Increment: NativeInt; overload; inline;
function Increment(const AIncrement: NativeInt): NativeInt; overload; inline;
function Decrement: NativeInt; overload; inline;
function Decrement(const ADecrement: NativeInt): NativeInt; overload; inline;
end;
{ Allocates memory aligned to a certain boundary.
Parameters:
APtr: the memory pointer to allocate
ASize: the number of bytes to allocate
AAlign: the alignment (eg. use 16 to align on a 16-byte boundary).
Must be >= 8 and a power of 2.
Do @bold(not) use FreeMem to free the memory. You @bold(must) use
FreeMemAligned instead. }
procedure GetMemAligned(out APtr; const ASize, AAlign: NativeInt);
{ Allocates memory aligned to a Level 1 cache line (which is currently assumed
to be 64 bytes on all platforms).
Parameters:
APtr: the memory pointer to allocate
ASize: the number of bytes to allocate
Do @bold(not) use FreeMem to free the memory. You @bold(must) use
FreeMemAligned instead. }
procedure GetMemL1Aligned(out APtr; const ASize: NativeInt); inline;
{ Frees memory previously allocated with GetMemAligned or GetMemL1Aligned.
Parameters:
APtr: the memory pointer to deallocate }
procedure FreeMemAligned(const APtr: Pointer);
resourcestring
RS_NULLABLE_ERROR = 'Illegal access of nullable value wity value null';
implementation
uses
System.Classes,
System.SysUtils;
procedure GetMemAligned(out APtr; const ASize, AAlign: NativeInt);
var
Orig: Pointer;
Aligned: IntPtr absolute APtr;
BytesShifted: Integer;
begin
Assert(AAlign >= 8);
Assert((AAlign and (AAlign - 1)) = 0);
{ Allocate AAlign extra bytes of memory. These extra bytes are used to move
the pointer to an aligned address, AND to store the number of bytes shifted,
so we can retrieve the original pointer later when freeing it. }
GetMem(Orig, ASize + AAlign);
{ Move pointer to next multiple of AAlign. If pointer is already aligned, then
it is moved AAlign bytes ahead (so we always have some extra room before the
pointer. }
Aligned := (IntPtr(Orig) and not (AAlign - 1)) + AAlign;
{ Store shift value before the pointer }
BytesShifted := Aligned - IntPtr(Orig);
PInteger(Aligned - 4)^ := BytesShifted;
end;
procedure GetMemL1Aligned(out APtr; const ASize: NativeInt);
begin
GetMemAligned(APtr, ASize, 64);
end;
procedure FreeMemAligned(const APtr: Pointer);
var
BytesShifted: Integer;
Orig: Pointer;
begin
if (APtr = nil) then
Exit;
{ Retrieve shift value }
BytesShifted := PInteger(IntPtr(APtr) - 4)^;
{ Get original pointer }
Orig := Pointer(IntPtr(APtr) - BytesShifted);
{ Free original pointer }
FreeMem(Orig);
end;
{ TNonRefCountedObject }
function TNonRefCountedObject.QueryInterface(const IID: TGUID;
out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := S_OK
else
Result := E_NOINTERFACE;
end;
function TNonRefCountedObject._AddRef: Integer;
begin
Result := -1;
end;
function TNonRefCountedObject._Release: Integer;
begin
Result := -1;
end;
{ Nullable<T> }
constructor Nullable<T>.Create(const AValue: T);
begin
FValue := AValue;
FHasValue := True;
end;
class function Nullable<T>.CreateNull: Nullable<T>;
begin
Result.SetNull;
end;
class operator Nullable<T>.Equal(const ALeft,
ARight: Nullable<T>): Boolean;
begin
Result := (ALeft.FHasValue = ARight.FHasValue);
if (Result) and (ALeft.FHasValue) then
Result := CompareMem(@ALeft.FValue, @ARight.FValue, SizeOf(T));
end;
class operator Nullable<T>.Explicit(const AValue: Nullable<T>): T;
begin
Result := AValue.Value;
end;
function Nullable<T>.GetValue: T;
begin
if (not FHasValue) then
raise EInvalidOperation.CreateRes(@RS_NULLABLE_ERROR);
Result := FValue;
end;
function Nullable<T>.GetValueOrDefault: T;
begin
if (FHasValue) then
Result := FValue
else
Result := Default(T);
end;
class operator Nullable<T>.Implicit(const AValue: T): Nullable<T>;
begin
Result := Nullable<T>.Create(AValue);
end;
class operator Nullable<T>.NotEqual(const ALeft,
ARight: Nullable<T>): Boolean;
begin
Result := not (ALeft = ARight);
end;
procedure Nullable<T>.SetNull;
begin
FValue := Default(T);
FHasValue := False;
end;
{ TTuple<T1, T2> }
constructor TTuple<T1, T2>.Create(const AItem1: T1; const AItem2: T2);
begin
Item1 := AItem1;
Item2 := AItem2;
end;
{ TTuple<T1, T2, T3> }
constructor TTuple<T1, T2, T3>.Create(const AItem1: T1; const AItem2: T2;
const AItem3: T3);
begin
Item1 := AItem1;
Item2 := AItem2;
Item3 := AItem3;
end;
{ TTuple<T1, T2, T3, T4> }
constructor TTuple<T1, T2, T3, T4>.Create(const AItem1: T1; const AItem2: T2;
const AItem3: T3; const AItem4: T4);
begin
Item1 := AItem1;
Item2 := AItem2;
Item3 := AItem3;
Item4 := AItem4;
end;
{ TAtomic<T> }
function TAtomic<T>.CompareExchange(const ANewValue, AExpected: T): T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
V8: Byte absolute ANewValue;
V16: Word absolute ANewValue;
V32: Cardinal absolute ANewValue;
V64: UInt64 absolute ANewValue;
E8: Byte absolute AExpected;
E16: Word absolute AExpected;
E32: Cardinal absolute AExpected;
E64: UInt64 absolute AExpected;
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicCmpExchange(PByte(@FValue)^, V8, E8)
else if (SizeOf(T) = 2) then
R16 := AtomicCmpExchange(PWord(@FValue)^, V16, E16)
else if (SizeOf(T) = 4) then
R32 := AtomicCmpExchange(PCardinal(@FValue)^, V32, E32)
else if (SizeOf(T) = 8) then
R64 := AtomicCmpExchange(PUInt64(@FValue)^, V64, E64)
else
Assert(False);
end;
function TAtomic<T>.CompareExchange(const ANewValue, AExpected: T;
out ASucceeded: Boolean): T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
V8: Byte absolute ANewValue;
V16: Word absolute ANewValue;
V32: Cardinal absolute ANewValue;
V64: UInt64 absolute ANewValue;
E8: Byte absolute AExpected;
E16: Word absolute AExpected;
E32: Cardinal absolute AExpected;
E64: UInt64 absolute AExpected;
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicCmpExchange(PByte(@FValue)^, V8, E8, ASucceeded)
else if (SizeOf(T) = 2) then
R16 := AtomicCmpExchange(PWord(@FValue)^, V16, E16, ASucceeded)
else if (SizeOf(T) = 4) then
R32 := AtomicCmpExchange(PCardinal(@FValue)^, V32, E32, ASucceeded)
else if (SizeOf(T) = 8) then
R64 := AtomicCmpExchange(PUInt64(@FValue)^, V64, E64, ASucceeded)
else
Assert(False);
end;
constructor TAtomic<T>.Create(const AValue: T);
begin
FValue := AValue;
end;
function TAtomic<T>.Decrement(const ADecrement: T): T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
V8: Byte absolute ADecrement;
V16: Word absolute ADecrement;
V32: Cardinal absolute ADecrement;
V64: UInt64 absolute ADecrement;
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicDecrement(PByte(@FValue)^, V8)
else if (SizeOf(T) = 2) then
R16 := AtomicDecrement(PWord(@FValue)^, V16)
else if (SizeOf(T) = 4) then
R32 := AtomicDecrement(PCardinal(@FValue)^, V32)
else if (SizeOf(T) = 8) then
R64 := AtomicDecrement(PUInt64(@FValue)^, V64)
else
Assert(False);
end;
function TAtomic<T>.Decrement: T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicDecrement(PByte(@FValue)^)
else if (SizeOf(T) = 2) then
R16 := AtomicDecrement(PWord(@FValue)^)
else if (SizeOf(T) = 4) then
R32 := AtomicDecrement(PCardinal(@FValue)^)
else if (SizeOf(T) = 8) then
R64 := AtomicDecrement(PUInt64(@FValue)^)
else
Assert(False);
end;
function TAtomic<T>.Exchange(const ANewValue: T): T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
V8: Byte absolute ANewValue;
V16: Word absolute ANewValue;
V32: Cardinal absolute ANewValue;
V64: UInt64 absolute ANewValue;
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicExchange(PByte(@FValue)^, V8)
else if (SizeOf(T) = 2) then
R16 := AtomicExchange(PWord(@FValue)^, V16)
else if (SizeOf(T) = 4) then
R32 := AtomicExchange(PCardinal(@FValue)^, V32)
else if (SizeOf(T) = 8) then
R64 := AtomicExchange(PUInt64(@FValue)^, V64)
else
Assert(False);
end;
function TAtomic<T>.Increment(const AIncrement: T): T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
V8: Byte absolute AIncrement;
V16: Word absolute AIncrement;
V32: Cardinal absolute AIncrement;
V64: UInt64 absolute AIncrement;
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicIncrement(PByte(@FValue)^, V8)
else if (SizeOf(T) = 2) then
R16 := AtomicIncrement(PWord(@FValue)^, V16)
else if (SizeOf(T) = 4) then
R32 := AtomicIncrement(PCardinal(@FValue)^, V32)
else if (SizeOf(T) = 8) then
R64 := AtomicIncrement(PUInt64(@FValue)^, V64)
else
Assert(False);
end;
function TAtomic<T>.Increment: T;
{ Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
if (SizeOf(T) = 1) then
R8 := AtomicIncrement(PByte(@FValue)^)
else if (SizeOf(T) = 2) then
R16 := AtomicIncrement(PWord(@FValue)^)
else if (SizeOf(T) = 4) then
R32 := AtomicIncrement(PCardinal(@FValue)^)
else if (SizeOf(T) = 8) then
R64 := AtomicIncrement(PUInt64(@FValue)^)
else
Assert(False);
end;
function TAtomic<T>.Load: T;
{ On Intel platforms, CPU loads are guaranteed to be atomic if the value
is properly aligned.
Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
R8: Byte absolute Result;
R16: Word absolute Result;
R32: Cardinal absolute Result;
R64: UInt64 absolute Result;
begin
{$IF Defined(CPUX86)}
if (SizeOf(T) = 1) then
R8 := PByte(@FValue)^
else if (SizeOf(T) = 2) then
begin
Assert((UIntPtr(@FValue) and 1) = 0);
R16 := PWord(@FValue)^;
end
else if (SizeOf(T) = 4) then
begin
Assert((UIntPtr(@FValue) and 3) = 0);
R32 := PCardinal(@FValue)^;
end
else if (SizeOf(T) = 8) then
R64 := AtomicCmpExchange(PUInt64(@FValue)^, 0, 0)
else
Assert(False);
{$ELSEIF Defined(CPUX64)}
if (SizeOf(T) = 1) then
R8 := PByte(@FValue)^
else if (SizeOf(T) = 2) then
begin
Assert((UIntPtr(@FValue) and 1) = 0);
R16 := PWord(@FValue)^;
end
else if (SizeOf(T) = 4) then
begin
Assert((UIntPtr(@FValue) and 3) = 0);
R32 := PCardinal(@FValue)^;
end
else if (SizeOf(T) = 8) then
begin
Assert((UIntPtr(@FValue) and 7) = 0);
R64 := PUInt64(@FValue)^;
end
else
Assert(False);
{$ELSE}
if (SizeOf(T) = 1) then
R8 := AtomicCmpExchange(PByte(@FValue)^, 0, 0)
else if (SizeOf(T) = 2) then
R16 := AtomicCmpExchange(PWord(@FValue)^, 0, 0)
else if (SizeOf(T) = 4) then
R32 := AtomicCmpExchange(PCardinal(@FValue)^, 0, 0)
else if (SizeOf(T) = 8) then
R64 := AtomicCmpExchange(PUInt64(@FValue)^, 0, 0)
else
Assert(False);
{$ENDIF}
end;
procedure TAtomic<T>.Store(const ANewValue: T);
{ On Intel platforms, CPU stored are guaranteed to be atomic if the value
is properly aligned.
Note that the "if" statements are evaluated at compile time, so they do not
affect runtime performance }
var
V8: Byte absolute ANewValue;
V16: Word absolute ANewValue;
V32: Cardinal absolute ANewValue;
V64: UInt64 absolute ANewValue;
begin
{$IF Defined(CPUX86)}
if (SizeOf(T) = 1) then
PByte(@FValue)^ := V8
else if (SizeOf(T) = 2) then
begin
Assert((UIntPtr(@FValue) and 1) = 0);
PWord(@FValue)^ := V16;
end
else if (SizeOf(T) = 4) then
begin
Assert((UIntPtr(@FValue) and 3) = 0);
PCardinal(@FValue)^ := V32;
end
else if (SizeOf(T) = 8) then
AtomicExchange(PUInt64(@FValue)^, V64)
else
Assert(False);
{$ELSEIF Defined(CPUX64)}
if (SizeOf(T) = 1) then
PByte(@FValue)^ := V8
else if (SizeOf(T) = 2) then
begin
Assert((UIntPtr(@FValue) and 1) = 0);
PWord(@FValue)^ := V16;
end
else if (SizeOf(T) = 4) then
begin
Assert((UIntPtr(@FValue) and 3) = 0);
PCardinal(@FValue)^ := V32;
end
else if (SizeOf(T) = 8) then
begin
Assert((UIntPtr(@FValue) and 7) = 0);
PUInt64(@FValue)^ := V64;
end
else
Assert(False);
{$ELSE}
if (SizeOf(T) = 1) then
AtomicExchange(PByte(@FValue)^, V8)
else if (SizeOf(T) = 2) then
AtomicExchange(PWord(@FValue)^, V16)
else if (SizeOf(T) = 4) then
AtomicExchange(PCardinal(@FValue)^, V32)
else if (SizeOf(T) = 8) then
AtomicExchange(PUInt64(@FValue)^, V64)
else
Assert(False);
{$ENDIF}
end;
{ TAtomicBoolean }
function TAtomicBoolean.CompareExchange(const ANewValue,
AExpected: Boolean): Boolean;
begin
Result := (AtomicCmpExchange(FValue, Ord(ANewValue), Ord(AExpected)) <> 0);
end;
function TAtomicBoolean.CompareExchange(const ANewValue, AExpected: Boolean;
out ASucceeded: Boolean): Boolean;
begin
Result := (AtomicCmpExchange(FValue, Ord(ANewValue), Ord(AExpected), ASucceeded) <> 0);
end;
constructor TAtomicBoolean.Create(const AValue: Boolean);
begin
FValue := Ord(AValue);
end;
function TAtomicBoolean.Exchange(const ANewValue: Boolean): Boolean;
begin
Result := (AtomicExchange(FValue, Ord(ANewValue)) <> 0);
end;
function TAtomicBoolean.Load: Boolean;
{ On Intel platforms, CPU loads are guaranteed to be atomic if the value
is properly aligned. }
begin
{$IF Defined(CPUX86) or Defined(CPUX64)}
Result := (FValue <> 0);
{$ELSE}
Result := (AtomicCmpExchange(FValue, 0, 0) <> 0);
{$ENDIF}
end;
procedure TAtomicBoolean.Store(const ANewValue: Boolean);
{ On Intel platforms, CPU stored are guaranteed to be atomic if the value
is properly aligned. }
begin
{$IF Defined(CPUX86) or Defined(CPUX64)}
FValue := Ord(ANewValue);
{$ELSE}
AtomicExchange(FValue, Ord(ANewValue));
{$ENDIF}
end;
{ TAtomicInteger }
function TAtomicInteger.CompareExchange(const ANewValue,
AExpected: Integer): Integer;
begin
Result := AtomicCmpExchange(FValue, ANewValue, AExpected);
end;
function TAtomicInteger.CompareExchange(const ANewValue, AExpected: Integer;
out ASucceeded: Boolean): Integer;
begin
Result := AtomicCmpExchange(FValue, ANewValue, AExpected, ASucceeded);
end;
constructor TAtomicInteger.Create(const AValue: Integer);
begin
FValue := AValue;
end;
function TAtomicInteger.Decrement(const ADecrement: Integer): Integer;
begin
Result := AtomicDecrement(FValue, ADecrement);
end;
function TAtomicInteger.Decrement: Integer;
begin
Result := AtomicDecrement(FValue);
end;
function TAtomicInteger.Exchange(const ANewValue: Integer): Integer;
begin
Result := AtomicExchange(FValue, ANewValue);
end;
function TAtomicInteger.Increment(const AIncrement: Integer): Integer;
begin
Result := AtomicIncrement(FValue, AIncrement);
end;
function TAtomicInteger.Increment: Integer;
begin
Result := AtomicIncrement(FValue);
end;
function TAtomicInteger.Load: Integer;
{ On Intel platforms, CPU loads are guaranteed to be atomic if the value
is properly aligned. }
begin
{$IF Defined(CPUX86) or Defined(CPUX64)}
Result := FValue;
{$ELSE}
Result := AtomicCmpExchange(FValue, 0, 0);
{$ENDIF}
end;
procedure TAtomicInteger.Store(const ANewValue: Integer);
{ On Intel platforms, CPU stored are guaranteed to be atomic if the value
is properly aligned. }
begin
{$IF Defined(CPUX86) or Defined(CPUX64)}
FValue := ANewValue;
{$ELSE}
AtomicExchange(FValue, ANewValue);
{$ENDIF}
end;
{ TAtomicInt64 }
function TAtomicInt64.CompareExchange(const ANewValue,
AExpected: Int64): Int64;
begin
Result := AtomicCmpExchange(FValue, ANewValue, AExpected);
end;
function TAtomicInt64.CompareExchange(const ANewValue, AExpected: Int64;
out ASucceeded: Boolean): Int64;
begin
Result := AtomicCmpExchange(FValue, ANewValue, AExpected, ASucceeded);
end;
constructor TAtomicInt64.Create(const AValue: Int64);
begin
FValue := AValue;
end;
function TAtomicInt64.Decrement(const ADecrement: Int64): Int64;
begin
Result := AtomicDecrement(FValue, ADecrement);
end;
function TAtomicInt64.Decrement: Int64;
begin
Result := AtomicDecrement(FValue);
end;
function TAtomicInt64.Exchange(const ANewValue: Int64): Int64;
begin
Result := AtomicExchange(FValue, ANewValue);
end;
function TAtomicInt64.Increment(const AIncrement: Int64): Int64;
begin
Result := AtomicIncrement(FValue, AIncrement);
end;
function TAtomicInt64.Increment: Int64;
begin
Result := AtomicIncrement(FValue);
end;
function TAtomicInt64.Load: Int64;
{ On Intel platforms, CPU loads are guaranteed to be atomic if the value
is properly aligned. }
begin
{$IF Defined(CPUX64)}
Result := FValue;
{$ELSE}
Result := AtomicCmpExchange(FValue, 0, 0);
{$ENDIF}
end;
procedure TAtomicInt64.Store(const ANewValue: Int64);
{ On Intel platforms, CPU stored are guaranteed to be atomic if the value
is properly aligned. }
begin
{$IF Defined(CPUX64)}
FValue := ANewValue;
{$ELSE}
AtomicExchange(FValue, ANewValue);
{$ENDIF}
end;
{ TAtomicNativeInt }
function TAtomicNativeInt.CompareExchange(const ANewValue,
AExpected: NativeInt): NativeInt;
begin
Result := AtomicCmpExchange(FValue, ANewValue, AExpected);
end;
function TAtomicNativeInt.CompareExchange(const ANewValue,
AExpected: NativeInt; out ASucceeded: Boolean): NativeInt;
begin
Result := AtomicCmpExchange(FValue, ANewValue, AExpected, ASucceeded);
end;
constructor TAtomicNativeInt.Create(const AValue: NativeInt);
begin
FValue := AValue;
end;
function TAtomicNativeInt.Decrement(const ADecrement: NativeInt): NativeInt;
begin
Result := AtomicDecrement(FValue, ADecrement);
end;
function TAtomicNativeInt.Decrement: NativeInt;
begin
Result := AtomicDecrement(FValue);
end;
function TAtomicNativeInt.Exchange(const ANewValue: NativeInt): NativeInt;
begin
Result := AtomicExchange(FValue, ANewValue);
end;
function TAtomicNativeInt.Increment(const AIncrement: NativeInt): NativeInt;