-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitThreadManager.pas
198 lines (161 loc) · 4.26 KB
/
UnitThreadManager.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
unit UnitThreadManager;
interface
uses
Windows, SysUtils, Classes, SyncObjs,
Common;
type
TThreadManager = class;
TProtoThread = class;
TOnFinishEvent = procedure(const TProtoThread: TProtoThread);
TProtoThread = class(TThread)
private
FIndex: integer;
FBlockIndex: integer;
FResultFileName: string;
FManager: TThreadManager;
public
constructor Create(const BlockIndex: integer);
destructor Destroy; override;
procedure Execute; override;
property Index: integer
read FIndex;
property BlockIndex: integer
read FBlockIndex;
property ResultFileName: string
read FResultFileName write FResultFileName;
end;
TThreadManager = class
private
FPoolSize: integer;
FPool: array of TProtoThread;
FFinishEvents: array of TEvent;
FRunning: integer;
FRunningCS: TCriticalSection;
const
WAIT_FOR_ALL = true;
WAIT_FOR_ANY = false;
// æäåò íàñòóïëåíèÿ èëè îäíîãî èç èëè âñåõ FinishEvents.
//  ïåðâîì ñëó÷àå âîçâðàùàåò èíäåêñ ñîáûòèÿ
function WaitForEvents(const WaitForAll: boolean): integer;
procedure OnThreadTerminate(Sender: TProtoThread);
public
constructor Create(const MaxPoolSize: integer);
destructor Destroy; override;
procedure Run(const Thread: TProtoThread);
procedure WaitAllThreads;
function ThreadByBlockIndex(const BlockIndex: integer): TProtoThread;
property Running: integer
read FRunning;
end;
var
ThreadManager: TThreadManager;
implementation
var
ThreadIndex: integer;
{ TProtoThread }
constructor TProtoThread.Create(const BlockIndex: integer);
begin
inherited Create(true);
FreeOnTerminate := true;
FManager := nil;
FBlockIndex := BlockIndex;
FIndex := InterlockedIncrement(ThreadIndex);
Log(ClassName + ' created ' + IntToStr(FIndex));
end;
destructor TProtoThread.Destroy;
begin
Log(ClassName + ' destroyed ' + IntToStr(FIndex));
inherited;
end;
procedure TProtoThread.Execute;
begin
Log('Thread finished ' + IntToStr(FIndex));
if Assigned(FManager) then
FManager.OnThreadTerminate(Self);
end;
{ TThreadManager }
constructor TThreadManager.Create(const MaxPoolSize: integer);
var
i: Integer;
begin
FPoolSize := MaxPoolSize;
SetLength(FPool, FPoolSize);
SetLength(FFinishEvents, FPoolSize);
for i := 0 to FPoolSize - 1 do
FFinishEvents[i] := TEvent.Create(nil, true, true, '');
FRunning := 0;
FRunningCS := TCriticalSection.Create;
end;
destructor TThreadManager.Destroy;
var
i: Integer;
begin
// Äîæäåìñÿ îêîí÷àíèÿ âñåõ çàïóùåííûõ íà òåêóùèé ìîìåíò ïîòîêîâ
WaitForEvents(WAIT_FOR_ALL);
for i := 0 to FPoolSize - 1 do
FFinishEvents[i].Free;
FRunningCS.Free;
inherited;
end;
procedure TThreadManager.OnThreadTerminate(Sender: TProtoThread);
var
i: integer;
begin
for i := 0 to FPoolSize - 1 do
if FPool[i] = Sender then
begin
FPool[i] := nil;
FFinishEvents[i].SetEvent;
InterlockedDecrement(FRunning);
Break;
end;
end;
procedure TThreadManager.Run(const Thread: TProtoThread);
var
FreeSlotIndex: integer;
begin
FRunningCS.Enter;
try
// åñëè êîëè÷åñòâî çàïóùåíûõ ïîòîêîâ ìàêñèìàëüíî - äîæäåìñÿ îñâîáîæäåíèÿ êàêîãî-íèáóäü ñëîòà
FreeSlotIndex := WaitForEvents(WAIT_FOR_ANY);
FPool[FreeSlotIndex] := Thread;
FFinishEvents[FreeSlotIndex].ResetEvent;
// ïî ñâîåìó îêîí÷àíèþ ïîòîê âûçîâåò ñîáûòèå OnThreadTerminate ìåíåäæåðà
Thread.FManager := Self;
InterlockedIncrement(FRunning);
Thread.Resume;
finally
FRunningCS.Leave;
end;
end;
function TThreadManager.ThreadByBlockIndex(const BlockIndex: integer): TProtoThread;
var
i: integer;
begin
Result := nil;
for i := 0 to FPoolSize - 1 do
if (FPool[i] <> nil) and (FPool[i].BlockIndex = BlockIndex) then
begin
Result := FPool[i];
Break;
end;
end;
procedure TThreadManager.WaitAllThreads;
begin
WaitForEvents(WAIT_FOR_ALL);
end;
function TThreadManager.WaitForEvents(const WaitForAll: boolean): integer;
var
H: array of THandle;
i: Integer;
R: DWORD;
begin
Result := -1;
SetLength(H, FPoolSize);
for i := 0 to FPoolSize - 1 do
H[i] := FFinishEvents[i].Handle;
R := WaitForMultipleObjects(FPoolSize, @H[0], WaitForAll, INFINITE);
if WaitForAll = WAIT_FOR_ANY then
Result := R - WAIT_OBJECT_0;
end;
end.