-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SyncThread.pas
418 lines (339 loc) · 15.1 KB
/
SyncThread.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
{-------------------------------------------------------------------------------
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/.
-------------------------------------------------------------------------------}
{===============================================================================
SyncThread
Provides a mean of synchronizing methods between any two cooperating
threads. Synchronization here means running a method of synchronizing
thread within a context of different (target) thread, and waiting for it
to finish.
To synchronize between any threads, use class TSyncThreadSynchronizer.
Both threads must have an instance of this class.
The synchronizing thread will use method Synchronize, giving a method to
be synchronized and a target (synchronizer running within a thread you
are synchronizing with).
The thread you are sychnronizing with must repeatedly call method
DoSynchronization of its synchronizer, as the synchronization itself is
done inside of this method.
To simplify, you can use provided class TSyncThread (a descendant of
TThread). It provides the same methods a synchronizer (which is internally
used anyway), so the process is the same (remember to call DoSynchronization
in a thread you are synchronizing with).
WARNING - methods DoSynchronization are synchronous, meaning they will
not return until a synchronization occurs or a timeout elapses.
Note that you cannot call method Synchronize from within a method that is
currently being synchronized. This is to prevent deadlocks.
Version 2.0.1 (2024-05-03)
Last change 2024-09-09
©2018-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.SyncThread
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
Messanger - github.com/TheLazyTomcat/Lib.Messanger
Library AuxExceptions is required only when rebasing local exception classes
(see symbol SyncThread_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
BitOps - github.com/TheLazyTomcat/Lib.BitOps
BitVector - github.com/TheLazyTomcat/Lib.BitVector
CrossSyncObjs - github.com/TheLazyTomcat/Lib.CrossSyncObjs
HashBase - github.com/TheLazyTomcat/Lib.HashBase
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
LinSyncObjs - github.com/TheLazyTomcat/Lib.LinSyncObjs
ListSorters - github.com/TheLazyTomcat/Lib.ListSorters
MemVector - github.com/TheLazyTomcat/Lib.MemVector
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
WinSyncObjs - github.com/TheLazyTomcat/Lib.WinSyncObjs
===============================================================================}
unit SyncThread;
{
SyncThread_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
SyncThread_UseAuxExceptions to achieve this.
}
{$IF Defined(SyncThread_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH CLASSICPROCVARS+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes,
AuxTypes, AuxClasses, Messanger{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exception
===============================================================================}
type
ESTException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESTNoGlobalManager = class(ESTException);
ESTCannotSynchronize = class(ESTException);
{===============================================================================
--------------------------------------------------------------------------------
TSyncThreadSynchronizer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSyncThreadSynchronizer - class declaration
===============================================================================}
type
TSyncThreadSynchronizer = class(TCustomObject)
protected
fEndpoint: TMessangerEndpoint;
fSynchronizing: Boolean;
procedure MessageHandler(Sender: TObject; Msg: TMsgrMessageIn; var Flags: TMsgrDispatchFlags); virtual;
procedure Initialize; virtual;
procedure Finalize; virtual;
public
constructor Create;
destructor Destroy; override;
procedure Synchronize(Method: TThreadMethod; Target: TSyncThreadSynchronizer); virtual;
procedure DoSynchronization(Timeout: UInt32 = 0); virtual;
property Synchronizing: Boolean read fSynchronizing;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSyncThread
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSyncThread - class declaration
===============================================================================}
type
TSyncThread = class(TThread)
protected
fSynchronizer: TSyncThreadSynchronizer;
Function GetSynchronizing: Boolean; virtual;
procedure Synchronize(Method: TThreadMethod; Target: TSyncThreadSynchronizer); overload; virtual;
procedure Synchronize(Method: TThreadMethod; Target: TSyncThread); overload; virtual;
procedure DoSynchronization(Timeout: UInt32 = 0); virtual;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property Synchronizing: Boolean read GetSynchronizing;
end;
implementation
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
Internal constants
===============================================================================}
const
ST_SYNC_MESSAGE_ID = $51235707; // just a random number
{===============================================================================
Global messanger management
===============================================================================}
var
GlobalMessanger: TMessanger = nil;
//------------------------------------------------------------------------------
Function CreateEndpoint: TMessangerEndpoint;
begin
If Assigned(GlobalMessanger) then
Result := GlobalMessanger.CreateEndpoint
else
raise ESTNoGlobalManager.Create('CreateEndpoint: There is no global SyncThread manager.');
end;
//------------------------------------------------------------------------------
procedure DestroyEndpoint(Endpoint: TMessangerEndpoint);
begin
If Assigned(GlobalMessanger) then
Endpoint.Free
else
raise ESTNoGlobalManager.Create('DestroyEndpoint: There is no global SyncThread manager.');
end;
//------------------------------------------------------------------------------
procedure UnitInitialization;
begin
GlobalMessanger := TMessanger.Create(1024);
end;
//------------------------------------------------------------------------------
procedure UnitFinalization;
begin
try
{
Following can fail. If it does, leave it hanging - the unit is being
finalized anyway, meaning the program is ending and potential memory leak
should not pose any problem.
}
GlobalMessanger.Free;
except
// eat the possible exception
on E: EMsgrInvalidState do
// nothing
else
raise; // re-raise other excetions
end;
GlobalMessanger := nil;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSyncThreadSynchronizer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSyncThreadSynchronizer - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSyncThreadSynchronizer - protected methods
-------------------------------------------------------------------------------}
{$IFDEF FPCDWM}{$PUSH}W4055 W5024{$ENDIF}
procedure TSyncThreadSynchronizer.MessageHandler(Sender: TObject; Msg: TMsgrMessageIn; var Flags: TMsgrDispatchFlags);
var
Method: TMethod;
begin
If (Msg.Parameter1 = ST_SYNC_MESSAGE_ID) and (mdfSentMessage in Flags) then
begin
// get code and data of the synchronized method from message params
Method.Code := Pointer(Msg.Parameter2);
Method.Data := Pointer(Msg.Parameter3);
// execute the method in current context
try
TThreadMethod(Method);
except
PPointer(Msg.Parameter4)^ := AcquireExceptionObject;
end;
end;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TSyncThreadSynchronizer.Initialize;
begin
fEndpoint := CreateEndpoint;
fEndpoint.OnMessage := MessageHandler;
fSynchronizing := False;
end;
//------------------------------------------------------------------------------
procedure TSyncThreadSynchronizer.Finalize;
begin
fEndpoint.Free;
end;
{-------------------------------------------------------------------------------
TSyncThreadSynchronizer - public methods
-------------------------------------------------------------------------------}
constructor TSyncThreadSynchronizer.Create;
begin
inherited Create;
Initialize;
end;
//------------------------------------------------------------------------------
destructor TSyncThreadSynchronizer.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
procedure TSyncThreadSynchronizer.Synchronize(Method: TThreadMethod; Target: TSyncThreadSynchronizer);
var
SyncException: Exception;
begin
If not fSynchronizing then
begin
SyncException := nil;
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
// following call will block until the target is done processing it
fEndpoint.SendMessage(Target.fEndpoint.EndpointID,
ST_SYNC_MESSAGE_ID,
TMSGRParam(TMethod(Method).Code),
TMSGRParam(TMethod(Method).Data),
TMSGRParam(@SyncException));
{$IFDEF FPCDWM}{$POP}{$ENDIF}
// check if exception occurred, and if so, raise it
If Assigned(SyncException) then
raise SyncException;
end
else raise ESTCannotSynchronize.Create('TSyncThreadSynchronizer.Synchronize: Nested synchronization not allowed.');
end;
//------------------------------------------------------------------------------
procedure TSyncThreadSynchronizer.DoSynchronization(Timeout: UInt32 = 0);
begin
fSynchronizing := True;
try
fEndpoint.Cycle(Timeout);
finally
fSynchronizing := False;
end;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSyncThread
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSyncThread - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSyncThread - protected methods
-------------------------------------------------------------------------------}
Function TSyncThread.GetSynchronizing: Boolean;
begin
Result := fSynchronizer.Synchronizing;
end;
//------------------------------------------------------------------------------
procedure TSyncThread.Synchronize(Method: TThreadMethod; Target: TSyncThreadSynchronizer);
begin
fSynchronizer.Synchronize(Method,Target);
end;
//------------------------------------------------------------------------------
procedure TSyncThread.Synchronize(Method: TThreadMethod; Target: TSyncThread);
begin
fSynchronizer.Synchronize(Method,Target.fSynchronizer);
end;
//------------------------------------------------------------------------------
procedure TSyncThread.DoSynchronization(Timeout: UInt32 = 0);
begin
fSynchronizer.DoSynchronization(Timeout);
end;
//------------------------------------------------------------------------------
procedure TSyncThread.AfterConstruction;
begin
inherited;
fSynchronizer := TSyncThreadSynchronizer.Create
end;
//------------------------------------------------------------------------------
procedure TSyncThread.BeforeDestruction;
begin
fSynchronizer.Free;
inherited;
end;
{===============================================================================
--------------------------------------------------------------------------------
Unit initialization and finalization
--------------------------------------------------------------------------------
===============================================================================}
initialization
UnitInitialization;
finalization
UnitFinalization;
end.