-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasync.pas
347 lines (299 loc) · 7.5 KB
/
async.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
unit Async;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils;
type
// Contract: call fun should not have anything to do with VCL/LCL, while Notify can.
TAsyncCallFun = procedure(Param: Pointer);
TAsyncCallNotify = procedure(Param: Pointer) of object;
TParamArray = array of Pointer;
PParamArray = ^TParamArray;
procedure AsyncCall(Fun: TAsyncCallFun; FunParam: Pointer;
Notify: TAsyncCallNotify; NotifyParam: Pointer);
procedure AsyncForEach(Fun: TAsyncCallFun; ParamArray: PParamArray;
Notify: TAsyncCallNotify; NotifyParam: Pointer; Cfg: array of Variant);
function PropListGet(Field: variant; Def: variant; Cfg: array of Variant): Variant;
function AtoI(const S: string): Integer;
function HexToInt(HexStr: string): DWord;
function ExcludeFileExt(FileName: string): string;
implementation
var
DefaultPoolSize: integer = 2;
type
{ TAsyncCallThread }
TAsyncCallThread = class(TThread)
private
FFun: TAsyncCallFun;
FFunParam: Pointer;
FNotify: TAsyncCallNotify;
FNotifyParam: Pointer;
procedure DoNotify;
public
constructor Create(Fun: TAsyncCallFun; FunParam: Pointer;
Notify: TAsyncCallNotify; NotifyParam: Pointer);
protected
procedure Execute; override;
end;
{ TAsyncWhileThread }
procedure TAsyncCallThread.DoNotify;
begin
FNotify(FNotifyParam);
end;
constructor TAsyncCallThread.Create(Fun: TAsyncCallFun; FunParam: Pointer;
Notify: TAsyncCallNotify; NotifyParam: Pointer);
begin
inherited Create(True);
FFun := Fun;
FFunParam := FunParam;
FNotify := Notify;
FNotifyParam := NotifyParam;
FreeOnTerminate := True;
Suspended := False;
end;
procedure TAsyncCallThread.Execute;
label
Quit;
begin
if @FFun = nil then
goto Quit;
try
FFun(FFunParam);
except
end;
Quit:
if Assigned(FNotify) then
Synchronize(DoNotify);
end;
function ExcludeFileExt(FileName: string): string;
var
i: longint;
EndSep: set of char;
begin
I := Length(FileName);
EndSep := AllowDirectorySeparators + AllowDriveSeparators + [ExtensionSeparator];
while (I > 0) and not (FileName[I] in EndSep) do
Dec(I);
if (I > 0) and (FileName[I] = ExtensionSeparator) then
Result := Copy(FileName, 1, I - 1)
else
Result := FileName;
end;
procedure AsyncCall(Fun: TAsyncCallFun; FunParam: Pointer;
Notify: TAsyncCallNotify; NotifyParam: Pointer);
begin
TAsyncCallThread.Create(Fun, FunParam, Notify, NotifyParam);
end;
type
// let me to write something portable
// RTLEventWaitFor could only wait for one object
// so, worker thread, when a worker done,
// 1) enter the cs,
// 2) set the WorkerReady event
// 3) wait for JobScheduled event
// 4) JobScheduled event then leave cs;
// scheduler thread
// 1) wait for the WorkerReady event, reset WorkerReady,
// 2) schedule jobs, set JobScheduled event for the worker
TAsyncWorker = record
JobScheduled: PRTLEvent;
I, J: Integer;
end;
PAsyncWorker = ^TAsyncWorker;
TAsyncSchedule = record
GroupSize: integer;
PoolSize: integer;
Fun: TAsyncCallFun;
ParamArray: PParamArray;
Total: integer;
CriticalSection: TRTLCriticalSection;
WorkerReady: PRTLEvent;
ReadyWorker: PAsyncWorker;
end;
PAsyncSchedule = ^TAsyncSchedule;
procedure AsyncWorker(Schedule: PAsyncSchedule);
var
W: TAsyncWorker;
Fun: TAsyncCallFun;
ParamArray: PParamArray;
K: integer;
begin
Fun := Schedule.Fun;
ParamArray := Schedule.ParamArray;
W.I := 0;
W.J := -1;
W.JobScheduled := RTLEventCreate;
repeat
EnterCriticalSection(Schedule^.CriticalSection);
Schedule.ReadyWorker := @W;
RTLEventSetEvent(Schedule^.WorkerReady);
RTLEventWaitFor(W.JobScheduled);
RTLeventResetEvent(W.JobScheduled);
LeaveCriticalSection(Schedule.CriticalSection);
for K := W.I to W.J do
try
Fun(ParamArray[K]);
except
end;
until (W.I > W.J);
RTLEventDestroy(W.JobScheduled);
end;
procedure AsyncSchedule(Schedule: PAsyncSchedule);
var
I: integer;
Active: Integer;
Total: integer;
Group: Integer;
Flags: array of Boolean;
Start: integer;
procedure DistributeJobs;
var
K, L: Integer;
W: PAsyncWorker;
begin
W := Schedule.ReadyWorker;
for K := W.I to W.J do
Flags[K] := True;
K := Start;
while (K < Total) and Flags[K] do Inc(K);
if K >= Total then
begin
Dec(Active);
W.I := 0;
W.J := -1;
RTLeventSetEvent(W.JobScheduled);
Exit;
end;
W.I := K;
L := 1;
while (K < Total) and (not Flags[K]) and (L < Group) do
begin
Inc(K);
Inc(L);
end;
if (K >= Total) or Flags[K] then Dec(K);
W.J := K;
Start := K + 1;
RTLeventSetEvent(W.JobScheduled);
Exit;
end;
begin
InitCriticalSection(Schedule.CriticalSection);
Schedule.WorkerReady := RTLEventCreate;
SetLength(Flags, Schedule.Total);
Total := Schedule.Total;
Start := 0;
Group := Schedule.GroupSize;
for I := 1 to Schedule^.PoolSize do
begin
AsyncCall(TAsyncCallFun(@AsyncWorker), Schedule, nil, nil);
end;
Active := Schedule^.PoolSize;
while Active > 0 do
begin
RTLEventWaitFor(Schedule.WorkerReady);
RTLeventResetEvent(Schedule.WorkerReady);
DistributeJobs;
end;
RTLEventDestroy(Schedule.WorkerReady);
DoneCriticalsection(Schedule.CriticalSection);
end;
procedure AsyncForEach(Fun: TAsyncCallFun; ParamArray: PParamArray;
Notify: TAsyncCallNotify; NotifyParam: Pointer; Cfg: array of variant);
var
Schedule: PAsyncSchedule;
Total: integer;
Max, Def: integer;
begin
Total := High(ParamArray^) - Low(ParamArray^) + 1;
if Total < 1 then
begin
Notify(NotifyParam);
Exit;
end;
New(Schedule);
Schedule^.GroupSize := PropListGet('GroupSize', 0, Cfg);
if Schedule.GroupSize > Total then
Schedule.GroupSize := Total
else if Schedule.GroupSize < 0 then
Schedule.GroupSize := 0;
if Schedule.GroupSize > 0 then
begin
Max := (Total + Schedule.GroupSize - 1) div Schedule.GroupSize;
Def := Max;
end
else
begin
Max := Total;
Def := DefaultPoolSize;
end;
Schedule.PoolSize := PropListGet('PoolSize', Def, Cfg);
if Schedule.PoolSize < 1 then
Schedule.PoolSize := 1
else if Schedule.PoolSize > Max then
Schedule.PoolSize := Max;
if Schedule.GroupSize = 0 then
Schedule.GroupSize := (Total + Schedule.PoolSize - 1) div Schedule.PoolSize;
Schedule.Fun := Fun;
Schedule.ParamArray := ParamArray;
Schedule.Total := Total;
AsyncCall(TAsyncCallFun(@AsyncSchedule), Schedule, Notify, NotifyParam);
end;
function AtoI(const S: string): integer;
var
T: string;
begin
T := S;
Result := 0;
if T = '' then
Exit;
if T[1] = '$' then
begin
Delete(T, 1, 1);
Result := HexToInt(T);
end
else if (Length(T) > 2) and (T[1] in ['X', 'x']) then
begin
Delete(T, 1, 2);
Result := HexToInt(T);
end
else
Result := StrToIntDef(T, 0);
end;
function HexToInt(HexStr: string): DWord;
var
I: integer;
begin
HexStr := UpperCase(HexStr);
Result := 0;
for i := 1 to length(HexStr) do
begin
Result := Result shl 4;
if HexStr[i] in ['0'..'9'] then
Result := Result + DWord(byte(HexStr[i]) - 48)
else
if HexStr[i] in ['A'..'F'] then
Result := Result + DWord(byte(HexStr[i]) - 55)
else
begin
Break;
end;
end;
end;
function PropListGet(Field: variant; Def: variant; Cfg: array of variant): variant;
var
I: Integer;
begin
Result := Def;
I := 0;
while I < High(Cfg) do
begin
if Cfg[I] = Field then
begin
Result := Cfg[I + 1];
Exit;
end;
Inc(I, 2);
end;
end;
end.