-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWinSyncObjs.pas
More file actions
4564 lines (3922 loc) · 177 KB
/
WinSyncObjs.pas
File metadata and controls
4564 lines (3922 loc) · 177 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
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/.
-------------------------------------------------------------------------------}
{===============================================================================
WinSyncObjs
Main aim of this library is to provide classes that are encapsulating
synchronization objects provided by the Windows operating system.
It currently implements classes for critical section, event, mutex,
semaphore and waitable timer.
Secondary function is to provide other synchronization primitives not
available in Windows. At this point, condition variable, barrier and
read-write lock are implemented.
They can all be used for inter-process synchronization. Unfortunately,
none can be used in waiting for multiple objects as they are compound or
(in-here refered as) complex objects.
WARNING - complex synchronization objects are not fully tested and should
be therefore considered experimental.
Another part of this library is encapsulation and extension of functions
allowing waiting for multiple objects.
The extension (functions WaitForManyHandles and WaitForManyObjects) allows
waiting for more than 64 objects, which is otherwise a limit for system-
provided calls.
WARNING - waiting for many objects should only be used for waiting on
objects with immutable state. Refer to description of these
functions for details.
WARNING - waiting on many objects should also be considered an
experimental implementation.
Version 1.2.2 (2025-03-06)
Last change 2026-02-25
©2016-2026 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
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.WinSyncObjs
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
NamedSharedItems - github.com/TheLazyTomcat/Lib.NamedSharedItems
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
Library AuxExceptions is required only when rebasing local exception classes
(see symbol WinSyncObjs_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BitOps - github.com/TheLazyTomcat/Lib.BitOps
HashBase - github.com/TheLazyTomcat/Lib.HashBase
ListUtils - github.com/TheLazyTomcat/Lib.ListUtils
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
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit WinSyncObjs;
{
WinSyncObjs_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
WinSyncObjs_UseAuxExceptions to achieve this.
}
{$IF Defined(WinSyncObjs_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF not(defined(MSWINDOWS) or defined(WINDOWS))}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$MODESWITCH ClassicProcVars+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
{$IF Declared(CompilerVersion)}
{$IF CompilerVersion >= 20} // Delphi 2009+
{$DEFINE DeprecatedCommentDelphi}
{$IFEND}
{$IFEND}
{$IF Defined(FPC) or Defined(DeprecatedCommentDelphi)}
{$DEFINE DeprecatedComment}
{$ELSE}
{$UNDEF DeprecatedComment}
{$IFEND}
interface
uses
Windows, SysUtils,
AuxTypes, AuxClasses, NamedSharedItems
{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
EWSOException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
EWSOTimestampError = class(EWSOException);
EWSOTimeConversionError = class(EWSOException);
EWSOInitializationError = class(EWSOException);
EWSOHandleDuplicationError = class(EWSOException);
EWSOOpenError = class(EWSOException);
EWSOEventError = class(EWSOException);
EWSOMutexError = class(EWSOException);
EWSOSemaphoreError = class(EWSOException);
EWSOTimerError = class(EWSOException);
EWSOInvalidHandle = class(EWSOException);
EWSOInvalidObject = class(EWSOException);
EWSOInvalidValue = class(EWSOException);
EWSOWaitError = class(EWSOException);
EWSOMultiWaitError = class(EWSOException);
EWSOMultiWaitInvalidCount = class(EWSOException);
{===============================================================================
--------------------------------------------------------------------------------
TCriticalSection
--------------------------------------------------------------------------------
===============================================================================}
{
To properly use the TCriticalSection object, create one instance and then
pass this one instance to other threads that need to be synchronized.
Make sure to only free it once.
You can also set the proterty FreeOnRelease to true (by default false) and
then use the build-in reference counting - call method Acquire for each
thread using it (including the one that created it) and method Release every
time a thread will stop using it. When reference count reaches zero in a
call to Release, the object will be automatically freed.
}
{===============================================================================
TCriticalSection - class declaration
===============================================================================}
type
TCriticalSection = class(TCustomRefCountedObject)
protected
fCriticalSectionObj: TRTLCriticalSection;
fSpinCount: DWORD;
Function GetSpinCount: DWORD;
procedure SetSpinCountProc(Value: DWORD); // only redirector to SetSpinCount (setter cannot be a function)
public
constructor Create; overload;
constructor Create(SpinCount: DWORD); overload;
destructor Destroy; override;
Function SetSpinCount(SpinCount: DWORD): DWORD; virtual;
Function TryEnter: Boolean; virtual;
procedure Enter; virtual;
procedure Leave; virtual;
{
If you are setting SpinCount in multiple threads, then the property might
not necessarily contain the correct value set for the underlying system
object.
Set this property only in one thread or use it only for reading the value
that was set in the constructor.
}
property SpinCount: DWORD read GetSpinCount write SetSpinCountProc;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWinSyncObject
--------------------------------------------------------------------------------
===============================================================================}
type
// used intarnally for object identification
TWSOLockType = (
ltEvent,ltMutex,ltSemaphore,ltWaitTimer, // simple locks
ltSmplBarrier,ltBarrier,ltCondVar,ltCondVarEx,ltRWLock); // complex locks
{===============================================================================
TWinSyncObject - class declaration
===============================================================================}
type
TWinSyncObject = class(TCustomObject)
protected
fLastError: DWORD;
fName: String;
Function RectifyAndSetName(const Name: String): Boolean; virtual;
class Function GetLockType: TWSOLockType; virtual; abstract;
public
constructor Create;
{
LastError contains code of the last operating system error that has not
resulted in an exception being raised (eg. error during waiting, release
operations, ...).
}
property LastError: DWORD read fLastError;
property Name: String read fName;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleWinSyncObject
--------------------------------------------------------------------------------
===============================================================================}
{
To properly use simple windows synchronization object (TEvent, TMutex,
TSemaphore, TWaitableTimer), create one progenitor instance and use this one
instance only in a thread where it was created. Note that it IS possible and
permissible to use the same instance in multiple threads, but this practice
is discouraged as none of the fields are protected against concurrent access
(especially problematic for LastError property).
To access the synchronizer in other threads of the same process, create a new
instance using DuplicateFrom constructor, passing the progenitor instance or
any duplicate instance based on it.
You can also use Open constructors where implemented and when the object is
created with a name.
To access the synchronizer in different process, it is recommended to use
Open constructors (requires named object).
DuplicateFromProcess constructors or DuplicateForProcess methods along with
CreateFrom constructor can also be used, but this requires some kind of IPC
to transfer the handles between processes.
}
{
wrFatal is only used internally, and should not be returned by any public
funtion. If it still is, you should treat it as library error.
}
type
TWSOWaitResult = (wrSignaled, wrAbandoned, wrIOCompletion, wrMessage, wrTimeout, wrError, wrFatal);
{===============================================================================
TSimpleWinSyncObject - class declaration
===============================================================================}
type
TSimpleWinSyncObject = class(TWinSyncObject)
protected
fHandle: THandle;
Function RectifyAndSetName(const Name: String): Boolean; override;
procedure CheckAndSetHandle(Handle: THandle); virtual;
procedure DuplicateAndSetHandle(SourceProcess: THandle; SourceHandle: THandle); virtual;
public
constructor CreateFrom(Handle: THandle{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF});
constructor DuplicateFrom(SourceHandle: THandle); overload;
constructor DuplicateFrom(SourceObject: TSimpleWinSyncObject); overload;
{
When passing handle from 32bit process to 64bit process, it is safe to
zero or sign-extend it. In the opposite direction, it is safe to truncate
it.
}
constructor DuplicateFromProcess(SourceProcess: THandle; SourceHandle: THandle);
constructor DuplicateFromProcessID(SourceProcessID: DWORD; SourceHandle: THandle);
destructor Destroy; override;
Function DuplicateForProcess(TargetProcess: THandle): THandle; virtual;
Function DuplicateForProcessID(TargetProcessID: DWORD): THandle; virtual;
{
WARNING - the first overload of method WaitFor intentionaly does not set
LastError property as the error code is returned in parameter
ErrCode.
}
Function WaitFor(Timeout: DWORD; out ErrCode: DWORD; Alertable: Boolean = False): TWSOWaitResult; overload; virtual;
Function WaitFor(Timeout: DWORD = INFINITE; Alertable: Boolean = False): TWSOWaitResult; overload; virtual;
property Handle: THandle read fHandle;
end;
{===============================================================================
--------------------------------------------------------------------------------
TEvent
--------------------------------------------------------------------------------
===============================================================================}
const
// constants for DesiredAccess parameter of Open constructor
EVENT_MODIFY_STATE = 2;
EVENT_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or 3;
{===============================================================================
TEvent - class declaration
===============================================================================}
type
TEvent = class(TSimpleWinSyncObject)
protected
class Function GetLockType: TWSOLockType; override;
public
constructor Create(SecurityAttributes: PSecurityAttributes; ManualReset, InitialState: Boolean; const Name: String); overload;
constructor Create(ManualReset, InitialState: Boolean; const Name: String); overload;
constructor Create(ManualReset, InitialState: Boolean); overload;
constructor Create(const Name: String); overload; // ManualReset := True, InitialState := False
constructor Create; overload;
constructor Open(DesiredAccess: DWORD; InheritHandle: Boolean; const Name: String); overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF}); overload;
Function WaitForAndReset(Timeout: DWORD = INFINITE; Alertable: Boolean = False): TWSOWaitResult;
procedure SetEventStrict; virtual;
Function SetEvent: Boolean; virtual;
procedure ResetEventStrict; virtual;
Function ResetEvent: Boolean; virtual;
{
Function PulseEvent is unreliable and should not be used. More info here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684914
}
procedure PulseEventStrict; virtual; deprecated {$IFDEF DeprecatedComment}'Unreliable, do not use.'{$ENDIF};
Function PulseEvent: Boolean; virtual; deprecated {$IFDEF DeprecatedComment}'Unreliable, do not use.'{$ENDIF};
end;
{===============================================================================
--------------------------------------------------------------------------------
TMutex
--------------------------------------------------------------------------------
===============================================================================}
const
MUTANT_QUERY_STATE = 1;
MUTANT_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or MUTANT_QUERY_STATE;
MUTEX_MODIFY_STATE = MUTANT_QUERY_STATE;
MUTEX_ALL_ACCESS = MUTANT_ALL_ACCESS;
{===============================================================================
TMutex - class declaration
===============================================================================}
type
TMutex = class(TSimpleWinSyncObject)
protected
class Function GetLockType: TWSOLockType; override;
public
constructor Create(SecurityAttributes: PSecurityAttributes; InitialOwner: Boolean; const Name: String); overload;
constructor Create(InitialOwner: Boolean; const Name: String); overload;
constructor Create(InitialOwner: Boolean); overload;
constructor Create(const Name: String); overload; // InitialOwner := False
constructor Create; overload;
constructor Open(DesiredAccess: DWORD; InheritHandle: Boolean; const Name: String); overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF}); overload;
Function WaitForAndRelease(TimeOut: DWORD = INFINITE; Alertable: Boolean = False): TWSOWaitResult; virtual;
procedure ReleaseMutexStrict; virtual;
Function ReleaseMutex: Boolean; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSemaphore
--------------------------------------------------------------------------------
===============================================================================}
const
SEMAPHORE_MODIFY_STATE = 2;
SEMAPHORE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or 3;
{===============================================================================
TSemaphore - class declaration
===============================================================================}
type
TSemaphore = class(TSimpleWinSyncObject)
protected
class Function GetLockType: TWSOLockType; override;
public
constructor Create(SecurityAttributes: PSecurityAttributes; InitialCount, MaximumCount: Integer; const Name: String); overload;
constructor Create(InitialCount, MaximumCount: Integer; const Name: String); overload;
constructor Create(InitialCount, MaximumCount: Integer); overload;
constructor Create(const Name: String); overload; // InitialCount := 1, MaximumCount := 1
constructor Create; overload;
constructor Open(DesiredAccess: LongWord; InheritHandle: Boolean; const Name: String); overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF}); overload;
Function WaitForAndRelease(TimeOut: LongWord = INFINITE; Alertable: Boolean = False): TWSOWaitResult; virtual;
procedure ReleaseSemaphoreStrict(ReleaseCount: Integer; out PreviousCount: Integer); overload; virtual;
procedure ReleaseSemaphoreStrict; overload; virtual;
Function ReleaseSemaphore(ReleaseCount: Integer; out PreviousCount: Integer): Boolean; overload; virtual;
Function ReleaseSemaphore: Boolean; overload; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWaitableTimer
--------------------------------------------------------------------------------
===============================================================================}
const
TIMER_QUERY_STATE = 1;
TIMER_MODIFY_STATE = 2;
TIMER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or TIMER_QUERY_STATE or TIMER_MODIFY_STATE;
{===============================================================================
TWaitableTimer - class declaration
===============================================================================}
type
TTimerAPCRoutine = procedure(ArgToCompletionRoutine: Pointer; TimerLowValue, TimerHighValue: DWORD); stdcall;
TWaitableTimer = class(TSimpleWinSyncObject)
protected
class Function GetLockType: TWSOLockType; override;
Function DateTimeToFileTime(DateTime: TDateTime): TFileTime; virtual;
public
constructor Create(SecurityAttributes: PSecurityAttributes; ManualReset: Boolean; const Name: String); overload;
constructor Create(ManualReset: Boolean; const Name: String); overload;
constructor Create(ManualReset: Boolean); overload;
constructor Create(const Name: String); overload; // ManualReset := True
constructor Create; overload;
constructor Open(DesiredAccess: DWORD; InheritHandle: Boolean; const Name: String); overload;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF}); overload;
procedure SetWaitableTimerStrict(DueTime: Int64; Period: Integer; CompletionRoutine: TTimerAPCRoutine; ArgToCompletionRoutine: Pointer; Resume: Boolean); overload; virtual;
procedure SetWaitableTimerStrict(DueTime: Int64; Period: Integer = 0); overload; virtual;
procedure SetWaitableTimerStrict(DueTime: TDateTime; Period: Integer; CompletionRoutine: TTimerAPCRoutine; ArgToCompletionRoutine: Pointer; Resume: Boolean); overload; virtual;
procedure SetWaitableTimerStrict(DueTime: TDateTime; Period: Integer = 0); overload; virtual;
Function SetWaitableTimer(DueTime: Int64; Period: Integer; CompletionRoutine: TTimerAPCRoutine; ArgToCompletionRoutine: Pointer; Resume: Boolean): Boolean; overload; virtual;
Function SetWaitableTimer(DueTime: Int64; Period: Integer = 0): Boolean; overload; virtual;
Function SetWaitableTimer(DueTime: TDateTime; Period: Integer; CompletionRoutine: TTimerAPCRoutine; ArgToCompletionRoutine: Pointer; Resume: Boolean): Boolean; overload; virtual;
Function SetWaitableTimer(DueTime: TDateTime; Period: Integer = 0): Boolean; overload; virtual;
procedure CancelWaitableTimerStrict; virtual;
Function CancelWaitableTimer: Boolean; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TComplexWinSyncObject
--------------------------------------------------------------------------------
===============================================================================}
{
Complex synchronization objects can be principally created in two ways - as
thread-shared or as process-shared.
Thread-shared objects are created without a name, or, more precisely, with an
empty name. They can be used only for synchronization between threads within
one process.
Process-shared objects are created with non-empty name and can be used for
synchronization between any threads within a system, including threads in
different processes.
Two instances with the same name, created in any process, will be using the
same synchronization object. Different types of synchronizers can have the
same name and still be considered separate, so there is no risk of naming
conflicts (each type of synchronizer has kind-of its own namespace).
To properly use complex windows synchronization object (TBarrier,
TConditioVariable, TConditioVariableEx, TReadWriteLock), create one
progenitor instance and use this instance only in the thread that created it.
To access the synchronizer in other threads of the same process, create a new
instance using DuplicateFrom constructor, passing the progenitor instance or
any duplicate instance based on it as a source. If the progenitor is
process-shared (ie. named), you can also open it using Create or Open
construtors, specifying the same name as has the progenitor.
To access the synchronizer in different process, the progenitor must be
created as process-shared, that is, it must have a non-empty name. Use Create
or Open constructors, specifying the same name as has the progenitor.
The newly created instances will then be using the same synchronization object
as the progenitor.
NOTE - DuplicateFrom constructor can be used on both thread-shared and
process-shared source object, the newly created instance will have
the same mode as source. For process-shared (named) objects, calling
this constructor is equivalent to caling Open constructor.
}
type
TWSOSharedUserData = packed array[0..31] of Byte;
PWSOSharedUserData = ^TWSOSharedUserData;
TWSOSharedDataLockType = (sltNone,sltSection,sltMutex);
TWSOSharedDataLock = record
case LockType: TWSOSharedDataLockType of
sltSection: (ThreadSharedLock: TCriticalSection);
sltMutex: (ProcessSharedLock: THandle); // mutex
sltNone: ();
end;
// all object shared data must start with this structure
TWSOCommonSharedData = packed record
SharedUserData: TWSOSharedUserData;
RefCount: Int32;
end;
PWSOCommonSharedData = ^TWSOCommonSharedData;
{===============================================================================
TComplexWinSyncObject - class declaration
===============================================================================}
type
TComplexWinSyncObject = class(TWinSyncObject)
protected
fProcessShared: Boolean;
fNamedSharedItem: TNamedSharedItem; // unused in thread-shared mode
fSharedDataLock: TWSOSharedDataLock;
fSharedData: Pointer;
fFullyInitialized: Boolean;
Function GetSharedUserDataPtr: PWSOSharedUserData; virtual;
Function GetSharedUserData: TWSOSharedUserData; virtual;
procedure SetSharedUserData(Value: TWSOSharedUserData); virtual;
Function RectifyAndSetName(const Name: String): Boolean; override;
procedure CheckAndSetHandle(out Destination: THandle; Handle: THandle); virtual;
procedure DuplicateAndSetHandle(out Destination: THandle; Handle: THandle); virtual;
// shared data lock management methods
class Function LocksSharedData: Boolean; virtual;
procedure LockSharedData; virtual;
procedure UnlockSharedData; virtual;
// shared data management methods
{
In the following three methods, the shared data are protected by a global
lock that ensures no new object can open the same shared data or destroy
them. Note that this lock is distinct from shared data lock, which is
NOT in effect here.
InitSharedData and BindSharedData are obligued to do a full rollback when
they fail in their respective operations.
InitSharedData should not assume anything about the shared data.
BindSharedData should raise an exception if the data are in any way
inconsistent.
FinalSharedData must be able to accept partialy initialized or completely
uninitialized shared data. Also, this function is called only once when the
shared data are being completely removed from the system.
NOTE - default implementation of all these methods does nothing.
}
procedure InitSharedData; virtual;
procedure BindSharedData; virtual;
procedure FinalSharedData; virtual;
// locks management methods
procedure DuplicateLocks(SourceObject: TComplexWinSyncObject); virtual; abstract;
procedure CreateLocks; virtual; abstract;
procedure OpenLocks; virtual; abstract;
procedure CloseLocks; virtual; abstract; // must be able to accept state where not all locks were successfuly created
// internal creation
procedure InternalCreate(const Name: String); virtual;
procedure InternalOpen(const Name: String); virtual;
procedure InternalClose; virtual;
// naming
{
ClassNameSuffix must return a string in the form '@xxx_', where xxx must
be unique for a class/locker type.
GetDecoratedName returns the object name with appended ClassNameSuffix
followed by Suffix parameter (this parameter must be exactly three
characters long, not more, not less).
}
class Function GetNameSuffix: String; virtual; abstract;
Function GetDecoratedName(const Suffix: String): String; virtual;
public
constructor Create(const Name: String); overload; virtual;
constructor Create; overload; virtual;
constructor Open(const Name: String{$IFNDEF FPC}; Dummy: Integer = 0{$ENDIF}); virtual;
{
Use DuplicateFrom to create instance that accesses the same synchronization
primitive(s) and shared data as the source object.
If the source object is process-shared, DuplicateFrom is equivalent to Open
constructor.
}
constructor DuplicateFrom(SourceObject: TComplexWinSyncObject); virtual;
destructor Destroy; override;
property ProcessShared: Boolean read fProcessShared;
property SharedUserDataPtr: PWSOSharedUserData read GetSharedUserDataPtr;
property SharedUserData: TWSOSharedUserData read GetSharedUserData write SetSharedUserData;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleBarrier
--------------------------------------------------------------------------------
===============================================================================}
type
TWSOSimpleBarrierSharedData = packed record
SharedUserData: TWSOSharedUserData;
RefCount: Int32;
MaxWaitCount: Int32; // invariant value, set only once
WaitCount: Int32; // interlocked access only
end;
PWSOSimpleBarrierSharedData = ^TWSOSimpleBarrierSharedData;
{===============================================================================
TSimpleBarrier - class declaration
===============================================================================}
type
TSimpleBarrier = class(TComplexWinSyncObject)
protected
fEntryLock: THandle; // semaphore with initial value and max value of count
fReleaseLock: THandle; // manual-reset event, initially locked
fBarrierSharedData: PWSOSimpleBarrierSharedData;
fCount: Integer;
class Function LocksSharedData: Boolean; override;
procedure InitSharedData; override;
procedure BindSharedData; override;
procedure DuplicateLocks(SourceObject: TComplexWinSyncObject); override;
procedure CreateLocks; override;
procedure OpenLocks; override;
procedure CloseLocks; override;
class Function GetLockType: TWSOLockType; override;
class Function GetNameSuffix: String; override;
public
constructor Create(const Name: String); override;
constructor Create; override;
constructor Create(Count: Integer; const Name: String); overload;
constructor Create(Count: Integer); overload;
Function Wait: Boolean; virtual; // returns true if this call released the barrier
property Count: Integer read fCount;
end;
{===============================================================================
--------------------------------------------------------------------------------
TBarrier
--------------------------------------------------------------------------------
===============================================================================}
type
TWSOBarrierSharedData = packed record
SharedUserData: TWSOSharedUserData;
RefCount: Int32;
MaxWaitCount: Int32; // invariant value, set only once
WaitCount: Int32;
Releasing: Boolean;
end;
PWSOBarrierSharedData = ^TWSOBarrierSharedData;
{===============================================================================
TBarrier - class declaration
===============================================================================}
type
TBarrier = class(TComplexWinSyncObject)
protected
fEntryLock: THandle; // manual-reset event, unlocked
fReleaseLock: THandle; // manual-reset event, locked
fBarrierSharedData: PWSOBarrierSharedData;
fCount: Integer;
procedure InitSharedData; override;
procedure BindSharedData; override;
procedure DuplicateLocks(SourceObject: TComplexWinSyncObject); override;
procedure CreateLocks; override;
procedure OpenLocks; override;
procedure CloseLocks; override;
class Function GetLockType: TWSOLockType; override;
class Function GetNameSuffix: String; override;
public
constructor Create(const Name: String); override;
constructor Create; override;
constructor Create(Count: Integer; const Name: String); overload;
constructor Create(Count: Integer); overload;
Function Wait: Boolean; virtual;
{
Releases all waiting threads, irrespective of their count, and sets the
barrier back to a non-signaled (blocking) state.
}
Function Release: Integer; virtual;
property Count: Integer read fCount;
end;
{===============================================================================
--------------------------------------------------------------------------------
TConditionVariable
--------------------------------------------------------------------------------
===============================================================================}
{
WARNING - be wery cautious about objects passed as DataLock parameter to
methods Sleep and AutoCycle. If they have been locked multiple
times before the call (affects mutexes and semaphores), it can
create a deadlock as the lock is released only once within the
method (so it can effectively stay locked indefinitely).
}
type
TWSOCondSharedData = packed record
SharedUserData: TWSOSharedUserData;
RefCount: Int32;
WaitCount: Int32;
WakeCount: Int32;
Broadcasting: Boolean;
end;
PWSOCondSharedData = ^TWSOCondSharedData;
// types for autocycle
TWSOWakeOption = (woWakeOne,woWakeAll,woWakeBeforeUnlock);
TWSOWakeOptions = set of TWSOWakeOption;
TWSOPredicateCheckCallback = procedure(Sender: TObject; var Predicate: Boolean);
TWSOPredicateCheckEvent = procedure(Sender: TObject; var Predicate: Boolean) of object;
TWSODataAccessCallback = procedure(Sender: TObject; var WakeOptions: TWSOWakeOptions);
TWSODataAccessEvent = procedure(Sender: TObject; var WakeOptions: TWSOWakeOptions) of object;
{===============================================================================
TConditionVariable - class declaration
===============================================================================}
type
TConditionVariable = class(TComplexWinSyncObject)
protected
fWaitLock: THandle; // semaphore, init 0, max $7FFFFFFF
fBroadcastDoneLock: THandle; // manual-reset event, locked
fCondSharedData: PWSOCondSharedData;
// autocycle events
fOnPredicateCheckCallback: TWSOPredicateCheckCallback;
fOnPredicateCheckEvent: TWSOPredicateCheckEvent;
fOnDataAccessCallback: TWSODataAccessCallback;
fOnDataAccessEvent: TWSODataAccessEvent;
procedure InitSharedData; override;
procedure BindSharedData; override;
procedure DuplicateLocks(SourceObject: TComplexWinSyncObject); override;
procedure CreateLocks; override;
procedure OpenLocks; override;
procedure CloseLocks; override;
class Function GetLockType: TWSOLockType; override;
class Function GetNameSuffix: String; override;
// autocycle events firing
Function DoOnPredicateCheck: Boolean; virtual;
Function DoOnDataAccess: TWSOWakeOptions; virtual;
// utility methods
procedure SelectWake(WakeOptions: TWSOWakeOptions); virtual;
public
{
In both overloads, DataLock parameter can only be an event, mutex or
semaphore, no other type of synchronizer is supported.
}
Function Sleep(DataLock: THandle; Timeout: DWORD = INFINITE): TWSOWaitResult; overload; virtual;
Function Sleep(DataLock: TSimpleWinSyncObject; Timeout: DWORD = INFINITE): TWSOWaitResult; overload; virtual;
procedure Wake; virtual;
procedure WakeAll; virtual;
{
First overload of AutoCycle method only supports mutex object as DataLock
parameter - it is because the object must be signaled and different objects
have different functions for that, and there is no way of discerning which
object is hidden behind the handle.
...well, there is a way, but it involves function NtQueryObject which
should not be used in application code, so let's avoid it.
Second overload allows for event, mutex and semaphore object to be used
as data synchronizer.
}
procedure AutoCycle(DataLock: THandle; Timeout: DWORD = INFINITE; AcceptAbandonedDataLock: Boolean = True); overload; virtual;
procedure AutoCycle(DataLock: TSimpleWinSyncObject; Timeout: DWORD = INFINITE; AcceptAbandonedDataLock: Boolean = True); overload; virtual;
// events
property OnPredicateCheckCallback: TWSOPredicateCheckCallback read fOnPredicateCheckCallback write fOnPredicateCheckCallback;
property OnPredicateCheckEvent: TWSOPredicateCheckEvent read fOnPredicateCheckEvent write fOnPredicateCheckEvent;
property OnPredicateCheck: TWSOPredicateCheckEvent read fOnPredicateCheckEvent write fOnPredicateCheckEvent;
property OnDataAccessCallback: TWSODataAccessCallback read fOnDataAccessCallback write fOnDataAccessCallback;
property OnDataAccessEvent: TWSODataAccessEvent read fOnDataAccessEvent write fOnDataAccessEvent;
property OnDataAccess: TWSODataAccessEvent read fOnDataAccessEvent write fOnDataAccessEvent;
end;
{===============================================================================
--------------------------------------------------------------------------------
TConditionVariableEx
--------------------------------------------------------------------------------
===============================================================================}
{
Only an extension of TConditionVariable with integrated data lock (use
methods Lock and Unlock to manipulate it). New versions of methods Sleep and
AutoCycle without the DataLock parameter are using the integrated data lock
for that purpose.
WARNING - as in the case of TConditionVariable, be wary of how many times
you lock the integrated data lock. A mutex is used internally,
so mutliple locks can result in a deadlock in sleep method.
}
{===============================================================================
TConditionVariableEx - class declaration
===============================================================================}
type
TConditionVariableEx = class(TConditionVariable)
protected
fDataLock: THandle; // mutex, not owned
procedure DuplicateLocks(SourceObject: TComplexWinSyncObject); override;
procedure CreateLocks; override;
procedure OpenLocks; override;
procedure CloseLocks; override;
class Function GetLockType: TWSOLockType; override;
class Function GetNameSuffix: String; override;
public
Function Lock: TWSOWaitResult; virtual;
procedure Unlock; virtual;
Function Sleep(Timeout: DWORD = INFINITE): TWSOWaitResult; overload; virtual;
procedure AutoCycle(Timeout: DWORD = INFINITE; AcceptAbandonedDataLock: Boolean = True); overload; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
TReadWriteLock
--------------------------------------------------------------------------------
===============================================================================}
{
This implementation of read-write lock does not allow for recursive locking
nor read lock promotion - that is, you cannot acquire write lock in the same
thread where you are already holding a read or write lock.
WARNING - trying to acquire write lock in a thread that is currently
holding read or write lock will create a deadlock.
}
type
TWSORWLockSharedData = packed record
SharedUserData: TWSOSharedUserData;
RefCount: Int32;
ReadCount: Int32;
WriteWaitCount: Int32;
Writing: Boolean;
end;
PWSORWLockSharedData = ^TWSORWLockSharedData;
{===============================================================================
TReadWriteLock - class declaration
===============================================================================}
type
TReadWriteLock = class(TComplexWinSyncObject)
protected
fReadLock: THandle; // manual-reset event, unlocked
fWriteQueueLock: THandle; // manual-reset event, unlocked
fWriteLock: THandle; // mutex, not owned
fRWLockSharedData: PWSORWLockSharedData;
procedure InitSharedData; override;
procedure BindSharedData; override;
procedure DuplicateLocks(SourceObject: TComplexWinSyncObject); override;
procedure CreateLocks; override;
procedure OpenLocks; override;
procedure CloseLocks; override;
class Function GetLockType: TWSOLockType; override;
class Function GetNameSuffix: String; override;
public
{
ReadLock can only return wrSignaled, wrTimeout or wrError (LastError will
contain the error code), treat any other returned value as an error (where
LastError does not contain a valid error code).
WARNING - the lock is acquired only when wrSignaled is returned!
}
Function ReadLock(Timeout: DWORD = INFINITE): TWSOWaitResult; virtual;
procedure ReadUnlock; virtual;
{
See ReadLock for returned value and indication of successful lock acquire.
}
Function WriteLock(Timeout: DWORD = INFINITE): TWSOWaitResult; virtual;
procedure WriteUnlock; virtual;
end;
{===============================================================================
--------------------------------------------------------------------------------
Wait functions
--------------------------------------------------------------------------------
===============================================================================}
type
{
Options for message waiting. See MsgWaitForMultipleObjects(Ex) documentation
for details of message waiting.
mwoEnable - enable waiting on messages
mwoInputAvailable - adds MWMO_INPUTAVAILABLE to flags when message waiting
is enabled.
}
TMessageWaitOption = (mwoEnable,mwoInputAvailable);
TMessageWaitOptions = set of TMessageWaitOption;
{
Currently defined values for WakeMask parameter.
}
const
QS_KEY = $0001;
QS_MOUSEMOVE = $0002;
QS_MOUSEBUTTON = $0004;
QS_POSTMESSAGE = $0008;
QS_TIMER = $0010;
QS_PAINT = $0020;
QS_SENDMESSAGE = $0040;
QS_HOTKEY = $0080;
QS_ALLPOSTMESSAGE = $0100;
QS_RAWINPUT = $0400;
QS_TOUCH = $0800;
QS_POINTER = $1000;
QS_MOUSE = QS_MOUSEMOVE or QS_MOUSEBUTTON;
QS_INPUT = QS_MOUSE or QS_KEY or QS_RAWINPUT or QS_TOUCH or QS_POINTER;
QS_ALLEVENTS = QS_INPUT or QS_POSTMESSAGE or QS_TIMER or QS_PAINT or QS_HOTKEY;
QS_ALLINPUT = QS_INPUT or QS_POSTMESSAGE or QS_TIMER or QS_PAINT or QS_HOTKEY or QS_SENDMESSAGE;
{===============================================================================
--------------------------------------------------------------------------------
Wait functions (N <= MAX)
--------------------------------------------------------------------------------
===============================================================================}
{
Waits on multiple handles - the function does not return until wait criteria
are met, an error occurs or the wait times-out (which of these occurred is
indicated by the result).
Handles of the following windows system objects are allowed:
Change notification
Console input
Event
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable timer
Handle array must not be empty and length (Count) must be less than or equal
to 64 (63 when message waiting is enabled), otherwise an exception of type
EWSOMultiWaitInvalidCount will be raised.
If WaitAll is set to true, the function will return wrSignaled only when ALL
objects are signaled. When set to false, it will return wrSignaled when at
least one object becomes signaled.
Timeout is in milliseconds. Default value for Timeout is INFINITE.
When WaitAll is false, Index indicates which object was signaled or abandoned
when wrSignaled or wrAbandoned is returned. In case of wrError, the Index
contains a system error number. For other results, the value of Index is
undefined.
When WaitAll is true, value of Index is undefined except for wrError, where
it again contains system error number.
If Alertable is true, the function will also return if APC has been queued
to the waiting thread. Default value for Alertable is False.
Use set argument MsgWaitOptions to enable and configure message waiting.
Default value is an empty set, meaning message waiting is disabled.
Argument WakeMask is used without change when message waiting is enabled
(it prescribes which messages to observe), otherwise it is ignored. Use
bitwise OR to combine multiple values. Default value is zero.
}
Function WaitForMultipleHandles(Handles: PHandle; Count: Integer; WaitAll: Boolean; Timeout: DWORD; out Index: Integer; Alertable: Boolean; MsgWaitOptions: TMessageWaitOptions; WakeMask: DWORD = QS_ALLINPUT): TWSOWaitResult; overload;
Function WaitForMultipleHandles(Handles: PHandle; Count: Integer; WaitAll: Boolean; Timeout: DWORD; out Index: Integer; Alertable: Boolean = False): TWSOWaitResult; overload;
Function WaitForMultipleHandles(Handles: PHandle; Count: Integer; WaitAll: Boolean; Timeout: DWORD; Alertable: Boolean = False): TWSOWaitResult; overload;
Function WaitForMultipleHandles(Handles: PHandle; Count: Integer; WaitAll: Boolean): TWSOWaitResult; overload;
//------------------------------------------------------------------------------
{
Following functions are behaving the same as the ones accepting pointer to
handle array, see there for details.
}
Function WaitForMultipleHandles(Handles: array of THandle; WaitAll: Boolean; Timeout: DWORD; out Index: Integer; Alertable: Boolean; MsgWaitOptions: TMessageWaitOptions; WakeMask: DWORD = QS_ALLINPUT): TWSOWaitResult; overload;
Function WaitForMultipleHandles(Handles: array of THandle; WaitAll: Boolean; Timeout: DWORD; out Index: Integer; Alertable: Boolean = False): TWSOWaitResult; overload;
Function WaitForMultipleHandles(Handles: array of THandle; WaitAll: Boolean; Timeout: DWORD; Alertable: Boolean = False): TWSOWaitResult; overload;
Function WaitForMultipleHandles(Handles: array of THandle; WaitAll: Boolean): TWSOWaitResult; overload;
//------------------------------------------------------------------------------
{
Functions WaitForMultipleObjects are, again, behaving exactly the same as
WaitForMultipleHandles.
NOTE - LastError property of the passed objects is not set by these
functions. Possible error code is returned in Index output parameter.
}
Function WaitForMultipleObjects(Objects: array of TSimpleWinSyncObject; WaitAll: Boolean; Timeout: DWORD; out Index: Integer; Alertable: Boolean; MsgWaitOptions: TMessageWaitOptions; WakeMask: DWORD = QS_ALLINPUT): TWSOWaitResult; overload;
Function WaitForMultipleObjects(Objects: array of TSimpleWinSyncObject; WaitAll: Boolean; Timeout: DWORD; out Index: Integer; Alertable: Boolean = False): TWSOWaitResult; overload;
Function WaitForMultipleObjects(Objects: array of TSimpleWinSyncObject; WaitAll: Boolean; Timeout: DWORD; Alertable: Boolean = False): TWSOWaitResult; overload;
Function WaitForMultipleObjects(Objects: array of TSimpleWinSyncObject; WaitAll: Boolean): TWSOWaitResult; overload;
{===============================================================================
--------------------------------------------------------------------------------
Wait functions (N > MAX)
--------------------------------------------------------------------------------
===============================================================================}
{-------------------------------------------------------------------------------
>>> WARNING <<<
--------------------------------------------------------------------------------
Waiting on many objects is not completely tested and, given its complexity,
should be considered strictly experimental.
--------------------------------------------------------------------------------
Waiting for many objects - generally, these functions behave the same as
WaitForMultipleObjects and can accept the same object types. But given the
implementation (described further), there are serious limitations that,
unfortunately, cannot be eliminated.
First, it is not possible to use them for mutexes. Mutex is owned by a thread
that finished waiting on it. And since the implementation works with waiter
threads, the ownership is granted to the waiter thread and not the thread that
invoked the waiting. The waiter threads are created and destroyed ad-hoc, so
when the waiting returns, the mutex will already be abandoned.