-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPkg.Threading.SyncObjs.pas
534 lines (443 loc) · 12.3 KB
/
Pkg.Threading.SyncObjs.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
unit Pkg.Threading.SyncObjs;
interface
uses syncObjs, {$IFDEF MSWINDOWS}Windows,{$ENDIF MSWINDOWS} sysUtils,
generics.collections, system.threading;
type
IPkgWaitHandle = interface ['{A11FED0F-204A-4FE6-BED1-5E2D902A7A4F}']
function WaitFor(Timeout: LongWord): TWaitResult;
function ToWaitHandle: IPkgWaitHandle;
end;
IPkgEvent = interface(IPkgWaitHandle) ['{F1CD8006-91EB-4ED7-BB15-423D9C4F3792}']
procedure setEvent;
procedure resetEvent;
end;
IPkgSemaphore = interface(IPkgWaitHandle) ['{0960C375-C6AA-470D-ABA9-EEBE28D0E942}']
procedure Acquire;
procedure Release;
end;
// For accessing TTask's protected members
TPkgTaskHack = class(System.Threading.TTask)
end;
IPkgTaskWaiter = interface(IPkgWaitHandle) ['{6D28E618-8FEF-441B-92EB-4F1F92130BF2}']
end;
TPkgWaitHandle = class(TInterfacedObject, IPkgWaitHandle)
private
FPadlock: TObject;
FNotifyList: TList<TProc<IPkgWaitHandle>>;
FWaiterCount: integer;
FIsManualReset: boolean;
protected
procedure incWaiters;
procedure decWaiters;
procedure doNotify(ANotifyCount: integer);
procedure atomic(AHandler: TProc);
procedure lock;
procedure unlock;
procedure addWaiter(AWaiter: TProc<IPkgWaitHandle>);
procedure removeWaiter(AWaiter: TProc<IPkgWaitHandle>);
public
constructor Create(AManualResetEvent: boolean);
destructor Destroy; override;
function WaitFor(Timeout: LongWord): TWaitResult; virtual;
class function WaitAny1(AItems: array of IPkgWaitHandle; ATimeout: LongWord): integer;
class function WaitAny(AItems: array of const; ATimeout: LongWord): integer; overload;
class function WaitAny(AItems: TArray<IPkgWaitHandle>; ATimeout: LongWord): integer; overload;
class function WaitList(AItems: array of const): TArray<IPkgWaitHandle>;
function ToWaitHandle: IPkgWaitHandle;
end;
TPkgEvent = class(TPkgWaitHandle, IPkgWaitHandle, IPkgEvent)
private
FEvent: TEvent;
protected
public
constructor Create(AManualResetEvent, AInitialState: boolean);
destructor Destroy; override;
procedure setEvent;
procedure resetEvent;
function WaitFor(ATimeout: LongWord): TWaitResult; override;
end;
TPkgSemaphore = class(TPkgWaitHandle, IPkgWaitHandle, IPkgSemaphore)
private
FSemaphore: TSemaphore;
FInitialCount,
FCurrentCount,
FMaxCount: integer;
protected
public
constructor Create(AInitialCount, AMaximumCount: Integer);
destructor Destroy; override;
procedure Acquire;
procedure Release;
function WaitFor(ATimeout: LongWord): TWaitResult; override;
end;
TPkgTaskWaiter = class(TPkgWaitHandle, IPkgWaitHandle, IPkgTaskWaiter)
private
FTask: ITask;
FEvent: TLightweightEvent;
FTaskCompletionHandler: TProc<ITask>;
protected
public
constructor Create(ATask: ITask);
destructor Destroy; override;
function WaitFor(ATimeout: LongWord): TWaitResult; override;
end;
TPkgAbstractExternalWaitHandle = class abstract(TPkgWaitHandle, IPkgWaitHandle)
end;
TPkgExternalEvent = class(TPkgEvent, IPkgEvent)
end;
implementation
uses strUtils;
{ TPkgWaitable }
constructor TPkgWaitHandle.Create(AManualResetEvent: boolean);
begin
inherited Create;
FPadlock := TObject.Create;
FNotifyList := TList<TProc<IPkgWaitHandle>>.Create;
FIsManualReset := AManualResetEvent;
end;
procedure TPkgWaitHandle.decWaiters;
begin
TMonitor.Enter(FPadlock);
try
dec(FWaiterCount);
finally
TMonitor.Exit(FPadlock);
end;
end;
destructor TPkgWaitHandle.Destroy;
begin
FreeAndNil(FNotifyList);
FreeAndNil(FPadlock);
inherited;
end;
procedure TPkgWaitHandle.incWaiters;
begin
TMonitor.Enter(FPadlock);
try
inc(FWaiterCount);
finally
TMonitor.Exit(FPadlock);
end;
end;
class function TPkgWaitHandle.WaitAny(AItems: array of const;
ATimeout: LongWord): integer;
begin
result := WaitAny(TPkgWaitHandle.WaitList(AItems), ATimeout);
end;
class function TPkgWaitHandle.WaitAny(AItems: TArray<IPkgWaitHandle>;
ATimeout: LongWord): integer;
var
LItems: array of IPkgWaitHandle;
k: Integer;
begin
setLength(LItems, length(AItems));
for k := low(AItems) to high(AItems) do
LItems[k] := AItems[k];
result := waitAny1(LItems, ATimeout);
setLength(LItems, 0);
end;
function TPkgWaitHandle.WaitFor(Timeout: LongWord): TWaitResult;
begin
raise Exception.Create('Not Implemented!');
end;
class function TPkgWaitHandle.WaitList(
AItems: array of const): TArray<IPkgWaitHandle>;
var
k: Integer;
i: IInterface;
o: TObject;
LClassName: string;
begin
setLength(result, length(AItems));
for k := low(AItems) to high(AItems) do
begin
if TVarRec(AItems[k]).VType <> vtInterface then
raise Exception.CreateFmt('Element %d is not an interface!', [k]);
i := IInterface(AItems[k].VInterface);
o := TObject(i);
LClassName := LowerCase(o.QualifiedClassName);
if not o.InheritsFrom(TPkgWaitHandle) then
if o.InheritsFrom(System.Threading.TTask) then
i := TPkgTaskWaiter.Create(ITask(i))
else
raise Exception.CreateFmt('Parameter %d must be a Waitable or an ITask!', [k]);
result[k] := (i as IPkgWaitHandle);
end;
end;
class function TPkgWaitHandle.WaitAny1(AItems: array of IPkgWaitHandle;
ATimeout: LongWord): integer;
var
LWaitable, LSignaledWaitable: IPkgWaitHandle;
k: integer;
LEvent: TLightweightEvent;
LSignaledProc: TProc<IPkgWaitHandle>;
LWaitRes: TWaitResult;
begin
LSignaledWaitable := nil;
result := -1;
// First check if we have already signalled events
for k := low(AItems) to high(AItems) do
begin
LWaitable := AItems[k];
if LWaitable.WaitFor(0) = wrSignaled then
begin
exit(k);
end;
end;
if (result = -1) AND (length(AItems) > 0) then
begin
LEvent := TLightweightEvent.Create(false);
try
// Local OnSignaled handler
LSignaledProc :=
procedure(AWaitable: IPkgWaitHandle)
begin
if TInterlocked.CompareExchange(pointer(LSignaledWaitable), pointer(AWaitable), pointer(nil)) = nil then
begin
LEvent.SetEvent;
end;
TPkgWaitHandle(AWaitable).removeWaiter(LSignaledProc);
end;
for k := low(AItems) to high(AItems) do
begin
LWaitable := AItems[k];
TPkgWaitHandle(LWaitable).lock;
try
// Again, check for already signaled events
if LWaitable.WaitFor(0) = wrSignaled then
begin
// Execute Local Handler
LSignaledProc(LWaitable);
break;
end
else
// Add Local Handler to Event's Notify list
TPkgWaitHandle(LWaitable).FNotifyList.Add(LSignaledProc);
finally
TPkgWaitHandle(LWaitable).unlock;
end;
end;
LWaitRes := LEvent.WaitFor(ATimeout);
// Update Event Index and remove Local Handlers
for k := low(AItems) to high(AItems) do
begin
LWaitable := AItems[k];
TPkgWaitHandle(LWaitable).lock;
try
if (LSignaledWaitable = LWaitable) AND (LWaitRes <> wrTimeout) then
result := k;
TPkgWaitHandle(LWaitable).FNotifyList.Remove(LSignaledProc);
finally
TPkgWaitHandle(LWaitable).unlock;
end;
end;
finally
FreeAndNil(LEvent);
end;
end;
end;
procedure TPkgWaitHandle.doNotify(ANotifyCount: integer);
var
LNotifyProc: TProc<IPkgWaitHandle>;
k, cnt: integer;
begin
cnt := 0;
if FWaiterCount = 0 then
begin
for k := FNotifyList.Count - 1 downto 0 do
begin
LNotifyProc := FNotifyList[k];
LNotifyProc(self as IPkgWaitHandle);
inc(cnt);
if (ANotifyCount > 0) AND (cnt >= ANotifyCount) then exit;
end;
end;
end;
procedure TPkgWaitHandle.addWaiter(AWaiter: TProc<IPkgWaitHandle>);
begin
TMonitor.Enter(FPadlock);
try
FNotifyList.Add(AWaiter);
finally
TMonitor.Exit(FPadlock);
end;
end;
procedure TPkgWaitHandle.atomic(AHandler: TProc);
begin
TMonitor.Enter(FPadlock);
try
AHandler;
finally
TMonitor.Exit(FPadlock);
end;
end;
procedure TPkgWaitHandle.lock;
begin
TMonitor.Enter(FPadlock);
end;
procedure TPkgWaitHandle.removeWaiter(AWaiter: TProc<IPkgWaitHandle>);
begin
TMonitor.Enter(FPadlock);
try
FNotifyList.Remove(AWaiter);
finally
TMonitor.Exit(FPadlock);
end;
end;
function TPkgWaitHandle.ToWaitHandle: IPkgWaitHandle;
begin
result := self as IPkgWaitHandle;
end;
procedure TPkgWaitHandle.unlock;
begin
TMonitor.Exit(FPadlock);
end;
{ TPkgEvent }
constructor TPkgEvent.Create(AManualResetEvent, AInitialState: boolean);
begin
inherited Create(AManualResetEvent);
FEvent := TEvent.Create(nil, AManualResetEvent, AInitialState, '');
end;
destructor TPkgEvent.Destroy;
begin
FreeAndNil(FEvent);
inherited;
end;
procedure TPkgEvent.resetEvent;
begin
TMonitor.Enter(FPadlock);
try
FEvent.ResetEvent;
finally
TMonitor.Exit(FPadlock);
end;
end;
procedure TPkgEvent.setEvent;
begin
TMonitor.Enter(FPadlock);
try
FEvent.SetEvent;
if FIsManualReset then
doNotify(1)
else
doNotify(-1);
finally
TMonitor.Exit(FPadlock);
end;
end;
function TPkgEvent.WaitFor(ATimeout: LongWord): TWaitResult;
begin
incWaiters;
try
result := FEvent.WaitFor(ATimeout);
finally
decWaiters;
end;
end;
{ TPkgSemaphore }
procedure TPkgSemaphore.Acquire;
begin
WaitFor(INFINITE);
end;
constructor TPkgSemaphore.Create(AInitialCount, AMaximumCount: Integer);
begin
inherited Create(true);
FSemaphore := TSemaphore.Create(nil, AInitialCount, AMaximumCount, '');
FInitialCount := AInitialCount;
FCurrentCount := AInitialCount;
FMaxCount := AMaximumCount;
end;
destructor TPkgSemaphore.Destroy;
begin
FreeAndNil(FSemaphore);
inherited;
end;
procedure TPkgSemaphore.Release;
var
LNotifyCount: integer;
begin
TMonitor.Enter(FPadlock);
try
FSemaphore.Release;
inc(FCurrentCount);
if FWaiterCount = 0 then
begin
LNotifyCount := FMaxCount - FCurrentCount;
doNotify(LNotifyCount);
end;
finally
TMonitor.Exit(FPadlock);
end;
end;
function TPkgSemaphore.WaitFor(ATimeout: LongWord): TWaitResult;
var
LNotifyCount: integer;
begin
result := wrTimeout;
incWaiters;
try
result := FSemaphore.WaitFor(ATimeout);
finally
TMonitor.Enter(FPadlock);
try
if result = wrSignaled then
dec(FCurrentCount);
decWaiters;
// In case when this is the last waiter
if (result = wrSignaled) AND (FWaiterCount = 0) then
begin
LNotifyCount := FMaxCount - FCurrentCount;
doNotify(LNotifyCount);
end;
finally
TMonitor.Exit(FPadlock);
end;
end;
end;
{ TPkgTaskWaiter }
constructor TPkgTaskWaiter.Create(ATask: ITask);
begin
inherited Create(true);
FEvent := TLightweightEvent.Create(false);
FTask := ATask;
FTaskCompletionHandler :=
procedure(ATask: ITask)
begin
FEvent.SetEvent;
ATask := nil;
TMonitor.Enter(FPadlock);
try
doNotify(-1);
finally
TMonitor.Exit(FPadlock);
end;
end;
TPkgTaskHack(TTask(FTask)).AddCompleteEvent(FTaskCompletionHandler);
end;
destructor TPkgTaskWaiter.Destroy;
begin
TPkgTaskHack(TTask(FTask)).RemoveCompleteEvent(FTaskCompletionHandler);
FTaskCompletionHandler := nil;
FTask := nil;
FreeAndNil(FEvent);
inherited;
end;
function TPkgTaskWaiter.WaitFor(ATimeout: LongWord): TWaitResult;
begin
result := wrTimeout;
incWaiters;
try
result := FEvent.WaitFor(ATimeout);
finally
TMonitor.Enter(FPadlock);
try
decWaiters;
// In case when this is the last waiter
if result = wrSignaled then
doNotify(-1);
finally
TMonitor.Exit(FPadlock);
end;
end;
end;
end.