-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathThreadPool.Simple.pas
More file actions
480 lines (409 loc) · 11.4 KB
/
Copy pathThreadPool.Simple.pas
File metadata and controls
480 lines (409 loc) · 11.4 KB
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
unit ThreadPool.Simple;
{$mode objfpc}{$H+}{$J-}
interface
uses
Classes, SysUtils, SyncObjs, ThreadPool.Types;
type
{$REGION 'Internal: Work Item'}
{ Simple work item implementation }
TSimpleWorkItem = class(TInterfacedObject, IWorkItem)
private
FProcedure: TThreadProcedure;
FMethod: TThreadMethod;
FProcedureIndex: TThreadProcedureIndex;
FMethodIndex: TThreadMethodIndex;
FIndex: Integer;
FItemType: TWorkItemType;
FThreadPool: TObject;
public
constructor Create(AThreadPool: TObject);
destructor Destroy; override;
{ IWorkItem implementation }
procedure Execute;
function GetItemType: Integer;
end;
{$ENDREGION}
{$REGION 'Internal: Worker Thread'}
{ Simple worker thread implementation }
TSimpleWorkerThread = class(TThread, IWorkerThread)
private
FThreadPool: TObject;
protected
procedure Execute; override;
public
constructor Create(AThreadPool: TObject);
destructor Destroy; override;
{ IWorkerThread implementation }
procedure Start;
procedure Terminate;
procedure WaitFor;
function GetThreadID: TThreadID;
function QueryInterface(constref IID: TGUID; out Obj): HResult; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
function _AddRef: Integer; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
function _Release: Integer; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
end;
{$ENDREGION}
{$REGION 'Public API: TSimpleThreadPool'}
{ Simple thread pool implementation }
TSimpleThreadPool = class(TThreadPoolBase)
private
FThreads: TThreadList;
FWorkItems: TThreadList;
FWorkItemLock: TCriticalSection;
FWorkItemCount: Integer;
FWorkItemEvent: TEvent;
FErrorLock: TCriticalSection;
FErrorEvent: TEvent;
procedure ClearThreads;
procedure ClearWorkItems;
public
constructor Create(AThreadCount: Integer = 0); override;
destructor Destroy; override;
{ IThreadPool implementation }
procedure Queue(AProcedure: TThreadProcedure); override;
procedure Queue(AMethod: TThreadMethod); override;
procedure Queue(AProcedure: TThreadProcedureIndex; AIndex: Integer); override;
procedure Queue(AMethod: TThreadMethodIndex; AIndex: Integer); override;
procedure WaitForAll; override;
function GetThreadCount: Integer; override;
function GetLastError: string; override;
property ThreadCount: Integer read GetThreadCount;
property LastError: string read GetLastError;
end;
{$ENDREGION}
var
{$REGION 'Public API: Global instance'}
{ Global thread pool instance.
Created automatically at unit initialization; freed at finalization.
Do NOT call GlobalThreadPool.Free — the unit manages its lifetime. }
GlobalThreadPool: TSimpleThreadPool;
{$ENDREGION}
implementation
{$REGION 'TSimpleWorkerThread'}
{ TSimpleWorkerThread }
constructor TSimpleWorkerThread.Create(AThreadPool: TObject);
begin
inherited Create(True); // Create suspended
FThreadPool := AThreadPool;
FreeOnTerminate := False;
end;
destructor TSimpleWorkerThread.Destroy;
begin
inherited;
end;
procedure TSimpleWorkerThread.Start;
begin
inherited Start;
end;
procedure TSimpleWorkerThread.Terminate;
begin
inherited Terminate;
end;
procedure TSimpleWorkerThread.WaitFor;
begin
inherited WaitFor;
end;
function TSimpleWorkerThread.GetThreadID: TThreadID;
begin
Result := ThreadID;
end;
function TSimpleWorkerThread.QueryInterface(constref IID: TGUID; out Obj): HResult; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
begin
if GetInterface(IID, Obj) then
Result := S_OK
else
Result := E_NOINTERFACE;
end;
function TSimpleWorkerThread._AddRef: Integer; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
begin
// The pool owns this thread's lifetime via FThreads/ClearThreads, so the
// IWorkerThread interface must NOT reference-count. Returning -1 marks this
// as a non-ref-counted interface (the same contract TComponent uses), which
// prevents an interface assignment from freeing the still-live worker.
Result := -1;
end;
function TSimpleWorkerThread._Release: Integer; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
begin
// See _AddRef: lifetime is owned by the pool, never by interface refcount.
Result := -1;
end;
procedure TSimpleWorkerThread.Execute;
var
Pool: TSimpleThreadPool;
List: TList;
WorkItem: TSimpleWorkItem;
begin
Pool := TSimpleThreadPool(FThreadPool);
while not Terminated do
begin
// Try to get a work item
List := Pool.FWorkItems.LockList;
try
if List.Count > 0 then
begin
WorkItem := TSimpleWorkItem(List[0]);
List.Delete(0);
end
else
WorkItem := nil;
finally
Pool.FWorkItems.UnlockList;
end;
// Process work item if we got one
if Assigned(WorkItem) then
begin
try
WorkItem.Execute;
except
on E: Exception do
begin
// Capture error
Pool.FErrorLock.Enter;
try
Pool.SetLastError(E.Message);
Pool.FErrorEvent.SetEvent;
finally
Pool.FErrorLock.Leave;
end;
end;
end;
WorkItem.Free;
end
else
Sleep(1); // Prevent busy waiting
end;
end;
{$ENDREGION}
{$REGION 'TSimpleWorkItem'}
{ TSimpleWorkItem }
constructor TSimpleWorkItem.Create(AThreadPool: TObject);
begin
inherited Create;
FThreadPool := AThreadPool;
FItemType := witProcedure;
FIndex := 0;
end;
destructor TSimpleWorkItem.Destroy;
begin
inherited;
end;
procedure TSimpleWorkItem.Execute;
var
Pool: TSimpleThreadPool;
begin
try
// Execute the appropriate work item based on its type
case FItemType of
witProcedure: if Assigned(FProcedure) then FProcedure;
witMethod: if Assigned(FMethod) then FMethod;
witProcedureIndex: if Assigned(FProcedureIndex) then FProcedureIndex(FIndex);
witMethodIndex: if Assigned(FMethodIndex) then FMethodIndex(FIndex);
end;
finally
// Update work item count and signal completion if necessary
Pool := TSimpleThreadPool(FThreadPool);
Pool.FWorkItemLock.Enter;
try
Dec(Pool.FWorkItemCount);
if Pool.FWorkItemCount = 0 then
Pool.FWorkItemEvent.SetEvent;
finally
Pool.FWorkItemLock.Leave;
end;
end;
end;
function TSimpleWorkItem.GetItemType: Integer;
begin
Result := Ord(FItemType);
end;
{$ENDREGION}
{$REGION 'TSimpleThreadPool — Public API'}
{ TSimpleThreadPool }
constructor TSimpleThreadPool.Create(AThreadCount: Integer = 0);
var
I: Integer;
Thread: TSimpleWorkerThread;
begin
inherited Create(AThreadCount);
// Initialize thread-safe collections and synchronization
FThreads := TThreadList.Create;
FWorkItems := TThreadList.Create;
FWorkItemLock := TCriticalSection.Create;
FErrorLock := TCriticalSection.Create;
FErrorEvent := TEvent.Create(nil, True, False, '');
FWorkItemEvent := TEvent.Create(nil, True, True, '');
FWorkItemCount := 0;
// Create and start worker threads
for I := 1 to FThreadCount do
begin
Thread := TSimpleWorkerThread.Create(Self);
FThreads.Add(Thread);
Thread.Start;
end;
end;
destructor TSimpleThreadPool.Destroy;
begin
// Only drain outstanding work if the pool was fully constructed. If the
// constructor failed partway, FPC still calls this destructor on the
// half-built object, so guard against the sync objects being nil.
if Assigned(FWorkItemEvent) and Assigned(FErrorEvent) then
WaitForAll; // Ensure all tasks complete before destroying
FShutdown := True;
if Assigned(FWorkItems) then
ClearWorkItems;
if Assigned(FThreads) then
ClearThreads;
// Clean up synchronization objects (each may be nil after a partial
// construction, so Free — which is nil-safe — is used throughout).
FWorkItemLock.Free;
FThreads.Free;
FWorkItems.Free;
FWorkItemEvent.Free;
FErrorEvent.Free;
FErrorLock.Free;
inherited Destroy;
end;
procedure TSimpleThreadPool.ClearThreads;
var
Thread: TSimpleWorkerThread;
List: TList;
I: Integer;
begin
// Signal all threads to terminate
List := FThreads.LockList;
try
for I := 0 to List.Count - 1 do
begin
Thread := TSimpleWorkerThread(List[I]);
Thread.Terminate;
end;
finally
FThreads.UnlockList;
end;
// Wait for all threads to finish and clean up
List := FThreads.LockList;
try
for I := 0 to List.Count - 1 do
begin
Thread := TSimpleWorkerThread(List[I]);
Thread.WaitFor;
Thread.Free;
end;
List.Clear;
finally
FThreads.UnlockList;
end;
end;
procedure TSimpleThreadPool.ClearWorkItems;
var
List: TList;
I: Integer;
begin
// Clean up any remaining work items
List := FWorkItems.LockList;
try
for I := 0 to List.Count - 1 do
TSimpleWorkItem(List[I]).Free;
List.Clear;
finally
FWorkItems.UnlockList;
end;
end;
{ Queue overloads for different types of work items }
procedure TSimpleThreadPool.Queue(AProcedure: TThreadProcedure);
var
WorkItem: TSimpleWorkItem;
begin
if FShutdown then Exit; // Don't queue if shutting down
// Update work item count
FWorkItemLock.Enter;
try
Inc(FWorkItemCount);
if FWorkItemCount > 0 then
FWorkItemEvent.ResetEvent; // Reset completion event
finally
FWorkItemLock.Leave;
end;
// Create and queue work item
WorkItem := TSimpleWorkItem.Create(Self);
WorkItem.FProcedure := AProcedure;
WorkItem.FItemType := witProcedure;
FWorkItems.Add(WorkItem);
end;
procedure TSimpleThreadPool.Queue(AMethod: TThreadMethod);
var
WorkItem: TSimpleWorkItem;
begin
if FShutdown then Exit;
FWorkItemLock.Enter;
try
Inc(FWorkItemCount);
if FWorkItemCount > 0 then
FWorkItemEvent.ResetEvent;
finally
FWorkItemLock.Leave;
end;
WorkItem := TSimpleWorkItem.Create(Self);
WorkItem.FMethod := AMethod;
WorkItem.FItemType := witMethod;
FWorkItems.Add(WorkItem);
end;
procedure TSimpleThreadPool.Queue(AProcedure: TThreadProcedureIndex; AIndex: Integer);
var
WorkItem: TSimpleWorkItem;
begin
if FShutdown then Exit;
FWorkItemLock.Enter;
try
Inc(FWorkItemCount);
if FWorkItemCount > 0 then
FWorkItemEvent.ResetEvent;
finally
FWorkItemLock.Leave;
end;
WorkItem := TSimpleWorkItem.Create(Self);
WorkItem.FProcedureIndex := AProcedure;
WorkItem.FIndex := AIndex;
WorkItem.FItemType := witProcedureIndex;
FWorkItems.Add(WorkItem);
end;
procedure TSimpleThreadPool.Queue(AMethod: TThreadMethodIndex; AIndex: Integer);
var
WorkItem: TSimpleWorkItem;
begin
if FShutdown then Exit;
FWorkItemLock.Enter;
try
Inc(FWorkItemCount);
if FWorkItemCount > 0 then
FWorkItemEvent.ResetEvent;
finally
FWorkItemLock.Leave;
end;
WorkItem := TSimpleWorkItem.Create(Self);
WorkItem.FMethodIndex := AMethod;
WorkItem.FIndex := AIndex;
WorkItem.FItemType := witMethodIndex;
FWorkItems.Add(WorkItem);
end;
procedure TSimpleThreadPool.WaitForAll;
begin
FWorkItemEvent.WaitFor(INFINITE); // Wait for all work items to complete
// If there was an error, ensure it's fully captured
if FErrorEvent.WaitFor(100) = wrSignaled then
FErrorEvent.ResetEvent;
end;
function TSimpleThreadPool.GetThreadCount: Integer;
begin
Result := inherited GetThreadCount;
end;
function TSimpleThreadPool.GetLastError: string;
begin
Result := inherited GetLastError;
end;
{$ENDREGION}
initialization
GlobalThreadPool := TSimpleThreadPool.Create; // Create global instance
finalization
GlobalThreadPool.Free; // Clean up global instance
end.