-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUAsyncQueue.pas
More file actions
486 lines (415 loc) · 9.68 KB
/
UAsyncQueue.pas
File metadata and controls
486 lines (415 loc) · 9.68 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
481
482
483
484
485
{ Async exchange Queue
Copyright (C) 2018-2020 Red_prig
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
}
unit UAsyncQueue;
{$mode objfpc}{$H+}
interface
uses
SysUtils,LFQueue;
type
TOnParent=Procedure of object;
PQNode=^TQNode;
TQNode=object
private
next_:PQNode;
public
Parent:TOnParent;
end;
TCustomQueue=class
protected
FCount:PtrUInt;
public
Function Send(Node:Pointer):Boolean; virtual; abstract;
Function Recv(Var Node:Pointer):Boolean; virtual; abstract;
Function IsEmpty:Boolean; virtual; abstract;
Function Count:PtrUInt; inline;
procedure OnUpdate;
procedure _OnUpdate(AData:PtrInt;AFlags:dword);
procedure _OnUpdate(Sender:TObject;var Done:Boolean);
end;
TNodeQueue=class(TCustomQueue)
protected
_Q:TIntrusiveMPSCQueue;
public
Constructor Create;
Function Send(Node:Pointer):Boolean; override;
Function Recv(Var Node:Pointer):Boolean; override;
Function IsEmpty:Boolean; override;
end;
TNodeQueueEvent=class(TNodeQueue)
protected
FhEvent:pRTLEvent;
public
Constructor Create;
Destructor Destroy; override;
Function Send(Node:Pointer):Boolean; override;
property hEvent:pRTLEvent read FhEvent;
end;
TNodeQueueSharedEvent=class(TNodeQueue)
protected
FhEvent:pRTLEvent;
public
Function Send(Node:Pointer):Boolean; override;
property hEvent:pRTLEvent read FhEvent write FhEvent;
end;
TSimpleQueue=class(TCustomQueue)
private
type
PNode=^TNode;
TNode=record
pNext:PNode;
//some data
end;
Var
pHead,pTail:PNode;
public
Constructor Create;
Function Send(Node:Pointer):Boolean; override;
Function Recv(Var Node:Pointer):Boolean; override;
Function IsEmpty:Boolean; override;
end;
PQFrom2Node=^TQFrom2Node;
TQFrom2Node=object(TQNode)
protected
FQueue:TCustomQueue;
public
end;
TCustomAsyncThread=class
Const
RtlWaitTime=10*1000;
private
hThread:TThreadID;
protected
FState:Boolean;
FQueue:TNodeQueueEvent;
FCmdQueue:TNodeQueueSharedEvent;
FCreator:TCustomQueue;
Procedure InherBegin; virtual;
Procedure InherUpdate; virtual;
Procedure InherEnd; virtual;
public
property State:Boolean read FState;
Constructor Create;
Destructor Destroy; override;
function Send(Node:PQFrom2Node):Boolean; virtual;
function SendCmd(Node:PQFrom2Node):Boolean; virtual;
procedure SyncFunc(N:TOnParent); virtual;
end;
function SendMainQueue(Node:Pointer):Boolean;
function SendCurrQueue(Node:Pointer):Boolean;
function SendFrom(Node:PQFrom2Node):Boolean;
Var
MainQueue:TCustomQueue;
threadvar
CurrQueue:TCustomQueue;
implementation
Uses ULog;
function SendMainQueue(Node:Pointer):Boolean;
begin
Result:=false;
if Assigned(MainQueue) then
begin
Result:=MainQueue.Send(Node);
end;
if not Result then raise exception.create('SendMainQueue');
end;
function SendCurrQueue(Node:Pointer):Boolean;
begin
Result:=false;
if Assigned(CurrQueue) then
begin
Result:=CurrQueue.Send(Node);
end;
if not Result then raise exception.create('SendCurrQueue');
end;
function SendFrom(Node:PQFrom2Node):Boolean;
begin
Result:=False;
if Assigned(Node) then
if Assigned(Node^.FQueue) then
begin
Result:=Node^.FQueue.Send(Node);
end;
if not Result then Log(app_logic,1,['Node(',Node,')^.SendFrom:',Node^.FQueue]);
end;
function CustomAsyncThread_Execute(RT:TCustomAsyncThread):ptrint; forward;
Constructor TCustomAsyncThread.Create;
begin
FState:=False;
FQueue:=TNodeQueueEvent.Create;
FCmdQueue:=TNodeQueueSharedEvent.Create;
FCmdQueue.hEvent:=FQueue.hEvent;
FCreator:=CurrQueue;
hThread:=BeginThread(tthreadfunc(@CustomAsyncThread_Execute),Self);
if hThread=0 then
begin
FState:=True;
Log(app_logic,1,['BeginThread(',ClassName,'):',hThread]);
end;
end;
Destructor TCustomAsyncThread.Destroy;
begin
if (not (FState)) or (hThread<>0) then
begin
FState:=True;
RTLeventSetEvent(FQueue.hEvent);
WaitForThreadTerminate(hThread,-1);
CloseThread(hThread);
end;
FreeAndNil(FQueue);
FreeAndNil(FCmdQueue);
inherited;
end;
function TCustomAsyncThread.Send(Node:PQFrom2Node):Boolean;
begin
Result:=False;
if Assigned(Self) then
begin
if FState then Exit;
if Assigned(Node) then
begin
Node^.FQueue:=UAsyncQueue.CurrQueue;
Result:=FQueue.Send(Node);
end else
begin
Log(app_logic,1,['Assigned(Node):',Node]);
end;
end else
begin
Log(app_logic,1,['Assigned(CustomAsyncThread):',Self]);
end;
end;
function TCustomAsyncThread.SendCmd(Node:PQFrom2Node):Boolean;
begin
Result:=False;
if Assigned(Self) then
begin
if FState then Exit;
if Assigned(Node) then
begin
Node^.FQueue:=UAsyncQueue.CurrQueue;
Result:=FCmdQueue.Send(Node);
end else
begin
Log(app_logic,1,['Assigned(Node):',Node]);
end;
end else
begin
Log(app_logic,1,['Assigned(CustomAsyncThread):',Self]);
end;
end;
type
PSyncFuncNode=^TSyncFuncNode;
TSyncFuncNode=object(UAsyncQueue.TQNode)
N:TOnParent;
Procedure Call;
end;
Procedure TSyncFuncNode.Call;
begin
if Assigned(N) then N();
FreeMem(@Self);
end;
procedure TCustomAsyncThread.SyncFunc(N:TOnParent);
Var
Node:PSyncFuncNode;
begin
if Assigned(N) and Assigned(FCreator) then
begin
Node:=AllocMem(SizeOf(TSyncFuncNode));
Node^:=Default(TSyncFuncNode);
Node^.Parent:=@Node^.Call;
Node^.N:=N;
FCreator.Send(Node);
end;
end;
Procedure TCustomAsyncThread.InherBegin;
begin
CurrQueue:=FCmdQueue;
if not Assigned(Self) then
begin
Log(app_logic,1,['Assigned(Thread):',Self]);
FState:=True;
end;
end;
Procedure TCustomAsyncThread.InherUpdate;
begin
if FQueue.IsEmpty then
begin
RTLeventWaitFor(FQueue.hEvent,RtlWaitTime);
end;
end;
Procedure TCustomAsyncThread.InherEnd;
begin
end;
function CustomAsyncThread_Execute(RT:TCustomAsyncThread):ptrint;
begin
Result:=0;
RT.InherBegin;
if not RT.FState then
repeat
try
RT.InherUpdate;
except
on E:Exception do
begin
DumpExceptionCallStack(E);
end;
end;
until RT.FState;
RT.InherEnd;
end;
///////////////
Constructor TNodeQueue.Create;
begin
inherited;
_Q.Create;
end;
Function TNodeQueue.Send(Node:Pointer):Boolean;
begin
Result:=_Q.Push(Node);
if Result then
begin
System.InterLockedIncrement(Pointer(FCount));
end;
end;
Function TNodeQueue.Recv(Var Node:Pointer):Boolean;
begin
Result:=_Q.Pop(Node);
if Result then
begin
System.InterLockedDecrement(Pointer(FCount));
end;
end;
Function TNodeQueue.IsEmpty:Boolean;
begin
Result:=_Q.IsEmpty;
end;
Function TCustomQueue.Count:PtrUInt;
begin
Result:=FCount;
end;
procedure TCustomQueue.OnUpdate;
Var
rc:PtrUInt;
Node:PQNode;
begin
if not Assigned(Self) then Exit;
Node:=nil;
rc:=Count;
try
While Recv(Node) do
begin
if Assigned(Node^.Parent) then
begin
Node^.Parent();
end else
begin
Log(app_logic,1,['Assigned(Node^.Parent):',TMethod(Node^.Parent).Code,TMethod(Node^.Parent).Data]);
end;
if rc=0 then Break;
Dec(rc);
if rc=0 then Break;
end;
except
on E:Exception do
DumpExceptionCallStack(E);
end;
end;
procedure TCustomQueue._OnUpdate(AData:PtrInt;AFlags:dword);
begin
OnUpdate;
end;
procedure TCustomQueue._OnUpdate(Sender:TObject;var Done:Boolean);
begin
Done:=False;
OnUpdate;
end;
Constructor TNodeQueueEvent.Create;
begin
inherited;
FhEvent:=RTLEventCreate;
end;
Destructor TNodeQueueEvent.Destroy;
begin
RTLEventDestroy(FhEvent);
inherited;
end;
Function TNodeQueueEvent.Send(Node:Pointer):Boolean;
begin
Result:=inherited Send(Node);
if Result then
begin
RTLEventSetEvent(FhEvent);
end;
end;
////TNodeQueueSharedEvent
Function TNodeQueueSharedEvent.Send(Node:Pointer):Boolean;
begin
Result:=inherited Send(Node);
if Result and Assigned(FhEvent) then
begin
RTLEventSetEvent(FhEvent);
end;
end;
//TSimpleQueue
Constructor TSimpleQueue.Create;
begin
pHead:=nil;
pTail:=nil;
end;
Function TSimpleQueue.Send(Node:Pointer):Boolean;
begin
if not Assigned(Node) then Exit(False);
if pTail=nil then
begin
pHead:=Node;
end else
begin
pTail^.pNext:=Node;
end;
pTail:=Node;
pTail^.pNext:=nil;
Result:=True;
Inc(FCount);
end;
Function TSimpleQueue.Recv(Var Node:Pointer):Boolean;
begin
Node:=pHead;
Result:=Node<>nil;
if Result then
begin
pHead:=PNode(Node)^.pNext;
PNode(Node)^.pNext:=nil;
if pHead=nil then
pTail:=nil;
Dec(FCount);
end;
end;
Function TSimpleQueue.IsEmpty:Boolean;
begin
Result:=pTail=nil;
end;
initialization
MainQueue:=nil;
CurrQueue:=nil;
finalization
FreeAndNil(MainQueue);
end.