-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchedThreads.pas
More file actions
346 lines (298 loc) · 10.1 KB
/
SynchedThreads.pas
File metadata and controls
346 lines (298 loc) · 10.1 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
unit SynchedThreads;
// Copyright © 1998 by Jon Shemitz, all rights reserved.
// Permission is hereby granted to freely use, modify, and
// distribute this source code PROVIDED that all six lines of
// this copyright and contact notice are included without any
// changes. Questions? Comments? Offers of work?
// mailto:jon@midnightbeach.com - http://www.midnightbeach.com
// Various modifications to Jon Shemitz original code by
// David Knaack (davidknaack@gmail.com). Mods are not intended
// to be nor should they be construed as being inteded to be
// well designed. It is more or less functional though, and
// the updates for D2009 allow for some interesting uses.
{$T+} {$hints on} {$warnings on}
interface
uses Windows, Classes, SysUtils, Forms;
// Simple threads
type
TThreadMethod = procedure (Data: pointer) of object;
{$ifdef VER200}
TSyncThreadProcedure = reference to procedure(Data: Pointer);
{$endif}
TSimpleThread = class (TThread)
protected
ThreadMethod: TThreadMethod;
{$ifdef VER200}
AnonMethod: TSyncThreadProcedure;
{$endif}
Data: pointer;
procedure Execute; override;
public
{$ifdef VER200}
constructor CreateSimple( CreateSuspended: boolean;
_Action: TThreadMethod; _Data: pointer ); overload;
constructor CreateSimple( CreateSuspended: boolean;
_Action: TSyncThreadProcedure; _Data: pointer ); overload;
{$else}
constructor CreateSimple( _Action: TThreadMethod;
_CreateSuspended: boolean;
_Data: pointer ); overload;
constructor CreateSimple( _Action: TThreadMethod;
_CreateSuspended: boolean=False;
_FreeOnTerminate : Boolean=True;
_OnTerminate: TNotifyEvent=nil;
_Data: pointer=nil ); overload;
constructor CreateSimple( _Action: TThreadMethod;
_CreateSuspended: boolean=False;
_FreeOnTerminate : Boolean=True;
_Data: pointer=nil ); overload;
{$endif}
procedure AbortThread;
end;
{$ifdef VER200}
function RunInThread( Handler: TThreadMethod; Data: pointer ): TSimpleThread; overload;
function RunInThread( AnonMeth: TSyncThreadProcedure; Data: pointer; CreateSuspended:Boolean=false ): TSimpleThread; overload;
{$else}
function RunInThread( Handler: TThreadMethod;
CreateSuspended: Boolean=False;
FreeOnTerminate: Boolean=False;
Data: pointer=nil ): TSimpleThread; overload;
function RunInThread( Handler: TThreadMethod;
CreateSuspended: Boolean=False;
FreeOnTerminate: Boolean=false;
OnTerminate: TNotifyEvent=nil;
Data:pointer=nil ): TSimpleThread; overload;
{$endif}
// Wait threads (basic synchronization)
procedure MsgWaitForSingleObject(Handle: THandle);
function SpawnProcess(const Command: string): TProcessInformation;
type
TWaitThread = class (TSimpleThread)
private
AbortFlag: ^boolean;
procedure Run(MsgWait: boolean);
public
constructor CreateWait( _Action: TThreadMethod; _Data: pointer );
procedure WaitFor;
procedure MsgWaitFor;
procedure AbortThread;
end;
procedure WaitForThread(Handler: TThreadMethod; Data: pointer );//Blocking
procedure MsgWaitForThread(var Thread: TWaitThread; //non-Blocking
Handler: TThreadMethod; Data: pointer );
// Stop/start threads
type
EAbortedThread = class(Exception);
EThreadInUse = class(Exception);
TStopStartThread = class (TSimpleThread)
private
Event: THandle;
Aborted: boolean;
procedure Run( _Action: TThreadMethod; _Data:pointer; MsgWait: boolean );
protected
procedure Execute; override;
public
Waiting: boolean;
constructor Create;
destructor Destroy; override;
procedure WaitFor ( _Action: TThreadMethod; _Data:pointer );
procedure MsgWaitFor( _Action: TThreadMethod; _Data:pointer );
procedure AbortThread;
end;
implementation
{ ============== TSimpleThread ============= }
function RunInThread( Handler: TThreadMethod;
CreateSuspended: Boolean=False;
FreeOnTerminate: Boolean=false;
Data:pointer=nil): TSimpleThread;
begin
Result := TSimpleThread.CreateSimple(Handler, CreateSuspended, FreeOnTerminate, Data);
end;
function RunInThread( Handler: TThreadMethod;
CreateSuspended: Boolean=False;
FreeOnTerminate: Boolean=false;
OnTerminate: TNotifyEvent=nil;
Data:pointer=nil ): TSimpleThread;
begin
Result := TSimpleThread.CreateSimple(Handler, CreateSuspended, FreeOnTerminate, OnTerminate, Data );
end;
{$ifdef VER200}
function RunInThread( AnonMeth: TSyncThreadProcedure; Data: pointer; CreateSuspended:Boolean=false): TSimpleThread;
begin
Result := TSimpleThread.CreateSimple(CreateSuspended, AnonMeth, Data);
end;
{$endif}
constructor TSimpleThread.CreateSimple( _Action: TThreadMethod;
_CreateSuspended: boolean=False;
_FreeOnTerminate: Boolean=True;
_Data: pointer=nil );
begin
ThreadMethod := _Action; // Set these BEFORE calling
Data := _Data; // inherited Create()!
FreeOnTerminate := _FreeOnTerminate;
inherited Create(_CreateSuspended);
end;
constructor TSimpleThread.CreateSimple( _Action: TThreadMethod;
_CreateSuspended: boolean;
_Data: pointer );
begin
ThreadMethod := _Action;
Data := _Data;
inherited Create(_CreateSuspended);
end;
constructor TSimpleThread.CreateSimple( _Action: TThreadMethod;
_CreateSuspended: boolean=False;
_FreeOnTerminate: Boolean=True;
_OnTerminate: TNotifyEvent=nil;
_Data: pointer=nil );
begin
ThreadMethod := _Action; // Set these BEFORE calling
Data := _Data; // inherited Create()!
OnTerminate := _OnTerminate;
FreeOnTerminate := _FreeOnTerminate;
inherited Create(_CreateSuspended);
end;
{$ifdef VER200}
constructor TSimpleThread.CreateSimple(CreateSuspended: boolean;
_Action: TSyncThreadProcedure; _Data: pointer );
begin
ThreadMethod := nil;
AnonMethod := _Action;
Data := _Data;
FreeOnTerminate := true;
inherited Create(CreateSuspended);
end;
{$endif}
procedure TSimpleThread.Execute;
begin //Call the procedure pointed to by ThreadMethod
{$ifdef VER200}
if Assigned(ThreadMethod) then
ThreadMethod(Data)
else
if Assigned(AnonMethod) then
AnonMethod(Data);
{$else}
ThreadMethod(Data);
{$endif}
end;
procedure TSimpleThread.AbortThread;
begin
Suspend; // Can't kill a running thread by Freeing it
Free; // Kills thread
end;
{ ============== Basic synchronization ============= }
procedure MsgWaitForSingleObject(Handle: THandle);
begin
repeat
if MsgWaitForMultipleObjects( 1, Handle, False, INFINITE, QS_ALLINPUT)
= WAIT_OBJECT_0 + 1
then Application.ProcessMessages
else BREAK;
until True = False;
end;
function SpawnProcess(const Command: string): TProcessInformation;
var StartupInfo: TStartupInfo;
begin
FillChar(StartupInfo, SizeOf(StartupInfo), 0); // use defaults
StartupInfo.cb := SizeOf(StartupInfo);
CreateProcess( Nil, PChar(Command), Nil, Nil, False, 0, Nil,
Nil, StartupInfo, Result );
end;
{ ============== TWaitThread ============= }
procedure WaitForThread( Handler: TThreadMethod; Data: pointer );
begin
TWaitThread.CreateWait(Handler, Data).WaitFor;
end;
procedure MsgWaitForThread( var Thread: TWaitThread;
Handler: TThreadMethod; Data: pointer );
begin
Thread := TWaitThread.CreateWait(Handler, Data);
Thread.MsgWaitFor; //
Thread := Nil;
end;
constructor TWaitThread.CreateWait( _Action: TThreadMethod; _Data: pointer );
begin
CreateSimple(_Action, True, _Data); // CreateSuspended
AbortFlag := Nil;
end;
procedure TWaitThread.WaitFor;
begin
Run(False);
end;
procedure TWaitThread.MsgWaitFor;
begin
Run(True);
end;
procedure TWaitThread.Run(MsgWait: boolean);
var
Aborted: boolean;
begin
AbortFlag := @ Aborted;
Aborted := False;
Resume;
if MsgWait
then MsgWaitForSingleObject(Handle)
else inherited WaitFor;
if Aborted then Abort;
end;
procedure TWaitThread.AbortThread;
begin
Assert(Assigned(AbortFlag));
AbortFlag^ := True;
inherited;
end;
{ ============== TStopStartThread ============= }
constructor TStopStartThread.Create;
begin
// CreateEvent API call is smaller and simpler than Delphi wrapper
Event := CreateEvent(Nil, True, False, Nil);
Assert(Event <> 0);
Waiting := False;
Aborted := False;
inherited Create(True); // Create a suspended thread
end;
destructor TStopStartThread.Destroy;
begin
CloseHandle(Event);
inherited;
end;
procedure TStopStartThread.Execute;
begin
while not Terminated do
if Assigned(ThreadMethod) then begin
ThreadMethod(Data); //Do the work
SetEvent(Event);
Suspend;
end;
end;
procedure TStopStartThread.Run( _Action: TThreadMethod; _Data:pointer;
MsgWait: boolean );
begin
if Waiting then raise EThreadInUse.Create('Thread in use');
if Aborted then raise EAbortedThread.Create('Aborted thread');
ThreadMethod := _Action;
Data := _Data;
Waiting := True;
ResetEvent(Event);
Resume; //Starts Execute again
if MsgWait
then MsgWaitForSingleObject(Event)
else WaitForSingleObject (Event, INFINITE);
Waiting := False;
if Aborted then Abort; // Raise an EAbort exception
end;
procedure TStopStartThread.MsgWaitFor( _Action: TThreadMethod; _Data:pointer );
begin
Run(_Action, _Data, True);
end;
procedure TStopStartThread.WaitFor( _Action: TThreadMethod; _Data:pointer );
begin
Run(_Action, _Data, False);
end;
procedure TStopStartThread.AbortThread;
begin
Suspend; // Can't kill a running thread by Freeing it
Aborted := True;
SetEvent(Event);
end;
end.