forked from gabr42/OmniThreadLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOtl.Parallel.SynchroPrimitives.InterfaceLevel.pas
1865 lines (1582 loc) · 44.3 KB
/
Otl.Parallel.SynchroPrimitives.InterfaceLevel.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
unit Otl.Parallel.SynchroPrimitives.InterfaceLevel;
// IMPORTANT!
// READ THE COMMENTS IN UNIT Otl.Parallel.SynchroPrimitives .
{$I OtlOptions.inc}
interface
uses System.SyncObjs, Otl.Parallel.SynchroPrimitives.BasicLevel,
Generics.Collections, System.SysUtils, Otl.Parallel.Atomic;
type
TLockingMechanism = (KernalLocking, BusLocking);
TSignalState = (esSignalled, esNotSignalled, esUnknown);
ISynchro = interface ['{EF480FC2-7BBB-40B9-A9F7-246A7834CFA9}']
{$IFDEF MSWINDOWS}
function GetHandle: THandle;
{$ENDIF}
procedure Signal;
/// <remarks> ISynchro.isSignalled() is not supported for auto-reset events and semaphores.</remarks>
function isSignalled: boolean;
function isSignalled_IsSupported: boolean;
function SignalState: TSignalState;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult;
function AsObject: TObject;
function AsMWObject: TObject;
function IsPoolManaged: boolean;
/// <remarks> Only modular synchros can be combined into a composite synchro. </remarks>
function IsModular: boolean;
{$IFDEF MSWINDOWS}
/// <remarks> ISynchro.Handle is not supported for:
/// Non-windows platforms;
/// Light events;
/// Testable semaphores;
/// CountDowns/Ups nor
/// Functional events.
/// Basically, it is only for heavy events and native semaphores, all on a windows platform.
/// </remarks>
property Handle: THandle read GetHandle;
{$ENDIF}
end;
TSynchroArray = TArray<ISynchro>;
IEvent = interface( ISynchro) ['{3E6E1606-88E5-4898-9F79-613085CAE090}']
procedure SetEvent;
procedure ResetEvent;
function isManualEvent: boolean;
function isLight: boolean;
end;
ISemaphore = interface( ISynchro) ['{4ACCD111-3E22-4902-B397-3309D84BBDD9}']
function InitialValue: cardinal;
/// <remarks> ISemaphore.Value() is not supported for native semaphores.</remarks>
function Value: cardinal;
/// <remarks> ISemaphore.Reset() is not supported for native semaphores.</remarks>
procedure Reset;
/// <remarks> Returns True iff it is safe to call Value() and Reset().</remarks>
function isValueTesting_IsSupported: boolean;
end;
ILock = interface ['{38665699-5A91-4930-8AB8-AC4AD36B7182}']
procedure Enter;
procedure Leave;
function AsObject: TObject;
function IsPoolManaged: boolean;
function IsKernalMode: boolean;
function AsSpinLock: PSBDSpinLock;
function AsCriticalSection: TCriticalSection;
function LockCount: integer;
end;
ICountDown = interface( ISynchro) ['{23D82B7E-9670-4B24-835C-D1E879CF36C9}']
function InitialValue: cardinal;
function Value: cardinal;
function SignalHit: boolean;
function Allocate: cardinal; // Signal and return the value.
procedure CounterSignal;
end;
ICountUp = interface( ISynchro) ['{C3AD39CC-7918-44E3-AE08-B5E8F2F9EDB5}']
function InitialValue: cardinal;
function MaxValue: cardinal;
function Value: cardinal;
function SignalHit: boolean;
end;
IOtlMREW = interface ['{4929F170-F5DC-41F8-852E-400D99B441BC}']
function EnterRead( Timeout: cardinal = INFINITE): TWaitResult;
procedure ExitRead;
function EnterWrite( Timeout: cardinal = INFINITE): TWaitResult;
procedure ExitWrite;
function ReaderCount: integer; // if +ve, this is the number of entered readers,
// if -ve, there is an entered writer.
function AsReadLock: ILock;
function AsWriteLock: ILock;
end;
function CreateMREW: IOtlMREW;
type
TObjectList = class( TObjectList<TObject>) end;
TObjectQueue = class( TQueue<TObject>) end;
TSynchroFactory = class
public
function AcquireKernelEvent( AManual, AInitialState, AReUse: boolean): IEvent;
function AcquireLightEvent( AManual, AInitialState: boolean; SpinMax: integer; AReUse: boolean): IEvent;
function AcquireNativeSemaphore ( AInitialCount: cardinal): ISemaphore; // Native semaphores are never pooled.
function AcquireTestableSemaphore( AInitialCount: cardinal; AReUse: boolean): ISemaphore;
/// <remarks> When re-using critical sections, Enter/Leave calls must be balanced before releasing the last reference. </remarks>
function AcquireCriticalSection( AReUse: boolean): ILock; overload;
class function AcquireCriticalSection: ILock; overload;
function AcquireSpinLock: ILock; // Spinlocks are never pooled.
function AcquireCountDown( InitialValue: cardinal; AReUse: boolean): ICountDown;
function AcquireCountUp( InitialValue, MaxValue: cardinal; AReUse: boolean): ICountUp;
/// <remarks> To pulse a functional event, call Signal() on it. </remarks>
function AcquireFunctionalEvent( ASignalTest: TEventFunction; APLock: PSBDSpinLock; AReUse: boolean): ISynchro;
procedure PurgeAllPools;
function CanDestroy: boolean;
constructor Create( APoolMax: cardinal);
destructor Destroy; override;
private
FManualKernelEvents: TObjectQueue;
FAutoKernelEvents : TObjectQueue;
FLightEvents : TObjectQueue;
FSemaphores : TObjectQueue; // TSimulatedSemaphores only. Native semaphores cannot be pooled.
FCrits : TObjectQueue;
FCountDowns : TObjectQueue;
FCountUps : TObjectQueue;
FFunctionals : TObjectQueue;
[Volatile]
FAquisitionCount : TVolatileUint32;
FPoolMax : cardinal;
//FSemaphoreCount : cardinal;
private
[Volatile] FLock: TSBDSpinLock;
function Queues: TArray<TObjectQueue>;
end;
{$REGION 'For internal use only.'}
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateCountDownIntf( const APool: TSynchroFactory; InitialValue: cardinal): ICountDown;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateCountUpIntf( const APool: TSynchroFactory; InitialValue, MaxValue: cardinal): ICountUp;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateKernalEventIntf( const APool: TSynchroFactory; ManualReset, InitialState: boolean): IEvent;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateLightEventIntf( const APool: TSynchroFactory; ManualReset, InitialState: boolean; SpinMax: cardinal): IEvent;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateCritLockIntf( const APool: TSynchroFactory): ILock;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateSpinLockIntf: ILock;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateTestableSemaphoreIntf( const APool: TSynchroFactory; AInitialCount: cardinal): ISemaphore;
/// <remarks>DO NOT USE. For internal use only.</remarks>
function _CreateNativeSemaphoreIntf( AInitialCount: cardinal): ISemaphore;
{$ENDREGION}
implementation
uses Otl.Parallel.Errors;
type
TRecycleObject = class abstract( TObject, IInterface)
private const
objPooledFlag = Integer( $80000000);
private
{$IFDEF AUTOREFCOUNT} [weak] {$ENDIF}
FPool: TSynchroFactory;
[Volatile] FRefCount: TVolatileInt32;
FAcquired: boolean;
function QueryInterface( const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
function AsObject: TObject;
function IsPoolManaged: boolean;
function GetRefCount: integer; inline;
protected
function Pool: TObjectQueue; virtual; abstract;
procedure ReleaseConfiguration; virtual;
function SignalState: TSignalState; virtual; abstract;
public
constructor Create( const APool: TSynchroFactory);
procedure AfterConstruction; override;
class function NewInstance: TObject; override;
property RefCount: integer read GetRefCount;
end;
TRecycleSynchro = class( TRecycleObject, ISynchro)
protected
{$IFDEF MSWINDOWS}
function GetHandle: THandle; virtual;
{$ENDIF}
procedure Signal; virtual; abstract;
function isSignalled: boolean; virtual; abstract;
function isSignalled_IsSupported: boolean; virtual;
function SignalState: TSignalState; override;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; virtual; abstract;
function IsModular: boolean; virtual;
function AsMWObject: TObject; virtual;
end;
TRecycleEvent = class( TRecycleSynchro, IEvent)
protected
{$IFDEF MSWINDOWS}
function GetHandle: THandle; override;
{$ENDIF}
procedure Signal; override;
function isSignalled: boolean; override;
function isSignalled_IsSupported: boolean; override;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; override;
function Pool: TObjectQueue; override;
function SignalState: TSignalState; override;
function AsMWObject: TObject; override;
private
FBase: TOtlEvent;
FManual: boolean;
FShadow: TSignalState;
FWaiters: cardinal;
FisLight: boolean;
[Volatile] FLock: TSBDSpinLock; // Only used for manual events.
FAsyncClear: boolean;
procedure SetEvent;
procedure ResetEvent;
function isManualEvent: boolean;
function isLight: boolean;
public
constructor CreateAsKernal( const APool: TSynchroFactory; AManual, AInitialState: boolean);
constructor CreateAsLight( const APool: TSynchroFactory; AManual, AInitialState: boolean; SpinMax: cardinal);
destructor Destroy; override;
public
/// <remarks>Although syntactically "public", semantically this is "private"
/// and for internal use only. Please do not use. </remarks>
procedure Reconfigure( Manual: boolean; Value: cardinal); virtual;
end;
TRecycleSemaphore = class( TRecycleSynchro, ISemaphore)
public
constructor Create( const APool: TSynchroFactory; AInitialCount: cardinal);
destructor Destroy; override;
protected
{$IFDEF MSWINDOWS}
function GetHandle: THandle; override;
{$ENDIF}
procedure Signal; override;
function isSignalled: boolean; override;
function isSignalled_IsSupported: boolean; override;
function SignalState: TSignalState; override;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; override;
function Pool: TObjectQueue; override;
function AsMWObject: TObject; override;
private type
TSimulatedSemaphore = class( TFunctionalEvent)
public
constructor Create( AInitialValue: cardinal);
destructor Destroy; override;
procedure Reset;
procedure ReconfigureInitial( Value: cardinal);
function SignalState: TSignalState;
protected
[Volatile]
FCurrentValue: TVolatileUint32;
FInitialValue: cardinal;
procedure SignalTest( doAcquire: boolean; var wasSuccessfullyAcquired: boolean; var isInSignalledState: boolean); override;
private
[Volatile] FLock: TSBDSpinLock;
end;
private
FBase: TSimulatedSemaphore;
function InitialValue: cardinal;
function Value: cardinal;
procedure Reset;
function isValueTesting_IsSupported: boolean;
public
/// <remarks>Although syntactically "public", semantically this is "private"
/// and for internal use only. Please do not use. </remarks>
procedure Reconfigure( Value: cardinal); virtual;
end;
TNativeSemaphore = class( TInterfacedObject, ISemaphore)
public
constructor Create( AInitialCount: cardinal);
destructor Destroy; override;
private
FBase: TOtlSemaphore;
FInitial: cardinal;
FWaiters: cardinal;
FShadow: cardinal;
[Volatile] FLock: TSBDSpinLock;
procedure Signal;
function isSignalled: boolean;
function isSignalled_IsSupported: boolean;
function SignalState: TSignalState;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult;
function AsObject: TObject;
function IsPoolManaged: boolean;
function InitialValue: cardinal;
function Value: cardinal;
procedure Reset;
function isValueTesting_IsSupported: boolean;
function IsModular: boolean;
{$IFDEF MSWINDOWS}
function GetHandle: THandle;
{$ENDIF}
protected
function AsMWObject: TObject;
end;
TRecycleCrit = class( TRecycleObject, ILock)
private
[Volatile] FLockCount: TVolatileInt32;
protected
function Pool: TObjectQueue; override;
function SignalState: TSignalState; override;
function LockCount: integer;
public
constructor Create( const APool: TSynchroFactory);
destructor Destroy; override;
private
FCrit: TCriticalSection;
procedure Enter;
procedure Leave;
function IsKernalMode: boolean;
function AsSpinLock: PSBDSpinLock;
function AsCriticalSection: TCriticalSection;
end;
TSpinIntf = class( TInterfacedObject, ILock)
public
constructor Create;
destructor Destroy; override;
private
[Volatile] FLock: TSBDSpinLock;
function LockCount: integer;
procedure Enter;
procedure Leave;
function AsObject: TObject;
function IsPoolManaged: boolean;
function IsKernalMode: boolean;
function AsSpinLock: PSBDSpinLock;
function AsCriticalSection: TCriticalSection;
end;
TRecycleCountDown = class( TRecycleSynchro, ICountDown)
protected
function Pool: TObjectQueue; override;
procedure Signal; override;
function isSignalled: boolean; override;
function isSignalled_IsSupported: boolean; override;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; override;
public
constructor Create( const APool: TSynchroFactory; AInitialValue: cardinal);
destructor Destroy; override;
private
FBase: TCountDown;
function InitialValue: cardinal;
function Value: cardinal;
function SignalHit: boolean;
function Allocate: cardinal; // Signal and return the value.
procedure Reconfigure( AInitial: cardinal);
procedure CounterSignal;
end;
TRecycleCountUp = class( TRecycleSynchro, ICountUp)
protected
function Pool: TObjectQueue; override;
procedure Signal; override;
function isSignalled: boolean; override;
function isSignalled_IsSupported: boolean; override;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; override;
public
constructor Create( const APool: TSynchroFactory; AInitial, AMaxValue: cardinal);
destructor Destroy; override;
private
FBase: TCountUp;
function InitialValue: cardinal;
function MaxValue: cardinal;
function Value: cardinal;
function SignalHit: boolean;
procedure Reconfigure( AInitial, AMaxValue: cardinal);
end;
TRecycleFunctional = class( TRecycleSynchro)
public
constructor Create( const APool: TSynchroFactory; ASignalTest: TEventFunction; APLock: PSBDSpinLock);
destructor Destroy; override;
protected
procedure Signal; override;
function isSignalled: boolean; override;
function isSignalled_IsSupported: boolean; override;
function Pool: TObjectQueue; override;
procedure ReleaseConfiguration; override;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; override;
private
FBase: TFunctionalEvent;
procedure Reconfigure( ASignalTest: TEventFunction; APLock: PSBDSpinLock);
end;
TIntfMREW = class( TInterfacedObject, IOtlMREW)
private
FMREW: TOtlMREW;
function EnterRead( Timeout: cardinal = INFINITE): TWaitResult;
procedure ExitRead;
function EnterWrite( Timeout: cardinal = INFINITE): TWaitResult;
procedure ExitWrite;
function ReaderCount: integer;
function AsReadLock: ILock;
function AsWriteLock: ILock;
private type
TFacade = class( TInterfacedObject, IInterface, ILock)
private
FController: TIntfMREW;
function AsObject: TObject;
function IsPoolManaged: boolean;
function IsKernalMode: boolean;
function AsSpinLock: PSBDSpinLock;
function AsCriticalSection: TCriticalSection;
protected
procedure Enter; virtual; abstract;
procedure Leave; virtual; abstract;
function LockCount: integer; virtual;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
end;
TReaderLock = class( TFacade)
protected
procedure Enter; override;
procedure Leave; override;
function LockCount: integer; override;
end;
TWriterLock = class( TFacade)
protected
procedure Enter; override;
procedure Leave; override;
function LockCount: integer; override;
end;
private
FReadFacade : TReaderLock;
FWriteFacade: TWriterLock;
public
constructor Create;
destructor Destroy; override;
end;
function TSynchroFactory.AcquireCountDown(
InitialValue: cardinal; AReUse: boolean): ICountDown;
var
Pool: TSynchroFactory;
Addend: TRecycleCountDown;
Queue: TObjectQueue;
begin
Queue := FCountDowns;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleCountDown;
Addend.FRefCount.Write( 0);
Addend.Reconfigure( InitialValue)
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleCountDown.Create( Pool, InitialValue)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave
end;
function TSynchroFactory.AcquireCountUp(
InitialValue, MaxValue: cardinal; AReUse: boolean): ICountUp;
var
Pool: TSynchroFactory;
Addend: TRecycleCountUp;
Queue: TObjectQueue;
begin
Queue := FCountUps;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleCountUp;
Addend.FRefCount.Write( 0);
Addend.Reconfigure( InitialValue, MaxValue)
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleCountUp.Create( Pool, InitialValue, MaxValue)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave
end;
class function TSynchroFactory.AcquireCriticalSection: ILock;
begin
result := TRecycleCrit.Create( nil)
end;
function TSynchroFactory.AcquireCriticalSection( AReUse: boolean): ILock;
var
Pool: TSynchroFactory;
Addend: TRecycleCrit;
Queue: TObjectQueue;
begin
Queue := FCrits;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleCrit;
Addend.FRefCount.Write( 0)
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleCrit.Create( Pool)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave
end;
function TSynchroFactory.AcquireFunctionalEvent(
ASignalTest: TEventFunction; APLock: PSBDSpinLock; AReUse: boolean): ISynchro;
var
Pool: TSynchroFactory;
Addend: TRecycleFunctional;
Queue: TObjectQueue;
doPulse: boolean;
begin
doPulse := False;
Queue := FFunctionals;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleFunctional;
Addend.FRefCount.Write( 0);
Addend.Reconfigure( ASignalTest, APlock);
doPulse := True;
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleFunctional.Create( Pool, ASignalTest, APLock)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave;
if doPulse then
Addend.Signal
end;
function TSynchroFactory.AcquireKernelEvent(
AManual, AInitialState, AReUse: boolean): IEvent;
var
Pool: TSynchroFactory;
Addend: TRecycleEvent;
Queue: TObjectQueue;
begin
if AManual then
Queue := FManualKernelEvents
else
Queue := FAutoKernelEvents;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleEvent;
Addend.FRefCount.Write( 0);
if (not (Addend.isSignalled_IsSupported)) or
(Addend.isSignalled <> AInitialState) then
begin
if AInitialState then
Addend.SetEvent
else
Addend.ResetEvent
end
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleEvent.CreateAsKernal( Pool, AManual, AInitialState)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave
end;
function TSynchroFactory.AcquireLightEvent(
AManual, AInitialState: boolean; SpinMax: integer; AReUse: boolean): IEvent;
var
Pool: TSynchroFactory;
Addend: TRecycleEvent;
Queue: TObjectQueue;
begin
Queue := FLightEvents;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleEvent;
Addend.Reconfigure( AManual, SpinMax);
Addend.FRefCount.Write( 0);
if (not (Addend.isSignalled_IsSupported)) or
(Addend.isSignalled <> AInitialState) then
begin
if AInitialState then
Addend.SetEvent
else
Addend.ResetEvent
end
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleEvent.CreateAsLight( Pool, AManual, AInitialState, SpinMax)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave
end;
function TSynchroFactory.AcquireNativeSemaphore( AInitialCount: cardinal): ISemaphore;
begin
result := TNativeSemaphore.Create( AInitialCount)
end;
function TSynchroFactory.AcquireTestableSemaphore( AInitialCount: cardinal; AReUse: boolean): ISemaphore;
var
Pool: TSynchroFactory;
Addend: TRecycleSemaphore;
Queue: TObjectQueue;
begin
Queue := FSemaphores;
FLock.Enter;
if AReUse and (Queue.Count > 0) then
begin
Addend := Queue.Dequeue as TRecycleSemaphore;
Addend.Reconfigure( AInitialCount);
Addend.FRefCount.Write( 0);
end
else
begin
if AReUse then
Pool := self
else
Pool := nil;
Addend := TRecycleSemaphore.Create( Pool, AInitialCount)
end;
result := Addend;
if AReUse then
begin
Addend.FAcquired := True;
FAquisitionCount.Increment
end;
FLock.Leave
end;
function TSynchroFactory.AcquireSpinLock: ILock;
begin
result := TSpinIntf.Create
end;
function TSynchroFactory.CanDestroy: boolean;
begin
result := FAquisitionCount.Read = 0
end;
constructor TSynchroFactory.Create( APoolMax: cardinal);
begin
FLock.Initialize;
FPoolMax := APoolMax;
FManualKernelEvents := TObjectQueue.Create;
FAutoKernelEvents := TObjectQueue.Create;
FLightEvents := TObjectQueue.Create;
FSemaphores := TObjectQueue.Create;
FCrits := TObjectQueue.Create;
FCountDowns := TObjectQueue.Create;
FCountUps := TObjectQueue.Create;
FFunctionals := TObjectQueue.Create
end;
destructor TSynchroFactory.Destroy;
{$IFNDEF AUTOREFCOUNT}
var
Queue: TObjectQueue;
{$ENDIF}
begin
if FAquisitionCount.Read <> 0 then
raise TParallelException.Create( ECannotDestroySynchroFactory);
PurgeAllPools;
{$IFNDEF AUTOREFCOUNT}
for Queue in Queues do
Queue.Free;
{$ENDIF}
inherited;
FLock.Finalize
end;
procedure TSynchroFactory.PurgeAllPools;
begin
FLock.WithinLock( procedure
var
Queue: TObjectQueue;
begin
for Queue in Queues do
while Queue.Count > 0 do
{$IFDEF AUTOREFCOUNT}
Queue.Dequeue
{$ELSE}
Queue.Dequeue.Free
{$ENDIF}
end)
end;
function TSynchroFactory.Queues: TArray<TObjectQueue>;
begin
result := [FManualKernelEvents, FAutoKernelEvents, FLightEvents, FSemaphores, FCrits, FCountDowns, FCountUps, FFunctionals];
end;
procedure TRecycleObject.AfterConstruction;
begin
FRefCount.Decrement
end;
function TRecycleObject.AsObject: TObject;
begin
result := self
end;
class function TRecycleObject.NewInstance: TObject;
begin
result := inherited NewInstance;
TRecycleObject( result).FRefCount.Initialize( 1)
end;
constructor TRecycleObject.Create( const APool: TSynchroFactory);
begin
FPool := APool
end;
function TRecycleObject.GetRefCount: integer;
begin
result := FRefCount.Read;
if ((result and objPooledFlag) <> 0) or (result <= -2) then
result := -1
end;
function TRecycleObject.IsPoolManaged: boolean;
begin
result := assigned( FPool)
end;
function TRecycleObject.QueryInterface( const IID: TGUID; out Obj): HResult;
begin
if GetInterface( IID, Obj) then
result := 0
else
result := E_NOINTERFACE
end;
procedure TRecycleObject.ReleaseConfiguration;
begin
end;
function TRecycleObject._AddRef: Integer;
begin
result := FRefCount.Increment
end;
function TRecycleObject._Release: Integer;
var
OldValue: integer;
PooledByOther: boolean;
Pl: TObjectQueue;
begin
result := FRefCount.Decrement;
if result <> 0 then exit;
repeat
OldValue := FRefCount.Read;
PooledByOther := (OldValue and objPooledFlag) = objPooledFlag;
until PooledByOther or FRefCount.CompareAndExchange( OldValue, OldValue or objPooledFlag);
if PooledByOther then exit;
ReleaseConfiguration;
if assigned( FPool) then
begin
if FAcquired then
begin
FAcquired := False;
FPool.FAquisitionCount.Decrement
end;
Pl := Pool;
if cardinal( Pl.Count) < FPool.FPoolMax then
Pool.Enqueue( self)
else
begin
FPool := nil;
Destroy
end
end
else
Destroy
end;
function TRecycleEvent.AsMWObject: TObject;
begin
result := FBase.AsMWObject
end;
constructor TRecycleEvent.CreateAsKernal( const APool: TSynchroFactory; AManual, AInitialState: boolean);
begin
FLock.Initialize;
inherited Create( APool);
FManual := AManual;
FisLight := False;
if AInitialState then
FShadow := esSignalled
else
FShadow := esNotSignalled;
FWaiters := 0;
FAsyncClear := False;
FBase := TKernelEvent.Create( AManual, AInitialState)
end;
constructor TRecycleEvent.CreateAsLight(
const APool: TSynchroFactory; AManual, AInitialState: boolean; SpinMax: cardinal);
begin
FLock.Initialize;
inherited Create( APool);
FManual := AManual;
FisLight := True;
if AInitialState then
FShadow := esSignalled
else
FShadow := esNotSignalled;
FWaiters := 0;
FAsyncClear := False;
FBase := TLightEvent.Create( AManual, AInitialState, SpinMax)
end;
destructor TRecycleEvent.Destroy;
begin
FBase.Free;
FLock.Finalize;
inherited
end;
{$IFDEF MSWINDOWS}
function TRecycleEvent.GetHandle: THandle;
begin
if FisLight then
result := 0
else
result := FBase.Handle
end;
{$ENDIF}
function TRecycleEvent.isLight: boolean;
begin
result := FisLight
end;
function TRecycleEvent.isManualEvent: boolean;
begin
result := FManual
end;
function TRecycleEvent.isSignalled: boolean;
var
Ok: boolean;
begin
if FisLight then
result := FBase.isSignalled
else if FManual then
result := FShadow = esSignalled
else
begin
FLock.Enter;
Ok := FShadow <> esUnknown;
result := FShadow = esSignalled;
FLock.Leave;
if not Ok then
raise TParallelException.Create( EIsSignalledNotSupported)
end
end;
function TRecycleEvent.isSignalled_IsSupported: boolean;
begin
result := FisLight or FManual
end;
function TRecycleEvent.Pool: TObjectQueue;
begin
if not assigned( FPool) then
result := nil
else if isLight then
result := FPool.FLightEvents
else if FManual then
result := FPool.FManualKernelEvents
else