-
Notifications
You must be signed in to change notification settings - Fork 8
/
ecommonobjs.pas
4124 lines (3395 loc) · 90.5 KB
/
ecommonobjs.pas
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
{
ECommonObjs:
Thread safe helpers to access to objects and data
Part of ESolver project
Copyright (c) 2019-2024 by Ilya Medvedkov
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit ECommonObjs;
{$mode objfpc}{$H+}
{.$define AtomicAsRW}
{$define AtomicAsInterlocked}
{$define UseLibInterlockedOps}
{.$define AtomicAsCS}
{$ifdef AtomicAsRW}
{$define userwlocks}
{$else}
{.$define userwlocks}
{$endif}
{$IF defined(CPUX86_64) or defined(CPUX64)}
{$DEFINE x64}
{$ELSEIF defined(CPU386)}
{$DEFINE x86}
{$ELSE}
{$undef UseLibInterlockedOps}
{$IFEND}
interface
uses
Classes, SysUtils, OGLFastList, extmemorystream
{$ifdef userwlocks}
{$ifdef Windows}
,Windows
{$else}
{$ifdef Unix}
{$ifdef usecthreads}
,cthreads
{$endif}
,UnixType,PThreads
{$endif}
{$endif}
{$endif};
type
{ TNetInterlocked }
TNetInterlocked=class
public
class function Increment(var Destination:Int32):Int32; overload; static; inline;
class function Increment(var Destination:UInt32):UInt32; overload; static; inline;
class function Increment(var Destination:Int64):Int64; overload; static; inline;
class function Increment(var Destination:UInt64):UInt64; overload; static; inline;
class function Decrement(var Destination:Int32):Int32; overload; static; inline;
class function Decrement(var Destination:UInt32):UInt32; overload; static; inline;
class function Decrement(var Destination:Int64):Int64; overload; static; inline;
class function Decrement(var Destination:UInt64):UInt64; overload; static; inline;
class function Add(var Destination:Int32;const Value:Int32):Int32; overload; static; inline;
class function Add(var Destination:UInt32;const Value:UInt32):UInt32; overload; static; inline;
class function Add(var Destination:Int64;const Value:Int64):Int64; overload; static; inline;
class function Add(var Destination:UInt64;const Value:UInt64):UInt64; overload; static; inline;
class function Sub(var Destination:Int32;const Value:Int32):Int32; overload; static; inline;
class function Sub(var Destination:UInt32;const Value:UInt32):UInt32; overload; static; inline;
class function Sub(var Destination:Int64;const Value:Int64):Int64; overload; static; inline;
class function Sub(var Destination:UInt64;const Value:UInt64):UInt64; overload; static; inline;
class function Exchange(var Destination:Int32;const Source:Int32):Int32; overload; static; inline;
class function Exchange(var Destination:UInt32;const Source:UInt32):UInt32; overload; static; inline;
class function Exchange(var Destination:Int64;const Source:Int64):Int64; overload; static; inline;
class function Exchange(var Destination:UInt64;const Source:UInt64):UInt64; overload; static; inline;
class function Exchange(var Destination:pointer;const Source:pointer):pointer; overload; static; inline;
class function Exchange(var Destination:TObject;const Source:TObject):TObject; overload; static; inline;
class function Exchange(var Destination:LongBool;const Source:LongBool):LongBool; overload; static; inline;
class function CompareExchange(var Destination:Int32;const NewValue,Comperand:Int32):Int32; overload; static; inline;
class function CompareExchange(var Destination:UInt32;const NewValue,Comperand:UInt32):UInt32; overload; static; inline;
class function CompareExchange(var Destination:Int64;const NewValue,Comperand:Int64):Int64; overload; static; inline;
class function CompareExchange(var Destination:UInt64;const NewValue,Comperand:UInt64):UInt64; overload; static; inline;
class function CompareExchange(var Destination:pointer;const NewValue,Comperand:pointer):pointer; overload; static; inline;
class function CompareExchange(var Destination:TObject;const NewValue,Comperand:TObject):TObject; overload; static; inline;
class function CompareExchange(var Destination:LongBool;const NewValue,Comperand:LongBool):LongBool; overload; static; inline;
class function Read(var Source:Int32):Int32; overload; static; inline;
class function Read(var Source:UInt32):UInt32; overload; static; inline;
class function Read(var Source:Int64):Int64; overload; static; inline;
class function Read(var Source:UInt64):UInt64; overload; static; inline;
class function Read(var Source:pointer):pointer; overload; static; inline;
class function Read(var Source:TObject):TObject; overload; static; inline;
class function Read(var Source:LongBool):LongBool; overload; static; inline;
class function Write(var Destination:Int32;const Source:Int32):Int32; overload; static; inline;
class function Write(var Destination:UInt32;const Source:UInt32):UInt32; overload; static; inline;
class function Write(var Destination:Int64;const Source:Int64):Int64; overload; static; inline;
class function Write(var Destination:UInt64;const Source:UInt64):UInt64; overload; static; inline;
class function Write(var Destination:pointer;const Source:pointer):pointer; overload; static; inline;
class function Write(var Destination:TObject;const Source:TObject):TObject; overload; static; inline;
class function Write(var Destination:LongBool;const Source:LongBool):LongBool; overload; static; inline;
{$ifdef UseLibInterlockedOps}
class function InterlockedAnd(var A: QWord; B: QWord): QWord; overload; static; inline;
class function InterlockedAnd(var A: Int64; B: Int64): Int64; overload; static; inline;
class function InterlockedAnd(var A: Int32; B: Int32): Int32; overload; static; inline;
class function InterlockedAnd(var A: Cardinal; B: Cardinal): Cardinal; overload; static; inline;
class function InterlockedAnd(var A: LongBool; B: LongBool): LongBool; overload; static; inline;
class function InterlockedOr(var A: QWord; B: QWord): QWord; overload; static; inline;
class function InterlockedOr(var A: Int64; B: Int64): Int64; overload; static; inline;
class function InterlockedOr(var A: Int32; B: Int32): Int32; overload; static; inline;
class function InterlockedOr(var A: Cardinal; B: Cardinal): Cardinal; overload; static; inline;
class function InterlockedOr(var A: LongBool; B: LongBool): LongBool; overload; static; inline;
class function InterlockedXor(var A: QWord; B: QWord): QWord; overload; static; inline;
class function InterlockedXor(var A: Int64; B: Int64): Int64; overload; static; inline;
class function InterlockedXor(var A: Int32; B: Int32): Int32; overload; static; inline;
class function InterlockedXor(var A: Cardinal; B: Cardinal): Cardinal; overload; static; inline;
class function InterlockedXor(var A: LongBool; B: LongBool): LongBool; overload; static; inline;
class function InterlockedNot(var A: QWord): QWord; overload; static; inline;
class function InterlockedNot(var A: Int64): Int64; overload; static; inline;
class function InterlockedNot(var A: Int32): Int32; overload; static; inline;
class function InterlockedNot(var A: Cardinal): Cardinal; overload; static; inline;
class function InterlockedNot(var A: LongBool): LongBool; overload; static; inline;
class function InterlockedShr(var A: QWord; Cnt : Integer): QWord; overload; static; inline;
class function InterlockedShr(var A: Int64; Cnt : Integer): Int64; overload; static; inline;
class function InterlockedShr(var A: Int32; Cnt : Integer): Int32; overload; static; inline;
class function InterlockedShr(var A: Cardinal; Cnt : Integer): Cardinal; overload; static; inline;
class function InterlockedShr(var A: LongBool; Cnt : Integer): LongBool; overload; static; inline;
class function InterlockedShl(var A: QWord; Cnt : Integer): QWord; overload; static; inline;
class function InterlockedShl(var A: Int64; Cnt : Integer): Int64; overload; static; inline;
class function InterlockedShl(var A: Int32; Cnt : Integer): Int32; overload; static; inline;
class function InterlockedShl(var A: Cardinal; Cnt : Integer): Cardinal; overload; static; inline;
class function InterlockedShl(var A: LongBool; Cnt : Integer): LongBool; overload; static; inline;
{$endif}
end;
{ TNetCustomLockedObject }
TNetCustomLockedObject = class
private
FRTI: TRTLCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure Lock;
procedure UnLock;
end;
{$ifdef userwlocks}
{ TNetRWVariable }
TNetRWVariable = class
{$if defined(Windows)}
private
fSRWLock:TPasMPSRWLock;
{$elseif defined(Unix)}
private
fReadWriteLock:pthread_rwlock_t;
{$ifend}
public
constructor Create;
destructor Destroy; override;
procedure BeginRead;
procedure EndRead;
procedure BeginWrite;
procedure EndWrite;
end;
{$endif}
{ TAtomicVariable }
generic TAtomicVariable<T{$ifdef AtomicAsInterlocked},ST{$endif}> = class(
{$ifdef AtomicAsRW}
TNetRWVariable
{$else}
{$ifdef AtomicAsInterlocked}
TNetInterlocked
{$else}
TNetCustomLockedObject
{$endif}
{$endif})
private
FValue : {$ifdef AtomicAsInterlocked}ST{$else}T{$endif};
function GetValue: T;
procedure SetValue(AValue: T);
public
constructor Create(aValue : T);
property Value : T read GetValue write SetValue;
end;
TAtomicBoolean = class(specialize TAtomicVariable<Boolean{$ifdef AtomicAsInterlocked},LongBool{$endif}>);
TAtomicPointer = class(specialize TAtomicVariable<Pointer{$ifdef AtomicAsInterlocked},Pointer{$endif}>);
{ TAtomicNumeric }
generic TAtomicNumeric<T{$ifdef AtomicAsInterlocked},ST{$endif}> = class(specialize TAtomicVariable<T{$ifdef AtomicAsInterlocked},ST{$endif}>)
public
function AddValue(const aValue : T) : T;
function SubValue(const aValue : T) : T;
function AndValue(const aValue : T) : T;
function OrValue(const aValue : T) : T;
function XorValue(const aValue : T) : T;
function ShlValue(aCount : Integer) : T;
function ShrValue(aCount : Integer) : T;
function NotValue() : T;
procedure IncValue;
procedure DecValue;
end;
TAtomicInteger = class(specialize TAtomicNumeric<Integer{$ifdef AtomicAsInterlocked},Int32{$endif}>);
TAtomicCardinal = class(specialize TAtomicNumeric<Cardinal{$ifdef AtomicAsInterlocked},UInt32{$endif}>);
TAtomicInt64 = class(specialize TAtomicNumeric<Int64{$ifdef AtomicAsInterlocked},Int64{$endif}>);
TAtomicQWord = class(specialize TAtomicNumeric<QWord{$ifdef AtomicAsInterlocked},Uint64{$endif}>);
{ TNetReferencedObject }
TNetReferencedObject = class(TNetCustomLockedObject)
private
mReferenceCount : TAtomicInteger;
public
constructor Create;
destructor Destroy; override;
procedure IncReference;
procedure DecReference;
function HasReferences : Boolean;
function HasExternReferences : Boolean;
function NoReferences: Boolean;
end;
TNetReferenceOnDisconnect = procedure (v : TNetReferencedObject) of object;
{ TNetReferenceHolderList }
TNetReferenceHolderList = class(TNetCustomLockedObject)
private
FList : TFastList;
FOnDisconnect : TNetReferenceOnDisconnect;
function GetCount: integer;
function GetItem(index : integer): TNetReferencedObject;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(Obj : TNetReferencedObject);
procedure Remove(Obj : TNetReferencedObject);
procedure Delete(i : integer);
function IndexOf(Obj : TNetReferencedObject) : integer;
property Count : integer read GetCount;
property OnDisconnect : TNetReferenceOnDisconnect read FOnDisconnect write FOnDisconnect;
property Item[index : integer] : TNetReferencedObject read GetItem; default;
end;
THandlesList = class;
{ TNetHandle }
TNetHandle = class(TNetReferencedObject)
private
fHandle : Cardinal;
fOwner : THandlesList;
public
procedure Disconnect;
property Handle : Cardinal read FHandle write FHandle;
property Owner : THandlesList read fOwner write fOwner;
end;
{ THandlesList }
THandlesList = class(TNetCustomLockedObject)
private
FList : TNetReferenceHolderList;
function GetCount: integer;
function GetItem(index : integer): TNetHandle;
public
constructor Create;
destructor Destroy; override;
function GetByHandle(aHandle : Cardinal) : TObject;
procedure RemoveHandle(obj : TNetHandle); virtual;
procedure AddHandle(Obj : TNetHandle); virtual;
procedure Clear; virtual;
property Count : integer read GetCount;
property Item[index : integer] : TNetHandle read GetItem; default;
end;
{ TThreadSafeObject }
TThreadSafeObject = class(TNetCustomLockedObject);
{ TThreadGetValue }
generic TThreadGetValue<T> = class(TThreadSafeObject)
private
FValue : T;
function GetValue: T;
public
constructor Create(const AValue : T);
property Value : T read GetValue;
end;
{ TThreadGetSetValue }
generic TThreadGetSetValue<T> = class(specialize TThreadGetValue<T>)
private
procedure SetValue(const AValue: T); virtual;
public
property Value : T read GetValue write SetValue;
end;
{ TThreadNumeric }
generic TThreadNumeric<T> = class(specialize TThreadGetSetValue<T>)
public
procedure IncValue; virtual; overload;
procedure DecValue; virtual; overload;
procedure IncValue(IncSz : T); virtual; overload;
procedure DecValue(DecSz: T); virtual; overload;
procedure OrValue(mask : T); virtual;
procedure AndValue(mask : T); virtual;
procedure XorValue(mask : T); virtual;
procedure NotValue; virtual;
function BitwiseOr(mask : T) : T;
function BitwiseAnd(mask : T) : T;
function BitwiseXor(mask : T) : T;
end;
{ TThreadActiveNumeric }
generic TThreadActiveNumeric<T> = class(specialize TThreadNumeric<T>)
private
FOnChange : TNotifyEvent;
procedure DoOnChange;
procedure SetValue(const AValue: T); override;
public
procedure IncValue; override; overload;
procedure DecValue; override; overload;
procedure IncValue(IncSz : T); override; overload;
procedure DecValue(DecSz: T); override; overload;
procedure OrValue(mask : T); override;
procedure AndValue(mask : T); override;
procedure XorValue(mask : T); override;
procedure NotValue; override;
property OnChange : TNotifyEvent read FOnChange write FOnChange;
end;
TThreadByte = class(specialize TThreadNumeric<Byte>);
TThreadWord = class(specialize TThreadNumeric<Word>);
TThreadInteger = class(specialize TThreadNumeric<Integer>);
TThreadCardinal = class(specialize TThreadNumeric<Cardinal>);
TThreadQWord = class(specialize TThreadNumeric<QWord>);
TThreadInt64 = class(specialize TThreadNumeric<Int64>);
TThreadActiveByte = class(specialize TThreadActiveNumeric<Byte>);
TThreadActiveWord = class(specialize TThreadActiveNumeric<Word>);
TThreadActiveInteger = class(specialize TThreadActiveNumeric<Integer>);
TThreadActiveCardinal = class(specialize TThreadActiveNumeric<Cardinal>);
TThreadActiveQWord = class(specialize TThreadActiveNumeric<QWord>);
TThreadActiveInt64 = class(specialize TThreadActiveNumeric<Int64>);
TThreadUtf8String = class(specialize TThreadGetSetValue<UTF8String>);
TThreadWideString = class(specialize TThreadGetSetValue<WideString>);
{ TThreadPointer }
TThreadPointer = class(specialize TThreadGetValue<Pointer>)
public
constructor Create(ASize : QWord);
destructor Destroy; override;
procedure Realloc(AValue : Pointer);
end;
{ TThreadBoolean }
TThreadBoolean = class(specialize TThreadGetSetValue<Boolean>)
public
procedure Enable;
procedure Disable;
end;
{ TThreadSafeAutoIncrementCardinal }
TThreadSafeAutoIncrementCardinal = class(TThreadSafeObject)
private
FValue : Cardinal;
function GetID: Cardinal;
public
constructor Create;
procedure Reset;
procedure SetValue(v : Cardinal);
property ID : Cardinal read GetID;
end;
{ TThreadStringList }
TThreadStringList = class(TThreadSafeObject)
private
FStringList: TStringList;
FOnChange : TNotifyEvent;
FUpdates : Integer;
function GetCount: Integer;
function GetDelimitedText : String;
function GetDelimiter : Char;
function GetStr(index : integer): String;
function GetText: String;
procedure SetDelimitedText(const AValue : String);
procedure SetDelimiter(AValue : Char);
procedure SetStr(index : integer; const AValue : String);
procedure SetText(const AValue: String);
procedure DoChange;
function GetOnChange: TNotifyEvent;
procedure SetOnChange(AValue: TNotifyEvent);
public
constructor Create;
destructor Destroy; override;
function Add(const S: string): Integer;
procedure Delete(Index: Integer);
procedure DeleteFromTo(Index1, Index2: Integer);
procedure SaveToFile(const FN : String);
function IndexOf(const S: string): Integer;
procedure AddStrings(S : TStringList);
procedure BeginUpdate;
procedure EndUpdate;
procedure Clear;
procedure ConcatText(const aText : String);
property Item[index : integer] : String read GetStr write SetStr; default;
property Count : Integer read GetCount;
property Text : String read GetText write SetText;
property DelimitedText : String read GetDelimitedText write SetDelimitedText;
property Delimiter : Char read GetDelimiter write SetDelimiter;
property OnChange : TNotifyEvent read GetOnChange write SetOnChange;
end;
{ TThreadSafeFastBaseCollection }
generic TThreadSafeFastBaseCollection<T> = class(TThreadSafeObject)
private
FCollection : TFastCollection;
function GetCount: integer;
function GetObject(index : integer): T;
procedure SetObject(index : integer; AValue: T);
protected
procedure SetCount(AValue: integer); virtual;
public
constructor Create;
function Add(const Obj : T) : Integer; virtual;
function IndexOf(const Obj : T) : Integer;
function Remove(const obj: T) : integer; virtual;
procedure Delete(Ind : integer); virtual;
procedure DeleteFreeAssigned(Ind : integer); virtual;
procedure Clear; virtual;
procedure Extract(Ind : integer); virtual;
procedure Pack; virtual;
destructor Destroy; override;
procedure SortList(func : TObjectSortFunction);
property Count : integer read GetCount write SetCount;
property Item[index : integer] : T read GetObject write SetObject; default;
end;
TThreadSafeFastCollection = class(specialize TThreadSafeFastBaseCollection<TObject>);
{ TThreadSafeFastBaseList }
generic TThreadSafeFastBaseList<T> = class(TThreadSafeObject)
private
FList : TFastList;
function GetCount: integer;
function GetObject(index : integer): T;
procedure SetObject(index : integer; AValue: T);
protected
procedure SetCount(AValue: integer); virtual;
public
constructor Create;
function Add(const Obj : T) : Integer; virtual;
function IndexOf(const Obj : T) : Integer;
function Remove(const obj: T) : integer; virtual;
procedure Delete(Ind : integer); virtual;
procedure Clear; virtual;
procedure Extract(Ind : integer); virtual;
procedure Pack; virtual;
destructor Destroy; override;
procedure SortList(func : TObjectSortFunction);
property Count : integer read GetCount write SetCount;
property Item[index : integer] : T read GetObject write SetObject; default;
end;
TThreadSafeFastList = class(specialize TThreadSafeFastBaseList<TObject>);
{ TThreadSafeFastBaseSeq }
generic TThreadSafeFastBaseSeq<T> = class(TThreadSafeObject)
private
FSeq : TFastSeq;
function GetCount: integer;
public
constructor Create;
destructor Destroy; override;
procedure Clean;
procedure ExtractAll;
procedure Push_back(const O : TObject); virtual;
procedure Push_front(const O : TObject); virtual;
function Pop : TIteratorObject;
function PopValue : T;
function LastValue : T;
function FirstValue : T;
function InsertBefore(loc: TIteratorObject; o: TObject): TIteratorObject; virtual;
function InsertAfter(loc: TIteratorObject; o: TObject): TIteratorObject; virtual;
procedure Erase(const loc : TIteratorObject);
procedure EraseObject(const obj : TObject);
function EraseObjectsByCriteria(criteria: TFindObjectCriteria;
data : pointer): Boolean;
function FindValue(criteria : TFindObjectCriteria; data : Pointer) : T;
procedure DoForAll(action: TObjectAction);
procedure DoForAllEx(action: TExObjectAction; data : Pointer);
procedure Extract(const loc: TIteratorObject);
procedure ExtractObject(const obj: TObject);
function ExtractObjectsByCriteria(criteria: TFindObjectCriteria;
afterextract : TObjectAction;
data : pointer): Boolean;
function ListBegin : TIteratorObject;
function IteratorBegin : TIterator;
class function ListEnd : TIteratorObject;
property Count : integer read GetCount;
end;
TThreadSafeFastSeq = class(specialize TThreadSafeFastBaseSeq<TObject>);
{ TThreadActiveFastBaseCollection }
generic TThreadActiveFastBaseCollection<T> = class(specialize TThreadSafeFastBaseCollection<T>)
private
FOnChange : TNotifyEvent;
FUpdateCnt : integer;
procedure DoChange;
function GetOnChange: TNotifyEvent;
procedure SetOnChange(AValue: TNotifyEvent);
protected
procedure SetCount(AValue: integer); override;
public
constructor Create;
procedure BeginUpdate;
procedure EndUpdate;
function Add(const Obj : T) : Integer; override;
function Remove(const obj: T) : integer; override;
procedure Delete(Ind : integer); override;
procedure Clear; override;
procedure Extract(Ind : integer); override;
procedure Pack; override;
property OnChange : TNotifyEvent read GetOnChange write SetOnChange;
end;
{ TThreadActiveFastBaseSeq }
generic TThreadActiveFastBaseSeq<T> = class(specialize TThreadSafeFastBaseSeq<T>)
private
FOnChange : TNotifyEvent;
FUpdateCnt : integer;
procedure DoChange;
function GetOnChange: TNotifyEvent;
procedure SetOnChange(AValue: TNotifyEvent);
public
constructor Create;
procedure BeginUpdate;
procedure EndUpdate;
procedure Push_back(const O : TObject); override;
procedure Push_front(const O : TObject); override;
function InsertBefore(loc: TIteratorObject; o: TObject): TIteratorObject; override;
function InsertAfter(loc: TIteratorObject; o: TObject): TIteratorObject; override;
property OnChange : TNotifyEvent read GetOnChange write SetOnChange;
end;
{ TNetReferenceList }
TNetReferenceList = class(TThreadSafeFastSeq)
private
function IfNoReferences(obj : Tobject; ptr : pointer) : Boolean;
public
destructor Destroy; override;
procedure Add(const O : TNetReferencedObject);
procedure CleanDead;
end;
{ TReferencedStream }
TReferencedStream = class(TNetReferencedObject)
private
FStream : TStream;
public
constructor Create(aStrm : TStream);
destructor Destroy; override;
procedure WriteTo(Strm : TStream; from, Sz : PtrInt); virtual;
property Stream : TStream read FStream;
end;
{ TRefMemoryStream }
TRefMemoryStream = class(TReferencedStream)
public
constructor Create;
constructor Create(Sz : PtrInt); overload;
procedure WriteTo(Strm : TStream; from, Sz : PtrInt); override;
end;
implementation
{$ifdef USELIBINTERLOCKEDOPS}
{$ASMMODE Intel}
{$IFDEF x64}
{$DEFINE Ptr64}
{$ELSE}
{$IF SizeOf(Pointer) <> 4 }
{$MESSAGE FATAL 'Unsupported size of pointers.'}
{$ENDIF}
{$ENDIF}
{
This code block based on InterlockedOps library Version 1.4.2 (2022-02-21)
git@github.com:iLya2IK/lib.InterlockedOps.git
©2021-2024 Frantisek Milt
Contacts:
Frantisek Milt: frantisek.milt@gmail.com
}
//------------------------------------------------------------------------------
Function InterlockedLoad32(I: Pointer): UInt32;
begin
asm
XOR EDX, EDX
{$IFDEF x64}
{$IFDEF Windows}
LOCK XADD dword ptr [RCX], EDX
{$ELSE}
LOCK XADD dword ptr [RDI], EDX
{$ENDIF}
{$ELSE}
LOCK XADD dword ptr [EAX], EDX
{$ENDIF}
MOV EAX, EDX
end;
end;
//------------------------------------------------------------------------------
Function InterlockedLoad64(I: Pointer): UInt64;
begin
asm
{$IFDEF x64}
XOR RDX, RDX
{$IFDEF Windows}
LOCK XADD qword ptr [RCX], RDX
{$ELSE}
LOCK XADD qword ptr [RDI], RDX
{$ENDIF}
MOV RAX, RDX
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
PUSH EDI
MOV EDI, EAX
@TryOutStart:
MOV EAX, dword ptr [EDI]
MOV EDX, dword ptr [EDI + 4]
MOV EBX, EAX
MOV ECX, EDX
LOCK CMPXCHG8B qword ptr [EDI]
JNZ @TryOutStart
POP EDI
POP EBX
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
Function InterlockedLoadBool(I: Pointer): Boolean;
begin
Result := Boolean(InterlockedLoad32(I));
end;
//------------------------------------------------------------------------------
Function InterlockedStore32(I: Pointer; NewValue: UInt32): UInt32;
begin
asm
{$IFDEF x64}
{$IFDEF Windows}
LOCK XCHG dword ptr [RCX], EDX
MOV EAX, EDX
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - -
LOCK XCHG dword ptr [RDI], ESI
MOV EAX, ESI
{$ENDIF}
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
LOCK XCHG dword ptr [EAX], EDX
MOV EAX, EDX
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
Function InterlockedStore64(I: Pointer; NewValue: UInt64): UInt64;
begin
asm
{$IFDEF x64}
{$IFDEF Windows}
LOCK XCHG qword ptr [RCX], RDX
MOV RAX, RDX
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - -
LOCK XCHG qword ptr [RDI], RSI
MOV RAX, RSI
{$ENDIF}
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
PUSH EDI
MOV EDI, EAX
@TryOutStart:
MOV EBX, dword ptr [NewValue]
MOV ECX, dword ptr [NewValue + 4]
MOV EAX, dword ptr [EDI]
MOV EDX, dword ptr [EDI + 4]
LOCK CMPXCHG8B qword ptr [EDI]
JNZ @TryOutStart
POP EDI
POP EBX
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
Function InterlockedStoreBool(I: Pointer; NewValue: Boolean): Boolean;
begin
Result := Boolean(InterlockedStore32(I,UInt32(NewValue)));
end;
//------------------------------------------------------------------------------
Function InterlockedAnd32(A: Pointer; B: UInt32): UInt32;
begin
asm
{$IFDEF x64}
@TryOutStart:
{$IFDEF Windows}
MOV EAX, dword ptr [RCX]
{$ELSE}
MOV EAX, dword ptr [RDI]
{$ENDIF}
MOV R8D, EAX
AND R8D, B
{$IFDEF Windows}
LOCK CMPXCHG dword ptr [RCX], R8D
{$ELSE}
LOCK CMPXCHG dword ptr [RDI], R8D
{$ENDIF}
JNZ @TryOutStart
MOV EAX, R8D
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
MOV ECX, EAX
@TryOutStart:
MOV EAX, dword ptr [ECX]
MOV EBX, EAX
AND EBX, EDX
LOCK CMPXCHG dword ptr [ECX], EBX
JNZ @TryOutStart
MOV EAX, EBX
POP EBX
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
Function InterlockedAnd64(A: Pointer; B: UInt64): UInt64;
begin
asm
{$IFDEF x64}
@TryOutStart:
{$IFDEF Windows}
MOV RAX, qword ptr [RCX]
{$ELSE}
MOV RAX, qword ptr [RDI]
{$ENDIF}
MOV R8, RAX
AND R8, B
{$IFDEF Windows}
LOCK CMPXCHG qword ptr [RCX], R8
{$ELSE}
LOCK CMPXCHG qword ptr [RDI], R8
{$ENDIF}
JNZ @TryOutStart
MOV RAX, R8
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
PUSH EDI
MOV EDI, EAX
@TryOutStart:
MOV EAX, dword ptr [EDI]
MOV EDX, dword ptr [EDI + 4]
MOV EBX, dword ptr [B]
MOV ECX, dword ptr [B + 4]
AND EBX, EAX
AND ECX, EDX
LOCK CMPXCHG8B qword ptr [EDI]
JNZ @TryOutStart
MOV EAX, EBX
MOV EDX, ECX
POP EDI
POP EBX
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
Function InterlockedOr32(A: Pointer; B: UInt32): UInt32;
begin
asm
{$IFDEF x64}
@TryOutStart:
{$IFDEF Windows}
MOV EAX, dword ptr [RCX]
{$ELSE}
MOV EAX, dword ptr [RDI]
{$ENDIF}
MOV R8D, EAX
OR R8D, B
{$IFDEF Windows}
LOCK CMPXCHG dword ptr [RCX], R8D
{$ELSE}
LOCK CMPXCHG dword ptr [RDI], R8D
{$ENDIF}
JNZ @TryOutStart
MOV EAX, R8D
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
MOV ECX, EAX
@TryOutStart:
MOV EAX, dword ptr [ECX]
MOV EBX, EAX
OR EBX, EDX
LOCK CMPXCHG dword ptr [ECX], EBX
JNZ @TryOutStart
MOV EAX, EBX
POP EBX
{$ENDIF}
end;
end;
//------------------------------------------------------------------------------
Function InterlockedOr64(A: Pointer; B: UInt64): UInt64;
begin
asm
{$IFDEF x64}
@TryOutStart:
{$IFDEF Windows}
MOV RAX, qword ptr [RCX]
{$ELSE}
MOV RAX, qword ptr [RDI]
{$ENDIF}
MOV R8, RAX
OR R8, B
{$IFDEF Windows}
LOCK CMPXCHG qword ptr [RCX], R8
{$ELSE}
LOCK CMPXCHG qword ptr [RDI], R8
{$ENDIF}
JNZ @TryOutStart
MOV RAX, R8
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
PUSH EDI
MOV EDI, EAX
@TryOutStart:
MOV EAX, dword ptr [EDI]
MOV EDX, dword ptr [EDI + 4]
MOV EBX, dword ptr [B]
MOV ECX, dword ptr [B + 4]
OR EBX, EAX
OR ECX, EDX
LOCK CMPXCHG8B qword ptr [EDI]
JNZ @TryOutStart
MOV EAX, EBX
MOV EDX, ECX