forked from gabr42/OmniThreadLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOtl.Parallel.SynchroPrimitives.ModularLevel.pas
1248 lines (1073 loc) · 33.1 KB
/
Otl.Parallel.SynchroPrimitives.ModularLevel.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.ModularLevel;
// IMPORTANT!
// READ THE COMMENTS IN UNIT Otl.Parallel.SynchroPrimitives .
{$I OtlOptions.inc}
interface
uses Otl.Parallel.SynchroPrimitives.InterfaceLevel, System.SyncObjs
, System.Classes, System.SysUtils, System.Generics.Collections
, Otl.Parallel.SynchroPrimitives.ConditionVariables
{$IFDEF MSWINDOWS}
, Winapi.Windows
{$ENDIF}
, Otl.Parallel.Atomic;
const
FOREVER = INFINITE;
MaxCardinal: cardinal = cardinal( -1);
type
ISimpleConditionEvent = interface ['{1EAEEE8F-54E1-44B5-BE3C-AFC14A465F74}']
procedure Pulse;
function WaitFor: TWaitResult;
end;
ICompositeSynchro = interface
function WaitFor(
var SignallerIdx: integer; TimeOut: cardinal = FOREVER): TWaitResult;
function Factors: TSynchroArray;
function Datum: TObject;
function isSignalled: boolean;
end;
type
ISynchroExInternal = interface;
ISynchroObserver = interface ['{C1EBE331-613F-4D0F-B645-CF2925F55B22}']
procedure PossibleStateChange( const Source: ISynchroExInternal; Token: TObject);
end;
ISynchroExInternal = interface( ISynchro) ['{A3D01DFF-CF6A-4913-8A34-E46C4C0FCF5F}']
function NativeMultiwaitObject: TObject;
function IsCompatibleNativeMWObject( Reference, Peer: TObject): boolean;
function NativeMultiwait( const Synchros: array of TObject;
Timeout: cardinal; AAll: boolean;
var SignallerIndex: integer): TWaitResult;
procedure RegisterDirectClient( Delta: integer);
procedure RegisterIndirectClient( Delta: integer);
procedure RegisterDedicatedSoleIndirectClient( Delta: integer);
function PermitsDirectClients: boolean;
function PermitsIndirectClients: boolean;
function PermitsDedicatedSoleIndirectClient: boolean;
function LockingMechanism: TLockingMechanism;
function IsResourceCounting: boolean;
function ConsumeResource: TWaitResult;
procedure Enrol( const Observer: ISynchroObserver; Token: TObject);
procedure Unenrol( ObserverToken: TObject);
function UnionLock: ILock;
end;
TWaitPropagation = (NoConsume, ConsumeOneSignalled, ConsumeAllSignalled);
TConditionTest = reference to function(
var SignallerIdx: integer;
const Peers: TSynchroArray; Datum: TObject): boolean;
TTestClass = (TestAny, TestAll, TestCustom);
TCompositeSynchro = class( TInterfacedObject, ICompositeSynchro, ISynchro, ISynchroExInternal)
private type
TImplementation = ({$IFDEF MSWINDOWS}Direct, {$ENDIF} Indirect, Solo);
private
{$IFDEF MSWINDOWS}
function GetHandle: THandle;
{$ENDIF}
procedure Signal;
function isSignalled: boolean;
function isSignalled_IsSupported: boolean;
function SignalState: Otl.Parallel.SynchroPrimitives.InterfaceLevel.TSignalState;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; overload;
function WaitFor( var SignallerIdx: integer; TimeOut: cardinal = FOREVER): TWaitResult; overload;
function AsObject: TObject;
function IsPoolManaged: boolean;
function IsModular: boolean;
function LockingMechanism: TLockingMechanism;
function Factors: TSynchroArray;
function Datum: TObject;
function NativeMultiwaitObject: TObject;
function IsCompatibleNativeMWObject( Reference, Peer: TObject): boolean;
function NativeMultiwait( const Synchros: array of TObject;
Timeout: cardinal; AAll: boolean;
var SignallerIndex: integer): TWaitResult;
procedure RegisterDirectClient( Delta: integer);
procedure RegisterIndirectClient( Delta: integer);
procedure RegisterDedicatedSoleIndirectClient( Delta: integer);
function PermitsDirectClients: boolean;
function PermitsIndirectClients: boolean;
function PermitsDedicatedSoleIndirectClient: boolean;
function IsResourceCounting: boolean;
function ConsumeResource: TWaitResult;
procedure Enrol( const Observer: ISynchroObserver; Token: TObject);
procedure Unenrol( ObserverToken: TObject);
function UnionLock: ILock;
procedure BubbleUp( var Me: ISynchroExInternal);
public
constructor Create( const AFactors: TSynchroArray; AEventFactory: TSynchroFactory;
APropagation: TWaitPropagation; ATest: TConditionTest;
ATestClass: TTestClass; AAllowSolo: boolean);
destructor Destroy; override;
function AsMWObject: TObject;
class function IsAll( var SignallerIdx: integer; const Peers: TSynchroArray; Datum: TObject): boolean;
class function IsAny( var SignallerIdx: integer; const Peers: TSynchroArray; Datum: TObject): boolean;
class function AnyTest: TConditionTest;
class function AllTest: TConditionTest;
private
[Volatile] FisSignalled: boolean;
FFactors: TSynchroArray;
FMemberCount: integer;
FEventFactory: TSynchroFactory;
FLock: ILock;
FDatum: TObject;
FCondVar: Otl.Parallel.SynchroPrimitives.ConditionVariables.TSBDConditionVariable;
FImplementation: TImplementation;
FPropagation: TWaitPropagation;
FTest: TConditionTest;
FTestClass: TTestClass;
FAllowSolo: boolean;
FHasAtLeastOneResourceCounter: boolean;
FObservers: TDictionary<TObject,ISynchroObserver>;
FRegistered: boolean;
private type
TMemberObserver = class( TInterfacedObject, ISynchroObserver)
private
{$IFDEF AUTOREFCOUNT} [weak] {$ENDIF}
[Volatile] FOwner: TCompositeSynchro;
procedure PossibleStateChange( const Source: ISynchroExInternal; Token: TObject);
public
constructor Create( AOwner: TCompositeSynchro);
end;
private
FMemberObserver: TMemberObserver;
{$IFDEF MSWINDOWS}
FSynchroObjs: array of TObject; // Actually array of THandle.
{$ENDIF}
end;
TModularSynchro = class abstract( TInterfacedObject, ISynchro, ISynchroExInternal)
protected
{$IFDEF MSWINDOWS}
function GetHandle: THandle;
{$ENDIF}
procedure Signal; virtual;
function isSignalled: boolean;
function isSignalled_IsSupported: boolean;
function SignalState: Otl.Parallel.SynchroPrimitives.InterfaceLevel.TSignalState;
function WaitFor( Timeout: cardinal = INFINITE): TWaitResult; virtual;
function AsObject: TObject;
function IsPoolManaged: boolean;
function IsModular: boolean;
function NativeMultiwaitObject: TObject;
function IsCompatibleNativeMWObject( Reference, Peer: TObject): boolean;
function NativeMultiwait( const Synchros: array of TObject;
Timeout: cardinal; AAll: boolean;
var SignallerIndex: integer): TWaitResult;
procedure RegisterDirectClient( Delta: integer);
procedure RegisterIndirectClient( Delta: integer);
procedure RegisterDedicatedSoleIndirectClient( Delta: integer);
function PermitsDirectClients: boolean;
function PermitsIndirectClients: boolean;
function PermitsDedicatedSoleIndirectClient: boolean;
function LockingMechanism: TLockingMechanism;
function IsResourceCounting: boolean; virtual;
function ConsumeResource: TWaitResult; virtual;
procedure Enrol( const Observer: ISynchroObserver; Token: TObject);
procedure Unenrol( ObserverToken: TObject);
function UnionLock: ILock;
procedure ObserverProc( Proc: TProc);
public
FUnionLock: ILock;
FBase : ISynchro;
FBaseEx: ISynchroExInternal;
FEventFactory: TSynchroFactory;
constructor Create( const ABase: ISynchro; const AUnionLock: ILock; AEventFactory: TSynchroFactory);
destructor Destroy; override;
function AsMWObject: TObject;
private
[Volatile] FDirectWaiters : TVolatileInt32;
[Volatile] FIndirectWaiters: TVolatileInt32;
FObservers: TDictionary<TObject,ISynchroObserver>;
FDedicatedToOneIndirectWaiter: boolean;
end;
TModularEvent = class( TModularSynchro, IEvent)
protected
procedure SetEvent;
procedure ResetEvent;
function isManualEvent: boolean;
function isLight: boolean;
procedure Signal; override;
function IsResourceCounting: boolean; override;
function ConsumeResource: TWaitResult; override;
public
FBaseEvent: IEvent;
constructor Create( const ABase: IEvent; const AUnionLock: ILock; AEventFactory: TSynchroFactory);
end;
TModularSemaphore = class( TModularSynchro, ISemaphore)
protected
function InitialValue: cardinal;
function Value: cardinal;
procedure Reset;
function isValueTesting_IsSupported: boolean;
procedure Signal; override;
function IsResourceCounting: boolean; override;
function ConsumeResource: TWaitResult; override;
public
FBaseSem: ISemaphore;
constructor Create( const ABase: ISemaphore; const AUnionLock: ILock; AEventFactory: TSynchroFactory);
end;
TModularCountDown = class( TModularSynchro, ICountDown)
protected
function InitialValue: cardinal;
function Value: cardinal;
function SignalHit: boolean;
procedure CounterSignal;
function Allocate: cardinal;
procedure Signal; override;
function IsResourceCounting: boolean; override;
function ConsumeResource: TWaitResult; override;
public
FBaseCountDown: ICountDown;
constructor Create( const ABase: ICountDown; const AUnionLock: ILock; AEventFactory: TSynchroFactory);
end;
implementation
uses System.Diagnostics, Otl.Parallel.Errors;
constructor TCompositeSynchro.Create(
const AFactors: TSynchroArray; AEventFactory: TSynchroFactory;
APropagation: TWaitPropagation; ATest: TConditionTest;
ATestClass: TTestClass; AAllowSolo: boolean);
var
i: integer;
Ok: boolean;
Memb: ISynchroExInternal;
Dummy: integer;
MWObj: TObject;
begin
FRegistered := False;
// Test for the direct solution
FMemberCount := Length( AFactors);
if FMemberCount = 0 then
raise TParallelException.Create( ECompositeNeedsOneFactor);
FPropagation := APropagation;
FDatum := Datum;
FTest := ATest;
FTestClass := ATestClass;
FObservers := TDictionary<TObject,ISynchroObserver>.Create;
SetLength( FFactors, FMemberCount);
FEventFactory := AEventFactory;
FAllowSolo := AAllowSolo;
case FTestClass of
TestAny : FTest := IsAny;
TestAll : FTest := IsAll;
end;
for i := 1 to FMemberCount - 1 do
begin
FFactors[ i] := AFactors[i];
if not FFactors[ i].IsModular then
raise TParallelException.Create( EOnlyModularCombinable);
end;
if Supports( FFactors[0], ISynchroExInternal, Memb) then
begin
MWObj := Memb.NativeMultiwaitObject;
FLock := Memb.UnionLock
end
else
begin
MWObj := nil;
FLock := nil
end;
Ok := True;
for i := 1 to FMemberCount - 1 do
begin
if Supports( FFactors[i], ISynchroExInternal, Memb) then
begin
Ok := Ok and assigned( FLock) and (FLock = Memb.UnionLock);
if assigned( MWObj) and Memb.IsCompatibleNativeMWObject( MWObj, Memb.NativeMultiwaitObject) then
MWObj := nil
end
end;
if (not Ok) or (not assigned( FLock.AsCriticalSection)) then
FLock := nil;
if assigned( FLock)
{$IFNDEF USE_EMBARCADERO_TConditionVariableCS} {$IFNDEF MSWINDOWS}
and assigned( FEventFactory)
{$ENDIF} {$ENDIF} then
FCondVar := TSBDConditionVariable.Create(
{$IFNDEF USE_EMBARCADERO_TConditionVariableCS} {$IFNDEF MSWINDOWS}
AEventFactory, True,
{$ENDIF} {$ENDIF}
FLock)
else
FCondVar := nil;
Ok := FMemberCount = 1;
if Ok then
FImplementation := Solo
else
begin
{$IFDEF MSWINDOWS}
if assigned( MWObj) then
for i := 0 to FMemberCount - 1 do
begin
Ok := Supports( FFactors[i], ISynchroExInternal, Memb) and
Memb.PermitsDirectClients and
(Memb.NativeMultiwaitObject <> nil) and
(((FPropagation = ConsumeOneSignalled) and (FTestClass = TestAny)) or
((FPropagation = ConsumeAllSignalled) and (FTestClass = TestAll)));
if not Ok then break
end;
if Ok then
begin
FImplementation := Direct;
FreeAndNil( FCondVar)
end;
{$ENDIF}
end;
FHasAtLeastOneResourceCounter := False;
if (not Ok) and assigned( FCondVar) then
begin
for i := 0 to FMemberCount - 1 do
begin
FHasAtLeastOneResourceCounter := Supports( FFactors[i], ISynchroExInternal, Memb) and
Memb.IsResourceCounting;
if FHasAtLeastOneResourceCounter then break
end;
for i := 0 to FMemberCount - 1 do
begin
Ok := Supports( FFactors[i], ISynchroExInternal, Memb) and
Memb.PermitsIndirectClients;
if Ok and (FPropagation = ConsumeAllSignalled) and FHasAtLeastOneResourceCounter then
Ok := Memb.PermitsDedicatedSoleIndirectClient;
if not Ok then break
end;
FImplementation := Indirect
end;
if Ok and (FImplementation = Solo) and (not AAllowSolo) then
Ok := False;
if not Ok then
raise TParallelException.Create( ECompositeSynchroMixedBag);
case FTestClass of
TestAny : FisSignalled := IsAny( Dummy, FFactors, nil);
TestAll : FisSignalled := IsAll( Dummy, FFactors, nil);
TestCustom: FisSignalled := FTest( Dummy, FFactors, FDatum);
end;
case FImplementation of
{$IFDEF MSWINDOWS}
Direct:
begin
SetLength( FSynchroObjs, FMemberCount);
for i := 0 to FMemberCount - 1 do
begin
if Supports( FFactors[i], ISynchroExInternal, Memb) then
FSynchroObjs[i] := Memb.NativeMultiwaitObject;
Memb.RegisterDirectClient( +1);
end;
if FTestClass <> TestCustom then
FTest := nil;
FRegistered := True
end;
{$ENDIF}
Indirect, Solo:
begin
FMemberObserver := TMemberObserver.Create( self);
FMemberObserver._AddRef;
for i := 0 to FMemberCount - 1 do
begin
if not Supports( FFactors[i], ISynchroExInternal, Memb) then continue;
if (FPropagation = ConsumeAllSignalled) and FHasAtLeastOneResourceCounter then
Memb.RegisterDedicatedSoleIndirectClient( +1)
else
Memb.RegisterIndirectClient( +1);
Memb.Enrol( FMemberObserver, TObject( i))
end;
FRegistered := True;
if not assigned( FLock) then
begin
if assigned( FEventFactory) then
FLock := FEventFactory.AcquireCriticalSection( True)
else
FLock := TSynchroFactory.AcquireCriticalSection
end
end
end
end;
destructor TCompositeSynchro.Destroy;
var
i: integer;
Memb: ISynchroExInternal;
begin
if assigned( FLock) then
FLock.Enter;
FCondVar.Free;
if FRegistered then
case FImplementation of
{$IFDEF MSWINDOWS}
Direct:
begin
for i := 0 to FMemberCount - 1 do
if Supports( FFactors[i], ISynchroExInternal, Memb) then
Memb.RegisterDirectClient( -1);
end;
{$ENDIF}
Indirect, Solo:
begin
for i := 0 to FMemberCount - 1 do
begin
if not Supports( FFactors[i], ISynchroExInternal, Memb) then continue;
if (FPropagation = ConsumeAllSignalled) and FHasAtLeastOneResourceCounter then
Memb.RegisterDedicatedSoleIndirectClient( -1)
else
Memb.RegisterIndirectClient( -1);
Memb.Unenrol( TObject( i))
end
end
end;
SetLength( FFactors, 0);
FTest := nil;
if assigned( FMemberObserver) then
begin
FMemberObserver.FOwner := nil;
FMemberObserver._Release;
FMemberObserver := nil
end;
FObservers.Free;
{$IFDEF MSWINDOWS}
SetLength( FSynchroObjs, 0);
{$ENDIF}
inherited;
if assigned( FLock) then
FLock.Leave;
FLock := nil
end;
class function TCompositeSynchro.AllTest: TConditionTest;
begin
result := function( var SignallerIdx: integer; const Peers: TSynchroArray; Datum: TObject): boolean
begin
result := IsAll( SignallerIdx, Peers, Datum)
end
end;
class function TCompositeSynchro.AnyTest: TConditionTest;
begin
result := function( var SignallerIdx: integer; const Peers: TSynchroArray; Datum: TObject): boolean
begin
result := IsAny( SignallerIdx, Peers, Datum)
end
end;
function TCompositeSynchro.AsMWObject: TObject;
begin
result := nil
end;
function TCompositeSynchro.AsObject: TObject;
begin
result := self
end;
function TCompositeSynchro.Datum: TObject;
begin
result := FDatum
end;
function TCompositeSynchro.ConsumeResource: TWaitResult;
begin
result := WaitFor( 0)
end;
procedure TCompositeSynchro.Enrol(
const Observer: ISynchroObserver; Token: TObject);
begin
FObservers.AddOrSetValue( Token, Observer)
end;
function TCompositeSynchro.Factors: TSynchroArray;
begin
result := FFactors
end;
{$IFDEF MSWINDOWS}
function TCompositeSynchro.GetHandle: THandle;
begin
result := 0
end;
{$ENDIF}
class function TCompositeSynchro.IsAll(
var SignallerIdx: integer; const Peers: TSynchroArray; Datum: TObject): boolean;
var
Member: ISynchro;
begin
result := True;
for Member in Peers do
begin
result := Member.SignalState = esSignalled;
if not result then break
end
end;
class function TCompositeSynchro.IsAny(
var SignallerIdx: integer; const Peers: TSynchroArray; Datum: TObject): boolean;
var
Idx: integer;
begin
result := False;
SignallerIdx := -1;
for Idx := 0 to Length( Peers) - 1 do
begin
result := Peers[ Idx].SignalState = esSignalled;
if not result then continue;
SignallerIdx := Idx;
break
end
end;
function TCompositeSynchro.IsCompatibleNativeMWObject(
Reference, Peer: TObject): boolean;
begin
result := False
end;
function TCompositeSynchro.IsModular: boolean;
begin
result := True
end;
function TCompositeSynchro.IsPoolManaged: boolean;
begin
result := False
end;
function TCompositeSynchro.IsResourceCounting: boolean;
begin
result := False
end;
function TCompositeSynchro.isSignalled: boolean;
begin
result := FIsSignalled
end;
function TCompositeSynchro.isSignalled_IsSupported: boolean;
begin
result := True
end;
function TCompositeSynchro.LockingMechanism: TLockingMechanism;
var
isKernal: boolean;
{$IFDEF MSWINDOW}
Memb: ISynchroExInternal;
{$ENDIF}
begin
isKernal := False;
case self.FImplementation of
{$IFDEF MSWINDOW}
Direct: isKernal := (Length( FFactors) > 0) and
Supports( FFactors[0], ISynchroExInternal, Memb) and
(Memb.LockingMechanism = KernalLocking);
{$ENDIF}
Indirect, Solo: isKernal := assigned( FLock) and FLock.IsKernalMode
end;
if isKernal then
result := KernalLocking
else
result := BusLocking
end;
function TCompositeSynchro.NativeMultiwait(
const Synchros: array of TObject; Timeout: cardinal; AAll: boolean; var SignallerIndex: integer): TWaitResult;
begin
result := wrError
end;
function TCompositeSynchro.NativeMultiwaitObject: TObject;
begin
result := nil
end;
function TCompositeSynchro.PermitsDedicatedSoleIndirectClient: boolean;
begin
result := False
end;
function TCompositeSynchro.PermitsDirectClients: boolean;
begin
result := False
end;
function TCompositeSynchro.PermitsIndirectClients: boolean;
begin
result := True
end;
procedure TCompositeSynchro.RegisterDedicatedSoleIndirectClient( Delta: integer);
begin
end;
procedure TCompositeSynchro.RegisterDirectClient( Delta: integer);
begin
end;
procedure TCompositeSynchro.RegisterIndirectClient( Delta: integer);
begin
end;
procedure TCompositeSynchro.Signal;
begin
if (FImplementation in [Indirect, Solo]) and assigned( FCondVar) then
FCondVar.Pulse
end;
function TCompositeSynchro.SignalState: Otl.Parallel.SynchroPrimitives.InterfaceLevel.TSignalState;
begin
if FisSignalled then
result := esSignalled
else
result := esNotSignalled
end;
procedure TCompositeSynchro.Unenrol( ObserverToken: TObject);
begin
FObservers.Remove( ObserverToken)
end;
function TCompositeSynchro.UnionLock: ILock;
begin
result := FLock
end;
procedure TCompositeSynchro.BubbleUp( var Me: ISynchroExInternal);
var
Obs: TPair<TObject,ISynchroObserver>;
Dummy: integer;
begin
if FTest( Dummy, FFactors, FDatum) <> FisSignalled then
begin
FisSignalled := not FisSignalled;
if assigned( FObservers) then
begin
if not assigned( Me) then
Me := self;
for Obs in FObservers do
Obs.Value.PossibleStateChange( Me, Obs.Key)
end
end
end;
function TCompositeSynchro.WaitFor(
var SignallerIdx: integer; TimeOut: cardinal): TWaitResult;
var
Me: ISynchroExInternal;
Idx: integer;
ImproperConstructionDetected: boolean;
begin
Idx := -1;
result := wrError;
ImproperConstructionDetected := False;
case FImplementation of
Indirect, Solo:
begin
result := FCondVar.WaitFor( TimeOut,
function( doAquireResource: boolean): boolean
var
ConsumedMembers, i: integer;
Synchro: ISynchroExInternal;
Confirmed: boolean;
begin
result := FTest( Idx, FFactors, FDatum);
if result and doAquireResource and
(FPropagation <> NoConsume) and FHasAtLeastOneResourceCounter then
begin
ConsumedMembers := 0;
for i := 0 to FMemberCount - 1 do
begin
if (((FPropagation = ConsumeOneSignalled) and (i = Idx)) or
(FPropagation = ConsumeAllSignalled)) and
Supports( FFactors[i], ISynchroExInternal, Synchro) and
Synchro.isSignalled and Synchro.IsResourceCounting then
begin
Confirmed := Synchro.ConsumeResource = wrSignaled;
Inc( ConsumedMembers);
if Confirmed then continue;
result := False;
if (FPropagation = ConsumeOneSignalled) or
((FPropagation = ConsumeAllSignalled) and (ConsumedMembers = 1)) then
begin
// We have a competitor waiter on one of the members and the competitor
// slipped in ahead of us, so loop back and try again.
end
else
begin
// This is an error. We should never get here if TConditionEvent is properly constructed.
// To prevent the possibility of this condition, we disallow members which have
// multiple waiters (including TConditionEvent) and where the propagation is ConsumeAllSignalled
// and at least one of the members are resource-counting synchros.
ImproperConstructionDetected := True;
result := True
end;
break
end
end
end
end,
procedure
begin
BubbleUp( Me)
end);
if ImproperConstructionDetected then
result := wrError;
if result = wrSignaled then
SignallerIdx := Idx
end;
{$IFDEF MSWINDOWS}
Direct:
begin
result := (FFactors[0] as ISynchroExInternal).NativeMultiwait( FSynchroObjs, TimeOut, FTestClass = TestAll, SignallerIdx);
if result in [wrSignaled, wrTimeOut] then
begin
FLock.Enter;
BubbleUp( Me);
FLock.Leave
end
end
{$ENDIF}
end;
end;
function TCompositeSynchro.WaitFor( Timeout: cardinal): TWaitResult;
var
Dummy: integer;
begin
result := WaitFor( Dummy, TimeOut)
end;
constructor TCompositeSynchro.TMemberObserver.Create( AOwner: TCompositeSynchro);
begin
FOwner := AOwner
end;
procedure TCompositeSynchro.TMemberObserver.PossibleStateChange(
const Source: ISynchroExInternal; Token: TObject);
begin
// Index of signaller is integer( Token)
FOwner.Signal
end;
constructor TModularSynchro.Create(
const ABase: ISynchro; const AUnionLock: ILock; AEventFactory: TSynchroFactory);
begin
FUnionLock := AUnionLock;
FBase := ABase;
Supports( FBase, ISynchroExInternal, FBaseEx);
FDirectWaiters.Initialize( 0);
FIndirectWaiters.Initialize( 0);
FObservers := TDictionary<TObject,ISynchroObserver>.Create;
FDedicatedToOneIndirectWaiter := False;
FEventFactory := AEventFactory
end;
destructor TModularSynchro.Destroy;
begin
FDirectWaiters.Finalize;
FIndirectWaiters.Finalize;
FObservers.Free;
inherited
end;
function TModularSynchro.AsMWObject: TObject;
begin
result := FBase.AsMWObject
end;
function TModularSynchro.AsObject: TObject;
begin
result := self
end;
function TModularSynchro.ConsumeResource: TWaitResult;
begin
result := FBase.WaitFor( 0)
end;
procedure TModularSynchro.Enrol(
const Observer: ISynchroObserver; Token: TObject);
begin
FObservers.AddOrSetValue( Token, Observer)
end;
{$IFDEF MSWINDOWS}
function TModularSynchro.GetHandle: THandle;
begin
if assigned( FBase) then
result := FBase.Handle
else
result := 0
end;
{$ENDIF}
function TModularSynchro.IsCompatibleNativeMWObject(
Reference, Peer: TObject): boolean;
begin
{$IFDEF MSWINDOWS}
result := assigned( Reference) and assigned( Peer) and
(Reference.ClassType = Peer.ClassType) and
(Reference <> Peer) and
(Reference is THandleObject) and (Peer is THandleObject)
{$ELSE}
result := False
{$ENDIF}
end;
function TModularSynchro.IsModular: boolean;
begin
result := True
end;
function TModularSynchro.IsPoolManaged: boolean;
begin
result := False
end;
function TModularSynchro.IsResourceCounting: boolean;
begin
result := False
end;
function TModularSynchro.isSignalled: boolean;
begin
result := FBase.isSignalled
end;
function TModularSynchro.isSignalled_IsSupported: boolean;
begin
result := FBase.isSignalled_IsSupported
end;
function TModularSynchro.LockingMechanism: TLockingMechanism;
begin
result := KernalLocking
end;
function TModularSynchro.NativeMultiwait(
const Synchros: array of TObject; Timeout: cardinal; AAll: boolean; var SignallerIndex: integer): TWaitResult;
{$IFDEF MSWINDOWS}
var
HandleObjs: THandleObjectArray;
L, i: integer;
Obj: TObject;
Ok: boolean;
SignaledObj: THandleObject;
{$ENDIF}
begin
SignallerIndex := -1;
{$IFDEF MSWINDOWS}
L := Length( Synchros);
Ok := False;
SetLength( HandleObjs, L);
for i := 0 to L - 1 do
begin
Obj := Synchros[i];
Ok := assigned( Obj) and (Obj is THandleObject);
if not Ok then break;
HandleObjs[i] := THandleObject( Obj)
end;
if Ok and (L >= 2) then
begin
result := THandleObject.WaitForMultiple( HandleObjs, Timeout, AAll, SignaledObj, False, 0);
if (result = wrSignaled) and (not AAll) then
for i := 0 to L - 1 do
begin
if HandleObjs[i] <> SignaledObj then continue;
SignallerIndex := i;
break
end
end
else
{$ENDIF}
result := wrError
end;
function TModularSynchro.NativeMultiwaitObject: TObject;
begin
result := nil
end;
procedure TModularSynchro.ObserverProc( Proc: TProc);
// This method assumes that RefCount > 0, so don't call from destructor.
var
Obs: TPair<TObject,ISynchroObserver>;
Me : ISynchroExInternal;
begin
Me := self as ISynchroExInternal;
FUnionLock.Enter;
try
Proc;
for Obs in FObservers do
Obs.Value.PossibleStateChange( Me, Obs.Key)
finally
FUnionLock.Leave
end
end;
function TModularSynchro.PermitsDedicatedSoleIndirectClient: boolean;
begin
result := (FIndirectWaiters.Read = 0) and (FDirectWaiters.Read = 0)
end;
function TModularSynchro.PermitsDirectClients: boolean;
{$IFDEF MSWINDOWS}
var
Obj: TObject;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
result := (FIndirectWaiters = 0) and (not FDedicatedToOneIndirectWaiter);
if result then
begin
Obj := FBase.AsMWObject;
result := assigned( Obj) and (Obj is THandleObject)
end
{$ELSE}
result := False
{$ENDIF}
end;
function TModularSynchro.PermitsIndirectClients: boolean;
begin
result := FDirectWaiters.Read = 0
end;
procedure TModularSynchro.RegisterDedicatedSoleIndirectClient( Delta: integer);
begin
if Delta > 0 then