/
sortedthreadpool.pas
959 lines (842 loc) · 22.5 KB
/
sortedthreadpool.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
{
SortedThreadPool
Modul fo async execution of clients jobs with ranking
Based on
Copyright (C) 2011 Maciej Kaczkowski / keit.co
Added jobs ranking
Copyright (C) 2020 Ilya Medvedkov
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit SortedThreadPool;
{$mode objfpc}{$H+}
interface
uses
AvgLvlTree, ECommonObjs, OGLFastList, Classes, SysUtils;
type
TSortedThreadPool = class;
{ TLinearJob }
TLinearJob = class
private
FNeedToRestart : Boolean;
FHoldOnValueMs : Cardinal;
FHoldStart : QWord;
public
constructor Create;
//set NeedToRestart = true if need to restart job
//after execution
procedure RestartJob(aHoldDelay : Cardinal; const aHoldStart : QWord);
property NeedToRestart : Boolean read FNeedToRestart write FNeedToRestart;
property HoldOnValueMs : Cardinal read FHoldOnValueMs write FHoldOnValueMs;
function IsReady(const TS: QWord): Cardinal;
procedure Execute; virtual; abstract;
end;
{ TSortedJob }
TSortedJob = class(TLinearJob)
private
FScore : Cardinal;
public
constructor Create(aScore : Cardinal); virtual;
procedure UpdateScore; virtual;
property Score : Cardinal read FScore write FScore;
end;
TPoolThreadKind = (ptkLinear, ptkSorted);
{ TThreadPoolThreadKind }
TThreadPoolThreadKind = class(TNetCustomLockedObject)
private
FValue : TPoolThreadKind;
function GetValue: TPoolThreadKind;
procedure SetValue(AValue: TPoolThreadKind);
public
constructor Create(AValue : TPoolThreadKind);
property Value : TPoolThreadKind read GetValue write SetValue;
end;
TJobToJobWait = record
DefaultValue : Integer;
AdaptMin : Word;
AdaptMax : Word;
end;
{ TThreadJobToJobWait }
TThreadJobToJobWait = class(TNetCustomLockedObject)
private
FValue : TJobToJobWait;
function GetValue: TJobToJobWait;
procedure SetValue(AValue: TJobToJobWait);
public
constructor Create(AValue : TJobToJobWait);
procedure AdaptDropToMin(var AValue: Integer);
procedure AdaptToDec(var AValue : Integer);
procedure AdaptToInc(var AValue : Integer);
function IsEqual(AValue : TJobToJobWait) : Boolean;
property Value : TJobToJobWait read GetValue write SetValue;
end;
TOnRefreshJob = procedure (j : TLinearJob; const delta : Cardinal) of object;
TOnRebornJob = procedure (j : TLinearJob) of object;
{ TWaitingCollection }
TWaitingCollection = class(specialize TFastBaseCollection<TLinearJob>)
private
FDelta : Cardinal;
public
constructor Create(dt : Cardinal);
procedure AddJob(j : TLinearJob);
procedure DeleteJob(i : integer);
procedure Refresh(const TS: QWord; cb : TOnRefreshJob);
end;
{ TWaitingPool }
TWaitingPool = class
private
F1msPool : TWaitingCollection;
F10msPool : TWaitingCollection;
F100msPool : TWaitingCollection;
F1000msPool : TWaitingCollection;
FOnRebornJob: TOnRebornJob;
FTS1ms, FTS10ms, FTS100ms, FTS1000ms : QWord;
procedure DoOnRefreshJob(j : TLinearJob; const delta : Cardinal);
public
constructor Create;
destructor Destroy; override;
procedure AddJob(j : TLinearJob; timeout : Cardinal);
procedure Refresh(const TS: QWord);
property OnRebornJob : TOnRebornJob read FOnRebornJob write FOnRebornJob;
end;
{ TSortedCustomThread }
TSortedCustomThread = class(TThread)
private
FOwner: TSortedThreadPool;
FRunning: TThreadBoolean;
FJob : TLinearJob;
FThreadKind : TThreadPoolThreadKind;
FSleepTime : TThreadJobToJobWait;
FWaitedJobs : TWaitingPool;
procedure DoOnRebornJob(j : TLinearJob);
procedure SendForWaiting(j: TLinearJob);
function GetRunning: Boolean;
function GetSleepTime: TJobToJobWait;
function GetThreadKind: TPoolThreadKind;
procedure SetSleepTime(AValue: TJobToJobWait);
procedure SetThreadKind(AValue: TPoolThreadKind);
public
procedure Execute; override;
constructor Create(AOwner: TSortedThreadPool;
AKind : TPoolThreadKind;
aSleepTime : TJobToJobWait);
destructor Destroy; override;
property Running: Boolean read GetRunning;
property myJob : TLinearJob read FJob write FJob;
property ThreadKind : TPoolThreadKind read GetThreadKind write SetThreadKind;
property SleepTime : TJobToJobWait read GetSleepTime write SetSleepTime;
end;
{ TSortedThreadPool }
TSortedThreadPool = class
private
FPool: TThreadSafeFastList;
FSortedList: TAvgLvlTree;
FSortedListLocker : TNetCustomLockedObject;
FLinearList : TThreadSafeFastSeq;
FSortedThreadsCount : TThreadInteger;
FLinearThreadsCount : TThreadInteger;
FThreadJobToJobWait : TThreadJobToJobWait;
FRunning: TThreadBoolean;
function GetLinearJobsCount: Integer;
function GetLinearThreadsCount: integer;
function GetRunning: Boolean;
function GetRunningThreads: Integer;
procedure ExcludeThread(aThread : TSortedCustomThread);
function GetSortedJob(const TS: QWord): TSortedJob;
function GetLinearJob(const TS: QWord): TLinearJob;
function GetSortedJobsCount: Integer;
function GetSortedThreadsCount: integer;
function GetThreadJobToJobWait: TJobToJobWait;
function GetThreadsCount: integer;
procedure ResizePool({%H-}aSortedThreads: Integer;
{%H-}aLinearThreads: Integer);
procedure SetLinearThreadsCount(AValue: integer);
procedure SetRunning(AValue: Boolean);
procedure SetSortedThreadsCount(AValue: integer);
procedure SetThreadJobToJobWait(AValue: TJobToJobWait);
public
constructor Create(const OnCompareMethod: TAvgLvlObjectSortCompare;
aSleepTime : TJobToJobWait;
aSortedThreads: Integer = 5;
aLinearThreads: Integer = 5);
destructor Destroy; override;
procedure AddSorted(AJob : TSortedJob);
procedure AddLinear(AJob : TLinearJob);
procedure Terminate;
property Running: Boolean read GetRunning write SetRunning;
property SortedJobsCount: Integer read GetSortedJobsCount;
property LinearJobsCount: Integer read GetLinearJobsCount;
//
property ThreadsCount : integer read GetThreadsCount;
property ThreadJobToJobWait : TJobToJobWait read GetThreadJobToJobWait
write SetThreadJobToJobWait;
property SortedThreadsCount : integer read GetSortedThreadsCount
write SetSortedThreadsCount;
property LinearThreadsCount : integer read GetLinearThreadsCount
write SetLinearThreadsCount;
end;
const
SORTED_DEFAULT_SLEEP_TIME = 5;
const DefaultJobToJobWait : TJobToJobWait = (DefaultValue : SORTED_DEFAULT_SLEEP_TIME;
AdaptMin : 0;
AdaptMax : 0);
implementation
{ TWaitingPool }
procedure TWaitingPool.DoOnRefreshJob(j: TLinearJob; const delta: Cardinal);
var
target : TWaitingCollection;
begin
case (delta div 4) of
0 : target := nil;
1..9 : target := F1msPool;
10..99 : target := F10msPool;
100..999 : target := F100msPool;
else
target := F1000msPool;
end;
if assigned(target) then
target.AddJob(j)
else
begin
if Assigned(FOnRebornJob) then
FOnRebornJob(j);
end;
end;
constructor TWaitingPool.Create;
begin
F1msPool := TWaitingCollection.Create(1);
F10msPool := TWaitingCollection.Create(10);
F100msPool := TWaitingCollection.Create(100);
F1000msPool := TWaitingCollection.Create(1000);
FTS1ms := GetTickCount64;
FTS10ms := GetTickCount64;
FTS100ms := GetTickCount64;
FTS1000ms := GetTickCount64;
end;
destructor TWaitingPool.Destroy;
begin
F1msPool.Free;
F10msPool.Free;
F100msPool.Free;
F1000msPool.Free;
inherited Destroy;
end;
procedure TWaitingPool.AddJob(j: TLinearJob; timeout: Cardinal);
begin
DoOnRefreshJob(j, timeout);
end;
procedure TWaitingPool.Refresh(const TS: QWord);
var
Delta : Int32;
begin
Delta := Int32(Int64(1) + Int64(FTS1ms) - Int64(TS));
if (Delta <= 0) then
begin
F1msPool.Refresh(TS, @DoOnRefreshJob);
FTS1ms := TS;
end;
Delta := Int32(Int64(10) + Int64(FTS10ms) - Int64(TS));
if (Delta <= 0) then
begin
F10msPool.Refresh(TS, @DoOnRefreshJob);
FTS10ms := TS;
end;
Delta := Int32(Int64(100) + Int64(FTS100ms) - Int64(TS));
if (Delta <= 0) then
begin
F100msPool.Refresh(TS, @DoOnRefreshJob);
FTS100ms := TS;
end;
Delta := Int32(Int64(1000) + Int64(FTS1000ms) - Int64(TS));
if (Delta <= 0) then
begin
F1000msPool.Refresh(TS, @DoOnRefreshJob);
FTS1000ms := TS;
end;
end;
{ TWaitingCollection }
constructor TWaitingCollection.Create(dt: Cardinal);
begin
inherited Create;
FDelta := dt;
end;
procedure TWaitingCollection.AddJob(j: TLinearJob);
var
i : integer;
begin
i := IndexOf(nil);
if i < 0 then
Add(j)
else
Item[i] := j;
end;
procedure TWaitingCollection.DeleteJob(i: integer);
begin
Item[i] := nil;
end;
procedure TWaitingCollection.Refresh(const TS: QWord; cb : TOnRefreshJob);
var
i : integer;
nd : Cardinal;
j : TLinearJob;
begin
for i := 0 to Count-1 do
begin
j := Item[i];
if Assigned(j) then
begin
nd := Item[i].IsReady(TS);
if (nd div FDelta) = 0 then
begin
Item[i] := nil;
cb(j, nd);
end;
end;
end;
end;
{ TLinearJob }
function TLinearJob.IsReady(const TS: QWord): Cardinal;
var
Delta : Int32;
begin
if FHoldOnValueMs > 0 then
begin
Delta := Int32(Int64(FHoldOnValueMs) + Int64(FHoldStart) - Int64(TS));
if Delta < 0 then
begin
Result := 0;
FHoldOnValueMs := 0;
end else
Result := Cardinal(Delta);
end else Result := 0;
end;
constructor TLinearJob.Create;
begin
FNeedToRestart := false;
FHoldOnValueMs := 0;
FHoldStart:= 0;
end;
procedure TLinearJob.RestartJob(aHoldDelay: Cardinal; const aHoldStart: QWord);
begin
FNeedToRestart := true;
FHoldStart := aHoldStart;
FHoldOnValueMs:=aHoldDelay;
end;
{ TThreadJobToJobWait }
function TThreadJobToJobWait.GetValue: TJobToJobWait;
begin
Lock;
try
Result := FValue;
finally
UnLock;
end;
end;
procedure TThreadJobToJobWait.SetValue(AValue: TJobToJobWait);
begin
Lock;
try
FValue := AValue;
finally
UnLock;
end;
end;
constructor TThreadJobToJobWait.Create(AValue: TJobToJobWait);
begin
inherited Create;
if AValue.DefaultValue < 0 then AValue.DefaultValue:= SORTED_DEFAULT_SLEEP_TIME;
FValue := AValue;
end;
procedure TThreadJobToJobWait.AdaptDropToMin(var AValue: Integer);
begin
Lock;
try
AValue := FValue.AdaptMin;
finally
UnLock;
end;
end;
procedure TThreadJobToJobWait.AdaptToDec(var AValue: Integer);
begin
Lock;
try
AValue := AValue shr 1;
if AValue < FValue.AdaptMin then
AValue := FValue.AdaptMin;
finally
UnLock;
end;
end;
procedure TThreadJobToJobWait.AdaptToInc(var AValue: Integer);
begin
Lock;
try
if AValue <= 0 then AValue := 1 else
AValue := AValue shl 1;
if AValue > FValue.AdaptMax then
AValue := FValue.AdaptMax;
finally
UnLock;
end;
end;
function TThreadJobToJobWait.IsEqual(AValue: TJobToJobWait): Boolean;
begin
Lock;
try
Result := (FValue.DefaultValue = AValue.DefaultValue) and
(FValue.AdaptMax = AValue.AdaptMax) and
(FValue.AdaptMin = AValue.AdaptMin);
finally
UnLock;
end;
end;
{ TThreadPoolThreadKind }
function TThreadPoolThreadKind.GetValue: TPoolThreadKind;
begin
Lock;
try
Result := FValue;
finally
UnLock;
end;
end;
procedure TThreadPoolThreadKind.SetValue(AValue: TPoolThreadKind);
begin
Lock;
try
FValue:= AValue;
finally
UnLock;
end;
end;
constructor TThreadPoolThreadKind.Create(AValue: TPoolThreadKind);
begin
inherited Create;
FValue:= AValue;
end;
{ TSortedJob }
constructor TSortedJob.Create(aScore: Cardinal);
begin
FScore:= aScore;
end;
procedure TSortedJob.UpdateScore;
begin
Inc(FScore);
end;
{ TSortedThreadPool }
function TSortedThreadPool.GetSortedJob(const TS : QWord) : TSortedJob;
var JN : TAvgLvlTreeNode;
begin
FSortedListLocker.Lock;
try
if FSortedList.Count > 0 then
begin
JN := FSortedList.FindLowest;
Result := TSortedJob(JN.Data);
while assigned(Result) and (Result.IsReady(TS) > 0) do
begin
JN := JN.Successor;
if assigned(JN) then
Result := TSortedJob(JN.Data) else
Result := nil;
end;
if assigned(JN) then
FSortedList.Delete(JN);
end else Result := nil;
finally
FSortedListLocker.UnLock;
end;
end;
function TSortedThreadPool.GetLinearJob(const TS : QWord) : TLinearJob;
begin
Result := TLinearJob(FLinearList.PopValue);
if assigned(Result) and (Result.IsReady(TS) > 0) then
begin
FLinearList.Push_back(Result);
Result := nil;
end;
end;
function TSortedThreadPool.GetSortedJobsCount: Integer;
begin
FSortedListLocker.Lock;
try
Result := FSortedList.Count;
finally
FSortedListLocker.UnLock;
end;
end;
function TSortedThreadPool.GetSortedThreadsCount: integer;
begin
Result := FSortedThreadsCount.Value;
end;
function TSortedThreadPool.GetThreadJobToJobWait: TJobToJobWait;
begin
Result := FThreadJobToJobWait.Value;
end;
function TSortedThreadPool.GetThreadsCount: integer;
begin
Result := SortedThreadsCount + LinearThreadsCount;
end;
procedure TSortedThreadPool.ResizePool(aSortedThreads: Integer;
aLinearThreads: Integer);
var MSC, MLC, i, j : Integer;
AThread : TSortedCustomThread;
FT : TJobToJobWait;
begin
MSC := FSortedThreadsCount.Value;
MLC := FLinearThreadsCount.Value;
FPool.Lock;
try
for i := 0 to FPool.Count-1 do
begin
if assigned(FPool[i]) then
begin
AThread := TSortedCustomThread(FPool[i]);
if AThread.ThreadKind = ptkLinear then
begin
if MLC > aLinearThreads then begin
//need to remove this linear thread
//or requalificate it to sorted
if (MSC < aSortedThreads) then
begin
Inc(MSC);
AThread.ThreadKind := ptkSorted;
end else
AThread.Terminate;
Dec(MLC);
end;
end else
begin
if MSC > aSortedThreads then begin
//need to remove this sorted thread
//or requalificate it to linear
if (MSC < aSortedThreads) then
begin
Inc(MLC);
AThread.ThreadKind := ptkLinear;
end else
AThread.Terminate;
Dec(MSC);
end;
end;
end;
end;
FT := ThreadJobToJobWait;
for i := MLC to aLinearThreads-1 do
begin
AThread := TSortedCustomThread.Create(Self, ptkLinear, FT);
j := FPool.IndexOf(nil);
if j >= 0 then
FPool[j] := AThread else
FPool.Add(AThread);
end;
for i := MSC to aSortedThreads-1 do
begin
AThread := TSortedCustomThread.Create(Self, ptkSorted, FT);
j := FPool.IndexOf(nil);
if j >= 0 then
FPool[j] := AThread else
FPool.Add(AThread);
end;
FSortedThreadsCount.Value := aSortedThreads;
FLinearThreadsCount.Value := aLinearThreads;
finally
FPool.UnLock;
end;
end;
procedure TSortedThreadPool.SetLinearThreadsCount(AValue: integer);
begin
ResizePool(FSortedThreadsCount.Value, AValue);
end;
procedure TSortedThreadPool.SetRunning(AValue: Boolean);
begin
FRunning.Value:= AValue;
end;
procedure TSortedThreadPool.SetSortedThreadsCount(AValue: integer);
begin
ResizePool(AValue, FLinearThreadsCount.Value);
end;
procedure TSortedThreadPool.SetThreadJobToJobWait(AValue: TJobToJobWait);
var i : integer;
begin
if AValue.DefaultValue < 0 then AValue.DefaultValue := SORTED_DEFAULT_SLEEP_TIME;
if not FThreadJobToJobWait.IsEqual(AValue) then
begin
FThreadJobToJobWait.Value := AValue;
FPool.Lock;
try
for i := 0 to FPool.Count-1 do
if assigned(FPool[i]) then
TSortedCustomThread(FPool[i]).SleepTime := AValue;
finally
FPool.UnLock;
end;
end;
end;
function TSortedThreadPool.GetLinearJobsCount: Integer;
begin
Result := FLinearList.Count;
end;
function TSortedThreadPool.GetLinearThreadsCount: integer;
begin
Result := FLinearThreadsCount.Value;
end;
function TSortedThreadPool.GetRunning: Boolean;
begin
Result := FRunning.Value;
end;
function TSortedThreadPool.GetRunningThreads: Integer;
var
i: Integer;
begin
FPool.Lock;
try
Result := 0;
for i := 0 to FPool.Count - 1 do
if assigned(FPool[i]) then
if TSortedCustomThread(FPool[i]).Running then
Inc(Result);
finally
FPool.UnLock;
end;
end;
procedure TSortedThreadPool.ExcludeThread(aThread: TSortedCustomThread);
var i : integer;
begin
FPool.Lock;
try
for i := 0 to FPool.Count - 1 do
begin
if (FPool[i] = aThread) then begin
if aThread.ThreadKind = ptkLinear then FLinearThreadsCount.DecValue else
if aThread.ThreadKind = ptkSorted then FSortedThreadsCount.DecValue;
FPool[i] := nil;
end;
end;
finally
FPool.UnLock;
end;
end;
constructor TSortedThreadPool.Create(
const OnCompareMethod: TAvgLvlObjectSortCompare;
aSleepTime: TJobToJobWait;
aSortedThreads: Integer;
aLinearThreads: Integer);
var
i, FThreads: Integer;
FT : TJobToJobWait;
begin
FPool:= TThreadSafeFastList.Create;
FSortedList := TAvgLvlTree.CreateObjectCompare(OnCompareMethod);
FSortedList.OwnsObjects:=false;
FSortedListLocker := TNetCustomLockedObject.Create;
FLinearList := TThreadSafeFastSeq.Create;
FSortedThreadsCount := TThreadInteger.Create(aSortedThreads);
FLinearThreadsCount := TThreadInteger.Create(aLinearThreads);
FThreadJobToJobWait := TThreadJobToJobWait.Create(DefaultJobToJobWait);
ThreadJobToJobWait := aSleepTime;
FT := ThreadJobToJobWait;
FRunning:= TThreadBoolean.Create(false);
FThreads := aSortedThreads + aLinearThreads;
FPool.Lock;
try
for i := 1 to FThreads do begin
if i <= aLinearThreads then
FPool.Add(TSortedCustomThread.Create(Self, ptkLinear, FT)) else
FPool.Add(TSortedCustomThread.Create(Self, ptkSorted, FT));
Sleep(FT.DefaultValue div FThreads);
end;
finally
FPool.UnLock;
end;
end;
destructor TSortedThreadPool.Destroy;
var
i, cnt: Integer;
c: TSortedCustomThread;
begin
FRunning.Value := False;
cnt := 0;
FPool.Lock;
try
for i := 0 to FPool.Count - 1 do
begin
c := TSortedCustomThread(FPool[i]);
if Assigned(c) then
begin
c.Terminate;
inc(cnt);
end;
end;
finally
FPool.Unlock;
end;
while cnt > 0 do
begin
cnt := 0;
for i := 0 to FPool.Count - 1 do
begin
FPool.Lock;
try
c := TSortedCustomThread(FPool[i]);
finally
FPool.UnLock;
end;
if Assigned(c) then
begin
inc(cnt);
end;
end;
Sleep(1);
end;
FPool.Free;
FSortedList.OwnsObjects:=true;
FSortedList.Free;
FLinearList.Free;
FSortedListLocker.Free;
FRunning.Free;
FLinearThreadsCount.Free;
FSortedThreadsCount.Free;
FThreadJobToJobWait.Free;
inherited Destroy;
end;
procedure TSortedThreadPool.AddSorted(AJob: TSortedJob);
begin
FSortedListLocker.Lock;
try
FSortedList.Add(AJob);
finally
FSortedListLocker.UnLock;
end;
end;
procedure TSortedThreadPool.AddLinear(AJob: TLinearJob);
begin
FLinearList.Push_back(AJob);
end;
procedure TSortedThreadPool.Terminate;
var
i: Integer;
begin
for i := 0 to FPool.Count - 1 do
if assigned(FPool[i]) then
TSortedCustomThread(FPool[i]).Terminate;
end;
{ TSortedCustomThread }
procedure TSortedCustomThread.DoOnRebornJob(j: TLinearJob);
begin
j.NeedToRestart := false;
if j is TSortedJob then
begin
TSortedJob(j).UpdateScore;
FOwner.AddSorted(TSortedJob(j));
end else
FOwner.AddLinear(j);
end;
procedure TSortedCustomThread.SendForWaiting(j: TLinearJob);
begin
FWaitedJobs.AddJob(j, j.HoldOnValueMs);
end;
function TSortedCustomThread.GetRunning: Boolean;
begin
if not assigned(FRunning) then Result := false else
Result := FRunning.Value;
end;
function TSortedCustomThread.GetSleepTime: TJobToJobWait;
begin
Result := FSleepTime.Value;
end;
function TSortedCustomThread.GetThreadKind: TPoolThreadKind;
begin
Result := FThreadKind.Value;
end;
procedure TSortedCustomThread.SetSleepTime(AValue: TJobToJobWait);
begin
FSleepTime.Value := AValue;
end;
procedure TSortedCustomThread.SetThreadKind(AValue: TPoolThreadKind);
begin
FThreadKind.Value := AValue;
end;
procedure TSortedCustomThread.Execute;
var
j: TLinearJob;
TS : QWord;
s, k : boolean;
FCurSleepTime : Integer;
begin
FCurSleepTime := FSleepTime.Value.DefaultValue;
s := false;
while not Terminated do
begin
if FOwner.Running then
begin
TS := GetTickCount64;
j := nil;
for k := false to true do
if not Assigned(j) then
begin
if (s = k) then
begin
FWaitedJobs.Refresh(TS);
end
else
begin
case ThreadKind of
ptkLinear: j := FOwner.GetLinearJob(TS);
ptkSorted: j := FOwner.GetSortedJob(TS);
else
j := nil;
end;
end;
end;
s := not s;
if Assigned(j) then
begin
FRunning.Value := True;
try
myJob := j;
try
j.Execute;
except
//Raise;
end;
if j.NeedToRestart then
begin
SendForWaiting(j);
j := nil;
end;
finally
FRunning.Value := False;
myJob := nil;
if assigned(j) then j.Free;
end;
FSleepTime.AdaptDropToMin(FCurSleepTime);
end else
FSleepTime.AdaptToInc(FCurSleepTime);
end;
Sleep(FCurSleepTime);
end;
end;
constructor TSortedCustomThread.Create(AOwner: TSortedThreadPool;
AKind: TPoolThreadKind; aSleepTime: TJobToJobWait);
begin
FWaitedJobs := TWaitingPool.Create;
FWaitedJobs.OnRebornJob := @DoOnRebornJob;
FOwner := AOwner;
FRunning := TThreadBoolean.Create(False);
FSleepTime:= TThreadJobToJobWait.Create(aSleepTime);
FThreadKind := TThreadPoolThreadKind.Create(AKind);
inherited Create(False);
FreeOnTerminate := true;
end;
destructor TSortedCustomThread.Destroy;
begin
FOwner.ExcludeThread(Self);
FreeAndNil(FRunning);
FreeAndNil(FThreadKind);
FreeAndNil(FSleepTime);
FreeAndNil(FWaitedJobs);
inherited Destroy;
end;
end.