forked from exilon/QuickLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick.Threads.pas
637 lines (552 loc) · 15.1 KB
/
Quick.Threads.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
{ ***************************************************************************
Copyright (c) 2016-2018 Kike Pérez
Unit : Quick.Threads
Description : Thread safe collections
Author : Kike Pérez
Version : 1.2
Created : 09/03/2018
Modified : 07/04/2018
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Threads;
{$i QuickLib.inc}
interface
uses
Classes,
Types,
//Quick.Chrono,
{$IFNDEF FPC}
System.RTLConsts,
System.Generics.Collections,
System.SyncObjs;
{$ELSE}
RtlConsts,
Generics.Collections,
syncobjs,
SysUtils;
{$ENDIF}
type
TThreadedQueueCS<T> = class
private
FQueue: array of T;
FQueueSize, FQueueOffset: Integer;
FQueueLock: TCriticalSection;
{$IFDEF FPC}
FQueueCondVar : TEventObject;
{$ELSE}
FQueueCondVar: TConditionVariableCS;
{$ENDIF}
FShutDown: Boolean;
FPushTimeout, FPopTimeout: Cardinal;
FTotalItemsPushed, FTotalItemsPopped: Cardinal;
public
constructor Create(AQueueDepth: Integer = 10; PushTimeout: Cardinal = INFINITE; PopTimeout: Cardinal = INFINITE);
destructor Destroy; override;
procedure Grow(ADelta: Integer);
function PushItem(const AItem: T): TWaitResult; overload;
function PushItem(const AItem: T; var AQueueSize: Integer): TWaitResult; overload;
function PopItem: T; overload;
function PopItem(var AQueueSize: Integer): T; overload;
function PopItem(var AQueueSize: Integer; var AItem: T): TWaitResult; overload;
function PopItem(var AItem: T): TWaitResult; overload;
procedure DoShutDown;
property QueueSize: Integer read FQueueSize;
property ShutDown: Boolean read FShutDown;
property TotalItemsPushed: Cardinal read FTotalItemsPushed;
property TotalItemsPopped: Cardinal read FTotalItemsPopped;
end;
TThreadedQueueList<T> = class
private
fQueue : TQueue<T>;
fQueueSize : Integer;
fQueueLock : TCriticalSection;
{$IFDEF FPC}
FQueueCondVar : TSimpleEvent;
{$ELSE}
FQueueCondVar: TConditionVariableCS;
{$ENDIF}
fShutDown : Boolean;
fPushTimeout : Cardinal;
fPopTimeout : Cardinal;
fTotalItemsPushed : Cardinal;
fTotalItemsPopped : Cardinal;
public
constructor Create(AQueueDepth: Integer = 10; PushTimeout: Cardinal = INFINITE; PopTimeout: Cardinal = INFINITE);
destructor Destroy; override;
procedure Grow(ADelta: Integer);
function PushItem(const AItem: T): TWaitResult; overload;
function PushItem(const AItem: T; var AQueueSize: Integer): TWaitResult; overload;
function PopItem: T; overload;
function PopItem(var AQueueSize: Integer): T; overload;
function PopItem(var AQueueSize: Integer; var AItem: T): TWaitResult; overload;
function PopItem(var AItem: T): TWaitResult; overload;
procedure DoShutDown;
property QueueSize: Integer read FQueueSize;
property ShutDown: Boolean read FShutDown;
property TotalItemsPushed: Cardinal read FTotalItemsPushed;
property TotalItemsPopped: Cardinal read FTotalItemsPopped;
end;
TThreadTask<T> = class(TThread)
private
fMaxQueue : Integer;
fInsertTimeout : Integer;
fExtractTimeout : Integer;
fTaskQueue : TThreadedQueueCS<T>;
function GetTaskQueue : Cardinal;
public
constructor Create;
destructor Destroy; override;
property MaxQueue : Integer read fMaxQueue write fMaxQueue;
property InsertTimeout : Integer read fInsertTimeout write fInsertTimeout;
property ExtractTimeout : Integer read fExtractTimeout write fExtractTimeout;
property TaskQueue : Cardinal read GetTaskQueue;
procedure Execute; override;
function AddTask(Task : T) : Boolean;
procedure Start;
end;
{$IFNDEF FPC}
TThreadObjectList<T: class> = class(TList<T>)
private
fList: TObjectList<T>;
fLock: TObject;
fDuplicates: TDuplicates;
function GetItem(aIndex : Integer) : T;
procedure SetItem(aIndex : Integer; aValue : T);
public
constructor Create(OwnedObjects : Boolean);
destructor Destroy; override;
property Items[Index : Integer] : T read GetItem write SetItem ; default;
procedure Add(const Item: T);
procedure Clear;
function LockList: TObjectList<T>;
procedure Remove(const Item: T); inline;
procedure RemoveItem(const Item: T; Direction: TDirection);
procedure UnlockList; inline;
property Duplicates: TDuplicates read fDuplicates write fDuplicates;
end;
{$ENDIF}
implementation
{ TThreadedQueueCS<T> }
constructor TThreadedQueueCS<T>.Create(AQueueDepth: Integer = 10; PushTimeout: Cardinal = INFINITE; PopTimeout: Cardinal = INFINITE);
begin
inherited Create;
SetLength(FQueue, AQueueDepth);
FQueueLock := TCriticalSection.Create;
{$IFDEF FPC}
FQueueCondVar := TEventObject.Create(nil, True, False, 'TQCS');
{$ELSE}
FQueueCondVar := TConditionVariableCS.Create;
{$ENDIF}
FPushTimeout := PushTimeout;
FPopTimeout := PopTimeout;
end;
destructor TThreadedQueueCS<T>.Destroy;
begin
DoShutDown;
FQueueLock.Free;
FQueueCondVar.Free;
inherited;
end;
procedure TThreadedQueueCS<T>.Grow(ADelta: Integer);
begin
FQueueLock.Enter;
try
SetLength(FQueue, Length(FQueue) + ADelta);
finally
FQueueLock.Leave;
end;
{$IFDEF FPC}
FQueueCondVar.SetEvent;
{$ELSE}
FQueueCondVar.ReleaseAll;
{$ENDIF}
end;
function TThreadedQueueCS<T>.PopItem: T;
var
LQueueSize: Integer;
begin
PopItem(LQueueSize, Result);
end;
function TThreadedQueueCS<T>.PopItem(var AQueueSize: Integer; var AItem: T): TWaitResult;
begin
AItem := Default(T);
FQueueLock.Enter;
try
Result := wrSignaled;
while (Result = wrSignaled) and (FQueueSize = 0) and not FShutDown do
begin
{$IFDEF FPC}
Result := FQueueCondVar.WaitFor(FPopTimeout);
{$ELSE}
Result := FQueueCondVar.WaitFor(FQueueLock, FPopTimeout);
{$ENDIF}
end;
if (FShutDown and (FQueueSize = 0)) or (Result <> wrSignaled) then Exit;
AItem := FQueue[FQueueOffset];
FQueue[FQueueOffset] := Default(T);
if FQueueSize = Length(FQueue) then
begin
{$IFDEF FPC}
FQueueCondVar.SetEvent;
{$ELSE}
FQueueCondVar.ReleaseAll;
{$ENDIF}
end;
Dec(FQueueSize);
Inc(FQueueOffset);
Inc(FTotalItemsPopped);
if FQueueOffset = Length(FQueue) then FQueueOffset := 0;
finally
AQueueSize := FQueueSize;
FQueueLock.Leave;
end;
end;
function TThreadedQueueCS<T>.PopItem(var AItem: T): TWaitResult;
var
LQueueSize: Integer;
begin
Result := PopItem(LQueueSize, AItem);
end;
function TThreadedQueueCS<T>.PopItem(var AQueueSize: Integer): T;
begin
PopItem(AQueueSize, Result);
end;
function TThreadedQueueCS<T>.PushItem(const AItem: T): TWaitResult;
var
LQueueSize: Integer;
begin
Result := PushItem(AItem, LQueueSize);
end;
function TThreadedQueueCS<T>.PushItem(const AItem: T; var AQueueSize: Integer): TWaitResult;
begin
FQueueLock.Enter;
try
Result := wrSignaled;
while (Result = wrSignaled) and (FQueueSize = Length(FQueue)) and not FShutDown do
begin
{$IFDEF FPC}
Result := FQueueCondVar.WaitFor(FPushTimeout);
{$ELSE}
Result := FQueueCondVar.WaitFor(FQueueLock, FPushTimeout);
{$ENDIF}
end;
if FShutDown or (Result <> wrSignaled) then Exit;
if FQueueSize = 0 then
begin
{$IFDEF FPC}
FQueueCondVar.SetEvent;
{$ELSE}
FQueueCondVar.ReleaseAll;
{$ENDIF}
end;
FQueue[(FQueueOffset + FQueueSize) mod Length(FQueue)] := AItem;
Inc(FQueueSize);
Inc(FTotalItemsPushed);
finally
AQueueSize := FQueueSize;
FQueueLock.Leave;
end;
end;
procedure TThreadedQueueCS<T>.DoShutDown;
begin
FShutDown := True;
{$IFDEF FPC}
FQueueCondVar.SetEvent;
{$ELSE}
FQueueCondVar.ReleaseAll;
{$ENDIF}
end;
{ TThreadedQueueList<T> }
constructor TThreadedQueueList<T>.Create(AQueueDepth: Integer = 10; PushTimeout: Cardinal = INFINITE; PopTimeout: Cardinal = INFINITE);
begin
inherited Create;
fQueue := TQueue<T>.Create;
fQueue.Capacity := AQueueDepth;
fQueueSize := 0;
fQueueLock := TCriticalSection.Create;
{$IFDEF FPC}
FQueueCondVar := TSimpleEvent.Create; //TEventObject.Create(nil, False, False, 'TQL');
{$ELSE}
fQueueCondVar := TConditionVariableCS.Create;
{$ENDIF}
fPushTimeout := PushTimeout;
fPopTimeout := PopTimeout;
end;
destructor TThreadedQueueList<T>.Destroy;
begin
DoShutDown;
fQueueLock.Free;
fQueueCondVar.Free;
fQueue.Free;
inherited;
end;
procedure TThreadedQueueList<T>.Grow(ADelta: Integer);
begin
fQueueLock.Enter;
try
fQueue.Capacity := fQueue.Capacity + ADelta;
finally
fQueueLock.Leave;
end;
{$IFDEF FPC}
FQueueCondVar.SetEvent;
{$ELSE}
FQueueCondVar.ReleaseAll;
{$ENDIF}
end;
function TThreadedQueueList<T>.PopItem: T;
var
LQueueSize: Integer;
begin
PopItem(LQueueSize, Result);
end;
{$IFDEF FPC}
function TThreadedQueueList<T>.PopItem(var AQueueSize: Integer; var AItem: T): TWaitResult;
//var
//crono : TChronometer;
begin
AItem := Default(T);
//crono := TChronometer.Create(False);
try
Result := wrSignaled;
//writeln('popitem');
//crono.Start;
while (Result = wrSignaled) and (fQueueSize = 0) and not fShutDown do
begin
//crono.Start;
Result := FQueueCondVar.WaitFor(FPopTimeout);
//crono.Stop;
//writeln('in: ' + crono.ElapsedTime);
//if result = twaitresult.wrError then result := twaitresult.wrError;
end;
//crono.Stop;
//writeln('out: ' + crono.ElapsedTime);
fQueueLock.Enter;
try
if (FShutDown and (fQueueSize = 0)) or (Result <> wrSignaled) then Exit;
AItem := fQueue.Extract;
Dec(FQueueSize);
Inc(fTotalItemsPopped);
finally
fQueueLock.Leave;
end;
finally
AQueueSize := fQueueSize;
end;
end;
{$ELSE}
function TThreadedQueueList<T>.PopItem(var AQueueSize: Integer; var AItem: T): TWaitResult;
begin
AItem := Default(T);
fQueueLock.Enter;
try
Result := wrSignaled;
while (Result = wrSignaled) and (fQueueSize = 0) and not fShutDown do
begin
Result := FQueueCondVar.WaitFor(FQueueLock, FPopTimeout);
end;
if (FShutDown and (fQueueSize = 0)) or (Result <> wrSignaled) then Exit;
AItem := fQueue.Extract;
if fQueueSize = fQueue.Count then
begin
FQueueCondVar.ReleaseAll;
end;
Dec(FQueueSize);
Inc(fTotalItemsPopped);
finally
AQueueSize := fQueueSize;
fQueueLock.Leave;
end;
end;
{$ENDIF}
function TThreadedQueueList<T>.PopItem(var AItem: T): TWaitResult;
var
LQueueSize: Integer;
begin
Result := PopItem(LQueueSize, AItem);
end;
function TThreadedQueueList<T>.PopItem(var AQueueSize: Integer): T;
begin
PopItem(AQueueSize, Result);
end;
function TThreadedQueueList<T>.PushItem(const AItem: T): TWaitResult;
var
LQueueSize: Integer;
begin
Result := PushItem(AItem, LQueueSize);
end;
{$IFDEF FPC}
function TThreadedQueueList<T>.PushItem(const AItem: T; var AQueueSize: Integer): TWaitResult;
begin
FQueueLock.Enter;
try
Result := wrSignaled;
//while (Result = wrSignaled) and (fQueueSize = fQueue.Count) and not fShutDown do
//begin
// Result := fQueueCondVar.WaitFor(fQueueLock, fPushTimeout);
//end;
if fShutDown or (Result <> wrSignaled) then Exit;
//if fQueueSize = 0 then
//begin
// FQueueCondVar.SetEvent;
//end;
fQueue.Enqueue(AItem);
Inc(FQueueSize);
Inc(fTotalItemsPushed);
finally
AQueueSize := fQueueSize;
FQueueLock.Leave;
//FQueueCondVar.SetEvent;
end;
end;
{$ELSE}
function TThreadedQueueList<T>.PushItem(const AItem: T; var AQueueSize: Integer): TWaitResult;
begin
FQueueLock.Enter;
try
Result := wrSignaled;
//while (Result = wrSignaled) and (fQueueSize = fQueue.Count) and not fShutDown do
//begin
// Result := fQueueCondVar.WaitFor(fQueueLock, fPushTimeout);
//end;
if fShutDown or (Result <> wrSignaled) then Exit;
if fQueueSize = 0 then FQueueCondVar.ReleaseAll;
fQueue.Enqueue(AItem);
Inc(FQueueSize);
Inc(fTotalItemsPushed);
finally
AQueueSize := fQueueSize;
FQueueLock.Leave;
end;
end;
{$ENDIF}
procedure TThreadedQueueList<T>.DoShutDown;
begin
fShutDown := True;
{$IFDEF FPC}
FQueueCondVar.SetEvent;
{$ELSE}
FQueueCondVar.ReleaseAll;
{$ENDIF}
end;
{ TThreadTask<T> }
function TThreadTask<T>.AddTask(Task: T): Boolean;
begin
Result := fTaskQueue.PushItem(Task) = TWaitResult.wrSignaled;
end;
constructor TThreadTask<T>.Create;
begin
inherited Create(True);
fMaxQueue := 10;
fInsertTimeout := INFINITE;
fExtractTimeout := INFINITE;
end;
destructor TThreadTask<T>.Destroy;
begin
if Assigned(fTaskQueue) then fTaskQueue.Free;
inherited;
end;
procedure TThreadTask<T>.Execute;
begin
inherited;
end;
function TThreadTask<T>.GetTaskQueue: Cardinal;
begin
if Assigned(fTaskQueue) then Result := fTaskQueue.QueueSize
else Result := 0;
end;
procedure TThreadTask<T>.Start;
begin
fTaskQueue := TThreadedQueueCS<T>.Create(fMaxQueue,fInsertTimeout,fExtractTimeout);
end;
{$IFNDEF FPC}
{ TThreadObjectList<T> }
procedure TThreadObjectList<T>.Add(const Item: T);
begin
LockList;
try
if (Duplicates = dupAccept) or (fList.IndexOf(Item) = -1) then fList.Add(Item)
else if Duplicates = dupError then raise EListError.CreateFmt(SDuplicateItem, [fList.ItemValue(Item)]);
finally
UnlockList;
end;
end;
procedure TThreadObjectList<T>.Clear;
begin
LockList;
try
fList.Clear;
finally
UnlockList;
end;
end;
constructor TThreadObjectList<T>.Create(OwnedObjects : Boolean);
begin
inherited Create;
fLock := TObject.Create;
fList := TObjectList<T>.Create;
fDuplicates := dupIgnore;
end;
destructor TThreadObjectList<T>.Destroy;
begin
LockList;
try
fList.Free;
inherited Destroy;
finally
UnlockList;
fLock.Free;
end;
end;
function TThreadObjectList<T>.GetItem(aIndex: Integer): T;
begin
LockList;
try
Result := fList[aIndex];
finally
UnlockList;
end;
end;
function TThreadObjectList<T>.LockList: TObjectList<T>;
begin
System.TMonitor.Enter(fLock);
Result := fList;
end;
procedure TThreadObjectList<T>.Remove(const Item: T);
begin
RemoveItem(Item, TDirection.FromBeginning);
end;
procedure TThreadObjectList<T>.RemoveItem(const Item: T; Direction: TDirection);
begin
LockList;
try
fList.RemoveItem(Item, Direction);
finally
UnlockList;
end;
end;
procedure TThreadObjectList<T>.SetItem(aIndex: Integer; aValue: T);
begin
LockList;
try
fList[aIndex] := aValue;
finally
UnlockList;
end;
end;
procedure TThreadObjectList<T>.UnlockList;
begin
System.TMonitor.Exit(fLock);
end;
{$ENDIF}
end.