-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkcThreadPool.pas
348 lines (302 loc) · 6.93 KB
/
kcThreadPool.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
{ Thread Pool
Copyright (C) 2011 Maciej Kaczkowski / keit.co
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
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. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit kcThreadPool;
{$mode objfpc}{$H+}
interface
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, syncobjs;
type
TParallelProc = procedure of object;
TThreadPool = class;
{ TJob }
TJob = class
private
FParallelProc: TParallelProc;
FOwner: TThreadPool;
FName: string;
public
procedure Execute; virtual;
property ParallelProc: TParallelProc read FParallelProc write FParallelProc;
property Owner: TThreadPool read FOwner write FOwner;
property Name: string read FName write FName;
end;
{ TCustomThread }
TCustomThread = class(TThread)
private
FJobName: string;
FOwner: TThreadPool;
FRunning: Boolean;
public
procedure Execute; override;
constructor Create(AOwner: TThreadPool);
destructor Destroy; override;
property Running: Boolean read FRunning;
property JobName: string read FJobName write FJobName;
end;
{ TThreadPool }
TThreadPool = class
private
FPool: TStrings;
FList: TStrings;
FCS: TCriticalSection;
FRunning: Boolean;
function GetJob: TJob;
function GetJobCount(Index: string): Integer;
function GetJobExists(Index: string): Boolean;
function GetJobsCount: Integer;
function GetRunningThreads: Integer;
public
class procedure Init;
constructor Create(FThreads: Integer = 5);
destructor Destroy; override;
procedure Add(AJob: TParallelProc; AName: string = ''); overload;
procedure Add(AJob: TJob); overload;
procedure Execute;
procedure Terminate;
property Running: Boolean read FRunning write FRunning;
property CriticalSection: TCriticalSection read FCS;
property JobCount[Index: string]: Integer read GetJobCount;
property JobExists[Index: string]: Boolean read GetJobExists;
property JobsCount: Integer read GetJobsCount;
end;
var
ThreadPool: TThreadPool;
implementation
const
SLEEP_TIME = 30;
{ TJob }
procedure TJob.Execute;
begin
if Assigned(FParallelProc) then
FParallelProc;
end;
{ TThreadPool }
function TThreadPool.GetJob: TJob;
begin
FCS.Enter;
try
if FList.Count > 0 then
begin
Result := TJob(FList.Objects[0]);
FList.Delete(0);
end
else
Result := nil;
finally
FCS.Leave;
end;
end;
function TThreadPool.GetJobCount(Index: string): Integer;
var
i: Integer;
begin
FCS.Enter;
try
Result := 0;
for i := 0 to FList.Count - 1 do
if Index = FList[i] then
Result := Result + 1;
finally
FCS.Leave;
end;
end;
function TThreadPool.GetJobExists(Index: string): Boolean;
var
i: Integer;
begin
FCS.Enter;
try
Result := False;
if not Result then
begin
for i := 0 to FPool.Count - 1 do
begin
if Index = TCustomThread(FPool.Objects[i]).JobName then
begin
Result := True;
Break;
end;
end;
end;
if not Result then
begin
for i := 0 to FList.Count - 1 do
begin
if Index = FList[i] then
begin
Result := True;
Break;
end;
end;
end;
finally
FCS.Leave;
end;
end;
function TThreadPool.GetJobsCount: Integer;
begin
Result := FList.Count;
end;
function TThreadPool.GetRunningThreads: Integer;
var
i: Integer;
begin
FCS.Enter;
Result := 0;
for i := 0 to FPool.Count - 1 do
if TCustomThread(FPool.Objects[i]).Running then
Inc(Result);
FCS.Leave;
end;
class procedure TThreadPool.Init;
begin
if not Assigned(ThreadPool) then
ThreadPool := TThreadPool.Create;
end;
constructor TThreadPool.Create(FThreads: Integer = 5);
var
i: Integer;
begin
FPool := TStringList.Create;
FCS := TCriticalSection.Create;
FList := TStringList.Create;
FCS.Enter;
try
for i := 1 to FThreads do
FPool.AddObject('', TCustomThread.Create(Self));
finally
FCS.Leave;
end;
end;
destructor TThreadPool.Destroy;
var
i: Integer;
c: TCustomThread;
o: TObject;
begin
for i := 0 to FPool.Count - 1 do
begin
c := TCustomThread(FPool.Objects[i]);
FPool.Objects[i] := nil;
c.Terminate;
c.Free;
end;
FPool.Free;
for i := 0 to FList.Count - 1 do
begin
o := FList.Objects[i];
if Assigned(o) then
o.Free;
end;
FList.Free;
FCS.Free;
inherited Destroy;
end;
procedure TThreadPool.Add(AJob: TParallelProc; AName: string);
var
j: TJob;
begin
j := TJob.Create;
j.ParallelProc := AJob;
j.Owner := Self;
j.Name := AName;
FCS.Enter;
try
FList.AddObject(AName, j);
finally
FCS.Leave;
end;
end;
procedure TThreadPool.Add(AJob: TJob);
begin
AJob.Owner := Self;
FCS.Enter;
try
FList.AddObject(AJob.Name, AJob);
finally
FCS.Leave;
end;
end;
procedure TThreadPool.Execute;
var
i: Integer;
begin
Running := True;
try
while (FList.Count > 0) do
Sleep(SLEEP_TIME);
i := 0;
while GetRunningThreads > 0 do
begin
Sleep(SLEEP_TIME);
Inc(i);
if i = 500 then
Break;
end;
finally
Running := False;
end;
end;
procedure TThreadPool.Terminate;
var
i: Integer;
begin
for i := 0 to FPool.Count - 1 do
TCustomThread(FPool.Objects[i]).Terminate;
end;
{ TCustomThead }
procedure TCustomThread.Execute;
var
j: TJob;
begin
while not Terminated do
begin
if FOwner.Running then
begin
j := FOwner.GetJob;
if Assigned(j) then
begin
FJobName := j.Name;
FRunning := True;
try
j.Execute;
finally
FJobName := '';
FRunning := False;
j.Free;
end;
end
else
FRunning := False;
end;
Sleep(SLEEP_TIME);
end;
end;
constructor TCustomThread.Create(AOwner: TThreadPool);
begin
FOwner := AOwner;
inherited Create(False);
end;
destructor TCustomThread.Destroy;
begin
inherited Destroy;
end;
finalization
if Assigned(ThreadPool) then
ThreadPool.Free;
end.