-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcocinasync.async.pas
408 lines (353 loc) · 8.77 KB
/
cocinasync.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
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
unit cocinasync.async;
interface
uses System.SysUtils, System.Classes, cocinasync.global, cocinasync.jobs,
System.SyncObjs;
type
IMREWSync = interface
procedure BeginRead;
procedure EndRead;
procedure BeginWrite;
procedure EndWrite;
end;
TAsync = class(TObject)
strict private
FCounter : TThreadCounter;
FTerminating : boolean;
strict private
class var FSynchronizeInMainThread : boolean;
class var FSync : TCriticalSection;
public
class function CreateMREWSync : IMREWSync;
class procedure SynchronizeIfInThread(const proc : TProc);
class procedure QueueIfInThread(const proc : TProc);
class property SynchronizeInMainThread : boolean read FSynchronizeInMainThread write FSynchronizeInMainThread;
class constructor Create;
class destructor Destroy;
procedure AfterDo(const After : Cardinal; const &Do : TProc; SynchronizedDo : boolean = true; const JobsOverride : IJobs = nil);
procedure DoLater(const &Do : TProc; SynchronizedDo : boolean = true; const JobsOverride : IJobs = nil);
procedure OnDo(const &On : TFunc<Boolean>; const &Do : TProc; CheckInterval : integer = 1000; &Repeat : TFunc<boolean> = nil; SynchronizedOn : boolean = false; SynchronizedDo : boolean = true; const JobsOverride : IJobs = nil);
function DoEvery(const MS : Cardinal; const &Do : TFunc<Boolean>; SynchronizedDo : boolean = true) : TThread;
constructor Create; reintroduce; virtual;
destructor Destroy; override;
end;
var
// NOTE: This global object handles async work at the global level. If you
// are using within a form or some other object that could be destroyed
// before the app is terminated, then you should create your own instance
// in your form or other object and use that instead. This will ensure
// that async operations are complete before your object is destroyed.
Async : TAsync;
{$IFDEF MSWINDOWS}
{.$DEFINE USEOURMWERS}
{$ENDIF}
implementation
{$IFDEF USEOURMWERS}uses WinAPI.Windows; {$ENDIF}
type
// NOTE: The intention here was to provide a faster MREWSync. Unfortunately
// the implementation below has a deadlock in it that hasn't yet been fixed
// for now the default is to use the MREWSync that comes with Delphi.
{ TFastMREWSync }
TFastMREWSync = class(TInterfacedObject, IMREWSync)
private
FRef : {$IFDEF USEOURMWERS}Integer{$ELSE}TMREWSync{$ENDIF};
{$IFDEF USEOURMWERS}
FReadLockCount : Cardinal;
FWriteLockCount : Cardinal;
{$ENDIF}
public
constructor Create;
destructor Destroy; override;
procedure BeginRead;
procedure EndRead;
procedure BeginWrite;
procedure EndWrite;
end;
{ TFastMREWSync }
constructor TFastMREWSync.Create;
begin
inherited Create;
{$IFNDEF USEOURMWERS}
FRef := TMREWSync.Create;
{$ENDIF}
end;
destructor TFastMREWSync.Destroy;
begin
{$IFNDEF USEOURMWERS}
FRef.Free;
{$ENDIF}
inherited;
end;
procedure TFastMREWSync.BeginRead;
{$IFDEF USEOURMWERS}
var
ref : Integer;
begin
//Wait on writer to reset write flag so Reference.Bit0 must be 0 than increase Reference
repeat
ref := Integer(FRef) and not 1;
until ref = Integer(InterlockedCompareExchange(FRef, ref + 2, ref));
inc(FReadLockCount);
end;
{$ELSE}
begin
FRef.BeginRead;
end;
{$ENDIF}
procedure TFastMREWSync.BeginWrite;
{$IFDEF USEOURMWERS}
var
ref : integer;
begin
//Wait on writer to reset write flag so omrewReference.Bit0 must be 0 then set omrewReference.Bit0
repeat
ref := FRef and (not 1);
until ref = Integer(InterlockedCompareExchange(FRef, ref+1, ref));
//Now wait on all readers
repeat
until FRef = 1;
inc(FWriteLockCount);
end;
{$ELSE}
begin
FRef.BeginWrite;
end;
{$ENDIF}
procedure TFastMREWSync.EndRead;
begin
{$IFDEF USEOURMWERS}
//Decrease omrewReference
InterlockedExchangeAdd(@FRef, -2);
dec(FReadLockCount);
{$ELSE}
FRef.EndRead;
{$ENDIF}
end;
procedure TFastMREWSync.EndWrite;
begin
{$IFDEF USEOURMWERS}
FRef := 0;
dec(FWriteLockCount);
{$ELSE}
FRef.EndWrite;
{$ENDIF}
end;
{ TAsync }
constructor TAsync.Create;
begin
inherited Create;
FCounter := TThreadCounter.Create;
FTerminating := False;
end;
class constructor TAsync.Create;
begin
FSynchronizeInMainThread := True;
FSync := TCriticalSection.Create;
end;
class function TAsync.CreateMREWSync : IMREWSync;
begin
Result := TFastMREWSync.Create;
end;
class procedure TAsync.SynchronizeIfInThread(const proc : TProc);
begin
if FSynchronizeInMainThread then
begin
if TThread.Current.ThreadID <> MainThreadID then
begin
TThread.Synchronize(TThread.Current,
procedure
begin
proc();
end
);
end else
proc();
end else
begin
FSync.Enter;
try
proc();
finally
FSync.Leave;
end;
end
end;
class procedure TAsync.QueueIfInThread(const proc : TProc);
begin
if FSynchronizeInMainThread then
begin
if TThread.Current.ThreadID <> MainThreadID then
begin
TThread.Queue(TThread.Current,
procedure
begin
proc();
end
);
end else
proc();
end else
begin
FSync.Enter;
try
proc();
finally
FSync.Leave;
end;
end
end;
type
TThreadHack = class(TThread) end;
destructor TAsync.Destroy;
begin
FTerminating := True;
FCounter.WaitForAll;
FCounter.Free;
inherited;
end;
class destructor TAsync.Destroy;
begin
FSync.Free;
end;
function TAsync.DoEvery(const MS : Cardinal; const &Do : TFunc<Boolean>; SynchronizedDo : boolean = true) : TThread;
begin
Result := TThread.CreateAnonymousThread(
procedure
var
bContinue : boolean;
begin
FCounter.NotifyThreadStart;
try
bContinue := True;
while not TThreadHack(TThread.Current).Terminated do
begin
if FTerminating then
Exit;
sleep(MS);
if SynchronizedDo then
SynchronizeIfInThread(
procedure
begin
bContinue := &Do();
end
)
else
bContinue := &Do();
if not bContinue then
break;
end;
finally
FCounter.NotifyThreadEnd;
end;
end
);
Result.Start;
end;
procedure TAsync.AfterDo(const After : Cardinal; const &Do : TProc; SynchronizedDo : boolean = true; const JobsOverride : IJobs = nil);
begin
TJobManager.Execute(
procedure
begin
if FTerminating then
Exit;
FCounter.NotifyThreadStart;
try
sleep(After);
if SynchronizedDo then
SynchronizeIfInThread(
procedure
begin
&Do();
end
)
else
&Do();
finally
FCounter.NotifyThreadEnd;
end;
end,
JobsOverride
);
end;
procedure TAsync.DoLater(const &Do : TProc; SynchronizedDo : boolean = true; const JobsOverride : IJobs = nil);
begin
TJobManager.Execute(
procedure
begin
if FTerminating then
Exit;
FCounter.NotifyThreadStart;
try
if SynchronizedDo then
SynchronizeIfInThread(
procedure
begin
&Do();
end
)
else
&Do();
finally
FCounter.NotifyThreadEnd;
end;
end,
JobsOverride
);
end;
procedure TAsync.OnDo(const &On : TFunc<Boolean>; const &Do : TProc; CheckInterval : integer = 1000; &Repeat : TFunc<boolean> = nil; SynchronizedOn : boolean = false; SynchronizedDo : boolean = true; const JobsOverride : IJobs = nil);
begin
if not Assigned(&Repeat) then
&Repeat :=
function : boolean
begin
Result := False;
end;
TJobManager.Execute(
procedure
var
bOn : Boolean;
begin
FCounter.NotifyThreadStart;
try
repeat
if FTerminating then
Exit;
repeat
if FTerminating then
Exit;
if SynchronizedOn then
SynchronizeIfInThread(
procedure
begin
bOn := &On();
end
)
else
bOn := &On();
if bOn then
break;
sleep(CheckInterval)
until bOn;
if FTerminating then
Exit;
if SynchronizedDo then
SynchronizeIfInThread(
procedure
begin
&Do();
end
)
else
&Do();
until not &Repeat();
finally
FCounter.NotifyThreadEnd;
end;
end,
JobsOverride
);
end;
initialization
Async := TAsync.Create;
finalization
Async.Free;
end.