-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadJobs.pas
163 lines (144 loc) · 3.4 KB
/
ThreadJobs.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
unit ThreadJobs;
interface
uses Classes,SyncObjs,Generics.Collections;
type
TThreadJob=class
Job : TThreadProcedure;
constructor Create(J : TThreadProcedure);
end;
TJobThread = class;
TJobThreadList = TObjectList<TJobThread>;
TThreadJobQueue = class(TObjectQueue<TThreadJob>)
WakeUp,AllPause : TEvent;
Lock : TMutex;
ThreadList : TJobThreadList;
RunningThread : integer;
clearing : boolean;
needpause : boolean;
pauseLock : TMutex;
procedure Run(J : TThreadProcedure);
constructor Create(ThreadCount : integer);
destructor Destroy; override;
procedure ClearJobs;
procedure PauseAll;
procedure ResumeAll;
end;
TJobThread = class(TThread)
JQ : TThreadJobQueue;
procedure Execute; override;
constructor Create(Q : TThreadJobQueue);
end;
implementation
uses SysUtils;
{ TThreadJobQueue }
procedure TThreadJobQueue.ClearJobs;
begin
clearing := true;
Lock.Acquire;
Clear;
Lock.Release;
AllPause.WaitFor;
clearing := false;
end;
constructor TThreadJobQueue.Create(ThreadCount : integer);
var i : integer;
begin
inherited Create;
clearing := false;
AllPause := TEvent.Create;
WakeUp := TEvent.Create;
Lock := TMutex.Create;
ThreadList := TJobThreadList.Create;
for i := 1 to ThreadCount do begin
ThreadList.Add(TJobThread.Create(self));
end;
end;
destructor TThreadJobQueue.Destroy;
var J : TJobThread;
begin
for J in ThreadList do
begin
J.Terminate;
end;
Lock.Acquire;
WakeUp.SetEvent;
Lock.Release;
for J in ThreadList do
begin
J.WaitFor;
end;
ThreadList.Free;
WakeUp.Free;
Lock.Free;
AllPause.Free;
inherited;
end;
procedure TThreadJobQueue.PauseAll;
begin
PauseLock.Acquire;
NeedPause := true;
AllPause.WaitFor;
PauseLock.Release;
end;
procedure TThreadJobQueue.ResumeAll;
begin
PauseLock.Acquire;
NeedPause := false;
PauseLock.Release;
WakeUp.SetEvent;
end;
procedure TThreadJobQueue.Run(J: TThreadProcedure);
begin
if clearing then exit;
Lock.Acquire;
if not clearing then begin
Enqueue(TThreadJob.Create(J));
if Count=1 then WakeUp.SetEvent;
end;
Lock.Release;
end;
{ TJobThread }
constructor TJobThread.Create(Q: TThreadJobQueue);
begin
JQ := Q;
inherited Create;
end;
procedure TJobThread.Execute;
var J : TThreadJob;
begin
if TInterlocked.Increment(JQ.RunningThread)=1 then JQ.AllPause.ResetEvent;
try
while not Terminated do begin
J :=nil;
if not JQ.needpause then begin
JQ.Lock.Acquire;
try
if not(Terminated or JQ.needpause) then
if JQ.Count=0 then JQ.WakeUp.ResetEvent
else J := JQ.Extract;
finally
JQ.Lock.Release;
end;
end;
if assigned(J) then begin
try
J.Job();
except on E: Exception do
end;
J.Free;
end else if not Terminated then begin
if TInterLocked.Decrement(JQ.RunningThread)=0 then JQ.AllPause.SetEvent;
JQ.WakeUp.WaitFor;
if TInterlocked.Increment(JQ.RunningThread)=1 then JQ.AllPause.ResetEvent;
end;
end;
finally
if TInterLocked.Decrement(JQ.RunningThread)=0 then JQ.AllPause.SetEvent;
end;
end;
{ TThreadJob }
constructor TThreadJob.Create(J: TThreadProcedure);
begin
Job := J;
end;
end.