-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCrossSyncObjs.pas
1428 lines (1195 loc) · 51.6 KB
/
CrossSyncObjs.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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
CrossSyncObjs
Provides wrapper classes for synchronization primitives and wait functions
from libraries WinSynObjs and LinSyncObjs.
Classes in WinSynObjs and LinSyncObjs are sticking to a nomenclature used
by system synchronization objects they are based upon, as a result the
corresponding synchronizers in both libraries have differently named
methods for the same functionality.
Wrappers in this library are hiding these differences behind an unified
interface. Note that only functionality common for both wrapped libraries
is provided - specialities (eg. releasing a semaphore by more than 1,
alertable waiting, ...) are not implemented or default value is used.
WARNING - unless noted otherwise, do not assume any features the
synchronizers might provide (eg. recursive locking, lock
promotion, ...), as they are usually only available in some
systems, and therefore cannot be relied upon. Expect only
bare-bone functionality.
WARNING - remembed that all system-specific limitations still apply here
(eg. max 64 events in WaitForMultipleEvents on Windows).
Version 1.1.1 (2024-05-15)
Last change 2024-09-09
©2022-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.CrossSyncObjs
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* LinSyncObjs - github.com/TheLazyTomcat/Lib.LinSyncObjs
* WinSyncObjs - github.com/TheLazyTomcat/Lib.WinSyncObjs
Library LinSyncObjs is required only when compiling for Linux OS.
Library WinSyncObjs is required only when compiling for Windows OS.
Indirect dependencies:
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
BitOps - github.com/TheLazyTomcat/Lib.BitOps
BitVector - github.com/TheLazyTomcat/Lib.BitVector
HashBase - github.com/TheLazyTomcat/Lib.HashBase
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
NamedSharedItems - github.com/TheLazyTomcat/Lib.NamedSharedItems
SHA1 - github.com/TheLazyTomcat/Lib.SHA1
SharedMemoryStream - github.com/TheLazyTomcat/Lib.SharedMemoryStream
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
SimpleFutex - github.com/TheLazyTomcat/Lib.SimpleFutex
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit CrossSyncObjs;
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$MODESWITCH ClassicProcVars+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes, SyncObjs,
AuxTypes, AuxClasses, {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF};
type
TCSOWaitResult = (wrSignaled,wrTimeout,wrError);
const
INFINITE = UInt32(-1); // infinite timeout
{===============================================================================
--------------------------------------------------------------------------------
TRTLSyncObject
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TRTLSyncObject - class declaration
===============================================================================}
type
TRTLSyncObject = class(TCustomRefCountedObject);
{===============================================================================
--------------------------------------------------------------------------------
TRTLCriticalSection
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TRTLCriticalSection - class declaration
===============================================================================}
type
TRTLCriticalSection = class(TRTLSyncObject)
protected
fSync: SyncObjs.TCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure Lock; virtual;
procedure Unlock; virtual;
end;
TCriticalSectionRTL = TRTLCriticalSection;
{===============================================================================
--------------------------------------------------------------------------------
TRTLMultiReadExclusiveWriteSynchronizer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TRTLMultiReadExclusiveWriteSynchronizer - class declaration
===============================================================================}
type
TRTLMultiReadExclusiveWriteSynchronizer = class(TRTLSyncObject)
protected
fSync: SysUtils.TMultiReadExclusiveWriteSynchronizer;
public
constructor Create;
destructor Destroy; override;
procedure ReadLock; virtual;
procedure ReadUnlock; virtual;
procedure WriteLock; virtual;
procedure WriteUnlock; virtual;
end;
TMultiReadExclusiveWriteSynchronizerRTL = TRTLMultiReadExclusiveWriteSynchronizer;
TRTLMREW = TRTLMultiReadExclusiveWriteSynchronizer;
TMREWRTL = TRTLMREW;
{===============================================================================
--------------------------------------------------------------------------------
TLocalSyncObject
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TLocalSyncObject - class declaration
===============================================================================}
type
TLocalSyncObject = class(TCustomRefCountedObject);
{===============================================================================
--------------------------------------------------------------------------------
TCriticalSection
--------------------------------------------------------------------------------
===============================================================================}
{
Always allows recursive locking.
}
{===============================================================================
TCriticalSection - class declaration
===============================================================================}
type
TCriticalSection = class(TLocalSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure Lock; virtual;
Function TryLock: Boolean; virtual;
procedure Unlock; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TAdvSyncObject
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TAdvSyncObject - class declaration
===============================================================================}
type
TAdvSyncObject = class(TCustomObject)
protected
Function GetName: String; virtual; abstract;
public
property Name: String read GetName;
end;
{===============================================================================
--------------------------------------------------------------------------------
TEvent
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TEvent - class declaration
===============================================================================}
type
TEvent = class(TAdvSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TEvent;
Function GetName: String; override;
public
constructor Create(const Name: String; ManualReset, InitialState: Boolean); overload;
constructor Create(const Name: String); overload;
constructor Create(ManualReset, InitialState: Boolean); overload;
constructor Create; overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
constructor DuplicateFrom(Source: TEvent);
destructor Destroy; override;
procedure Lock; virtual;
procedure Unlock; virtual;
Function Wait(Timeout: UInt32 = INFINITE): TCSOWaitResult; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TMutex
--------------------------------------------------------------------------------
===============================================================================}
{
Allows for recursive locking.
}
{===============================================================================
TMutex - class declaration
===============================================================================}
type
TMutex = class(TAdvSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TMutex;
Function GetName: String; override;
public
constructor Create(const Name: String); overload;
constructor Create; overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
constructor DuplicateFrom(Source: TMutex);
destructor Destroy; override;
Function Lock(Timeout: UInt32 = INFINITE): TCSOWaitResult; virtual;
procedure Unlock; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSemaphore
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSemaphore - class declaration
===============================================================================}
type
TSemaphore = class(TAdvSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TSemaphore;
Function GetName: String; override;
public
constructor Create(const Name: String; InitialCount: Integer); overload;
constructor Create(InitialCount: Integer); overload;
constructor Open(const Name: String);
constructor DuplicateFrom(Source: TSemaphore);
destructor Destroy; override;
Function Lock(Timeout: UInt32 = INFINITE): TCSOWaitResult; virtual;
procedure Unlock; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TReadWriteLock
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TReadWriteLock - class declaration
===============================================================================}
type
TReadWriteLock = class(TAdvSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TReadWriteLock;
Function GetName: String; override;
public
constructor Create(const Name: String); overload;
constructor Create; overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
constructor DuplicateFrom(Source: TReadWriteLock);
destructor Destroy; override;
Function ReadLock(Timeout: UInt32 = INFINITE): TCSOWaitResult; virtual;
procedure ReadUnlock; virtual;
Function WriteLock(Timeout: UInt32 = INFINITE): TCSOWaitResult; virtual;
procedure WriteUnlock; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TConditionVariable
--------------------------------------------------------------------------------
===============================================================================}
type
TConditionVariableClass = class of TConditionVariable; // original class from WinSyncObjs or LinSyncObjs
type
// types for autocycle
TCSOWakeOption = (woWakeOne,woWakeAll,woWakeBeforeUnlock);
TCSOWakeOptions = set of TCSOWakeOption;
TCSOPredicateCheckEvent = procedure(Sender: TObject; var Predicate: Boolean) of object;
TCSOPredicateCheckCallback = procedure(Sender: TObject; var Predicate: Boolean);
TCSODataAccessEvent = procedure(Sender: TObject; var WakeOptions: TCSOWakeOptions) of object;
TCSODataAccessCallback = procedure(Sender: TObject; var WakeOptions: TCSOWakeOptions);
{===============================================================================
TConditionVariable - class declaration
===============================================================================}
type
TConditionVariable = class(TAdvSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TConditionVariable;
// autocycle events
fOnPredicateCheckEvent: TCSOPredicateCheckEvent;
fOnPredicateCheckCallback: TCSOPredicateCheckCallback;
fOnDataAccessEvent: TCSODataAccessEvent;
fOnDataAccessCallback: TCSODataAccessCallback;
// getters, setters
Function GetName: String; override;
// autocycle handlers
procedure PredicateCheckHandler(Sender: TObject; var Predicate: Boolean); virtual;
procedure DataAccessHandler(Sender: TObject; var WakeOptions: {$IFDEF Windows}TWSOWakeOptions{$ELSE}TLSOWakeOptions{$ENDIF}); virtual;
// othes
class Function GetActualSyncClass: TConditionVariableClass; virtual;
public
constructor Create(const Name: String); overload;
constructor Create; overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
constructor DuplicateFrom(Source: TConditionVariable);
destructor Destroy; override;
procedure Wait(DataLock: TMutex; Timeout: UInt32 = INFINITE); overload; virtual;
procedure Wake; virtual;
procedure WakeAll; virtual;
procedure AutoCycle(DataLock: TMutex; Timeout: DWORD = INFINITE); overload; virtual;
// events
property OnPredicateCheckEvent: TCSOPredicateCheckEvent read fOnPredicateCheckEvent write fOnPredicateCheckEvent;
property OnPredicateCheckCallback: TCSOPredicateCheckCallback read fOnPredicateCheckCallback write fOnPredicateCheckCallback;
property OnPredicateCheck: TCSOPredicateCheckEvent read fOnPredicateCheckEvent write fOnPredicateCheckEvent;
property OnDataAccessEvent: TCSODataAccessEvent read fOnDataAccessEvent write fOnDataAccessEvent;
property OnDataAccessCallback: TCSODataAccessCallback read fOnDataAccessCallback write fOnDataAccessCallback;
property OnDataAccess: TCSODataAccessEvent read fOnDataAccessEvent write fOnDataAccessEvent;
end;
{===============================================================================
--------------------------------------------------------------------------------
TConditionVariableEx
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TConditionVariableEx - class declaration
===============================================================================}
type
TConditionVariableEx = class(TConditionVariable)
protected
class Function GetActualSyncClass: TConditionVariableClass; override;
public
procedure Lock; virtual;
procedure Unlock; virtual;
procedure Wait(Timeout: UInt32 = INFINITE); overload; virtual;
procedure AutoCycle(Timeout: DWORD = INFINITE); overload; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBarrier
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TBarrier - class declaration
===============================================================================}
type
TBarrier = class(TAdvSyncObject)
protected
fSync: {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TBarrier;
Function GetName: String; override;
public
constructor Create(const Name: String; Count: Integer); overload;
constructor Create(Count: Integer); overload;
constructor Open(const Name: String);
constructor DuplicateFrom(Source: TBarrier);
destructor Destroy; override;
procedure Wait; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
Wait functions
--------------------------------------------------------------------------------
===============================================================================}
Function WaitForMultipleEvents(Objects: array of TEvent; WaitAll: Boolean; Timeout: DWORD; out Index: Integer): TCSOWaitResult; overload;
Function WaitForMultipleEvents(Objects: array of TEvent; WaitAll: Boolean; Timeout: DWORD): TCSOWaitResult; overload;
Function WaitForMultipleEvents(Objects: array of TEvent; WaitAll: Boolean): TCSOWaitResult; overload;
{===============================================================================
--------------------------------------------------------------------------------
Utility functions
--------------------------------------------------------------------------------
===============================================================================}
{
WaitResultToStr returns textual representation of a given wait result.
Meant mainly for debugging.
}
Function WaitResultToStr(WaitResult: TCSOWaitResult): String;
implementation
{$IFNDEF Windows}
uses
UnixType;
{$ENDIF}
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
Internal functions
--------------------------------------------------------------------------------
===============================================================================}
Function TranslateWaitResult(WaitResult: {$IFDEF Windows}TWSOWaitResult{$ELSE}TLSOWaitResult{$ENDIF}): TCSOWaitResult;
begin
case WaitResult of
{$IFDEF Windows}
WinSyncObjs.wrSignaled,
WinSyncObjs.wrAbandoned: Result := wrSignaled;
WinSyncObjs.wrTimeout: Result := wrTimeout;
{$ELSE}
LinSyncObjs.wrSignaled,
LinSyncObjs.wrAbandoned: Result := wrSignaled;
LinSyncObjs.wrTimeout: Result := wrTimeout;
{$ENDIF}
else
{wrIOCompletion, wrMessage, wrError, wrFatal}
Result := wrError;
end;
end;
//------------------------------------------------------------------------------
Function TranslateWakeOptions(WakeOptions: {$IFDEF Windows}TWSOWakeOptions{$ELSE}TLSOWakeOptions{$ENDIF}): TCSOWakeOptions; overload;
begin
Result := [];
If {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.woWakeOne in WakeOptions then
Include(Result,woWakeOne);
If {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.woWakeAll in WakeOptions then
Include(Result,woWakeAll);
If {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.woWakeBeforeUnlock in WakeOptions then
Include(Result,woWakeBeforeUnlock);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TranslateWakeOptions(WakeOptions: TCSOWakeOptions): {$IFDEF Windows}TWSOWakeOptions{$ELSE}TLSOWakeOptions{$ENDIF}; overload;
begin
Result := [];
If woWakeOne in WakeOptions then
Include(Result,{$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.woWakeOne);
If woWakeAll in WakeOptions then
Include(Result,{$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.woWakeAll);
If woWakeBeforeUnlock in WakeOptions then
Include(Result,{$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.woWakeBeforeUnlock);
end;
{===============================================================================
--------------------------------------------------------------------------------
TRTLCriticalSection
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TRTLCriticalSection - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TRTLCriticalSection - public methods
-------------------------------------------------------------------------------}
constructor TRTLCriticalSection.Create;
begin
inherited Create;
fSync := SyncObjs.TCriticalSection.Create;
end;
//------------------------------------------------------------------------------
destructor TRTLCriticalSection.Destroy;
begin
fSync.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TRTLCriticalSection.Lock;
begin
fSync.Enter;
end;
//------------------------------------------------------------------------------
procedure TRTLCriticalSection.Unlock;
begin
fSync.Leave;
end;
{===============================================================================
--------------------------------------------------------------------------------
TRTLMultiReadExclusiveWriteSynchronizer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TRTLMultiReadExclusiveWriteSynchronizer - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TRTLMultiReadExclusiveWriteSynchronizer - public methods
-------------------------------------------------------------------------------}
constructor TRTLMultiReadExclusiveWriteSynchronizer.Create;
begin
inherited Create;
fSync := SysUtils.TMultiReadExclusiveWriteSynchronizer.Create;
end;
//------------------------------------------------------------------------------
destructor TRTLMultiReadExclusiveWriteSynchronizer.Destroy;
begin
fSync.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TRTLMultiReadExclusiveWriteSynchronizer.ReadLock;
begin
fSync.BeginRead;
end;
//------------------------------------------------------------------------------
procedure TRTLMultiReadExclusiveWriteSynchronizer.ReadUnlock;
begin
fSync.EndRead;
end;
//------------------------------------------------------------------------------
procedure TRTLMultiReadExclusiveWriteSynchronizer.WriteLock;
begin
fSync.BeginWrite;
end;
//------------------------------------------------------------------------------
procedure TRTLMultiReadExclusiveWriteSynchronizer.WriteUnlock;
begin
fSync.EndWrite
end;
{===============================================================================
--------------------------------------------------------------------------------
TCriticalSection
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCriticalSection - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TCriticalSection - public methods
-------------------------------------------------------------------------------}
constructor TCriticalSection.Create;
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TCriticalSection.Create;
end;
//------------------------------------------------------------------------------
destructor TCriticalSection.Destroy;
begin
fSync.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TCriticalSection.Lock;
begin
fSync.Enter;
end;
//------------------------------------------------------------------------------
Function TCriticalSection.TryLock: Boolean;
begin
Result := fSync.TryEnter;
end;
//------------------------------------------------------------------------------
procedure TCriticalSection.Unlock;
begin
fSync.Leave;
end;
{===============================================================================
--------------------------------------------------------------------------------
TEvent
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TEvent - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TEvent - protected methods
-------------------------------------------------------------------------------}
Function TEvent.GetName: String;
begin
Result := fSync.Name;
end;
{-------------------------------------------------------------------------------
TEvent - public methods
-------------------------------------------------------------------------------}
constructor TEvent.Create(const Name: String; ManualReset, InitialState: Boolean);
begin
inherited Create;
{$IFDEF Windows}
fSync := WinSyncObjs.TEvent.Create(ManualReset,InitialState,Name);
{$ELSE}
fSync := LinSyncObjs.TEvent.Create(Name,ManualReset,InitialState);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TEvent.Create(const Name: String);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TEvent.Create(Name);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TEvent.Create(ManualReset, InitialState: Boolean);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TEvent.Create(ManualReset,InitialState);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TEvent.Create;
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TEvent.Create;
end;
//------------------------------------------------------------------------------
constructor TEvent.Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TEvent.Open(Name);
end;
//------------------------------------------------------------------------------
constructor TEvent.DuplicateFrom(Source: TEvent);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TEvent.DuplicateFrom(Source.fSync);
end;
//------------------------------------------------------------------------------
destructor TEvent.Destroy;
begin
fSync.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TEvent.Lock;
begin
{$IFDEF Windows}
fSync.ResetEventStrict;
{$ELSE}
fSync.LockStrict;
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TEvent.Unlock;
begin
{$IFDEF Windows}
fSync.SetEventStrict;
{$ELSE}
fSync.UnlockStrict;
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function TEvent.Wait(Timeout: UInt32 = INFINITE): TCSOWaitResult;
begin
{$IFDEF Windows}
Result := TranslateWaitResult(fSync.WaitFor(Timeout));
{$ELSE}
case Timeout of
0: If fSync.TryWaitStrict then
Result := wrSignaled
else
Result := wrTimeout;
INFINITE: begin
fSync.WaitStrict;
Result := wrSignaled;
end;
else
Result := TranslateWaitResult(fSync.TimedWait(Timeout));
end;
{$ENDIF}
end;
{===============================================================================
--------------------------------------------------------------------------------
TMutex
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TMutex - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TMutex - protected methods
-------------------------------------------------------------------------------}
Function TMutex.GetName: String;
begin
Result := fSync.Name;
end;
{-------------------------------------------------------------------------------
TMutex - public methods
-------------------------------------------------------------------------------}
constructor TMutex.Create(const Name: String);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TMutex.Create(Name);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TMutex.Create;
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TMutex.Create;
end;
//------------------------------------------------------------------------------
constructor TMutex.Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TMutex.Open(Name);
end;
//------------------------------------------------------------------------------
constructor TMutex.DuplicateFrom(Source: TMutex);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TMutex.DuplicateFrom(Source.fSync);
end;
//------------------------------------------------------------------------------
destructor TMutex.Destroy;
begin
fSync.Free;
inherited;
end;
//------------------------------------------------------------------------------
Function TMutex.Lock(Timeout: UInt32 = INFINITE): TCSOWaitResult;
begin
{$IFDEF Windows}
Result := TranslateWaitResult(fSync.WaitFor(Timeout));
{$ELSE}
case Timeout of
0: If fSync.TryLockStrict then
Result := wrSignaled
else
Result := wrTimeout;
INFINITE: begin
fSync.LockStrict;
Result := wrSignaled;
end;
else
Result := TranslateWaitResult(fSync.TimedLock(Timeout));
end;
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TMutex.Unlock;
begin
{$IFDEF Windows}
fSync.ReleaseMutexStrict;
{$ELSE}
fSync.UnlockStrict;
{$ENDIF}
end;
{===============================================================================
--------------------------------------------------------------------------------
TSemaphore
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSemaphore - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSemaphore - protected methods
-------------------------------------------------------------------------------}
Function TSemaphore.GetName: String;
begin
Result := fSync.Name;
end;
{-------------------------------------------------------------------------------
TSemaphore - public methods
-------------------------------------------------------------------------------}
constructor TSemaphore.Create(const Name: String; InitialCount: Integer);
begin
inherited Create;
{$IFDEF Windows}
fSync := WinSyncObjs.TSemaphore.Create(InitialCount,Integer($7FFFFFFF),Name);
{$ELSE}
fSync := LinSyncObjs.TSemaphore.Create(Name,cUnsigned(InitialCount));
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TSemaphore.Create(InitialCount: Integer);
begin
inherited Create;
{$IFDEF Windows}
fSync := WinSyncObjs.TSemaphore.Create(InitialCount,Integer($7FFFFFFF));
{$ELSE}
fSync := LinSyncObjs.TSemaphore.Create(cUnsigned(InitialCount));
{$ENDIF}
end;
//------------------------------------------------------------------------------
constructor TSemaphore.Open(const Name: String);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TSemaphore.Open(Name);
end;
//------------------------------------------------------------------------------
constructor TSemaphore.DuplicateFrom(Source: TSemaphore);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TSemaphore.DuplicateFrom(Source.fSync);
end;
//------------------------------------------------------------------------------
destructor TSemaphore.Destroy;
begin
fSync.Free;
inherited;
end;
//------------------------------------------------------------------------------
Function TSemaphore.Lock(Timeout: UInt32 = INFINITE): TCSOWaitResult;
begin
{$IFDEF Windows}
Result := TranslateWaitResult(fSync.WaitFor(Timeout));
{$ELSE}
case Timeout of
0: If fSync.TryWaitStrict then
Result := wrSignaled
else
Result := wrTimeout;
INFINITE: begin
fSync.WaitStrict;
Result := wrSignaled;
end;
else
Result := TranslateWaitResult(fSync.TimedWait(Timeout));
end;
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TSemaphore.Unlock;
begin
{$IFDEF Windows}
fSync.ReleaseSemaphoreStrict;
{$ELSE}
fSync.PostStrict;
{$ENDIF}
end;
{===============================================================================
--------------------------------------------------------------------------------
TReadWriteLock
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TReadWriteLock - class declaration
===============================================================================}
{-------------------------------------------------------------------------------
TReadWriteLock - protected methods
-------------------------------------------------------------------------------}
Function TReadWriteLock.GetName: String;
begin
Result := fSync.Name;
end;
{-------------------------------------------------------------------------------
TReadWriteLock - public methods
-------------------------------------------------------------------------------}
constructor TReadWriteLock.Create(const Name: String);
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TReadWriteLock.Create(Name);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TReadWriteLock.Create;
begin
inherited Create;
fSync := {$IFDEF Windows}WinSyncObjs{$ELSE}LinSyncObjs{$ENDIF}.TReadWriteLock.Create;
end;