-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActivationQueue.pas
198 lines (157 loc) · 4.35 KB
/
ActivationQueue.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
//
// Copyright (c) Jasper Schellingerhout. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
// I kindly request that you notify me if you use this in your software projects.
// Project located at: https://github.com/schellingerhout/active-object-delphi
// How to use https://schellingerhout.github.io/tags/#active-object
unit ActivationQueue;
interface
uses
system.classes, system.syncobjs, system.generics.collections, system.sysutils;
Type
TMethodRequest = class(TObject)
private
FCall: TProc;
FGuard: TFunc<boolean>;
FGuardless: boolean;
public
constructor Create(const ACall: TProc; const AGuard: TFunc<boolean> = nil);
function guard: boolean; virtual; // returns true as default
procedure call; virtual;
end;
TActivationScheduler = class(TThread)
private
FDataReady: TEvent;
FGuardedMethodRequests: TList<TMethodRequest>;
FActivationQueue: TQueue<TMethodRequest>;
procedure CallPreviouslyGuarded;
protected
// Dispatch the Method Requests on their Servant
// in the Scheduler’s thread.
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
// Insert the Method Request into
// the Activation_Queue. This method
// runs in the thread of its client, i.e.,
// in the Proxy’s thread.
procedure enqueue(AMethodRequest: TMethodRequest);
end;
implementation
{ TActivationScheduler }
constructor TActivationScheduler.Create;
begin
inherited Create(False);
FreeOnTerminate := true;
FDataReady := TEvent.Create();
FActivationQueue := TQueue<TMethodRequest>.Create;
FGuardedMethodRequests := TList<TMethodRequest>.Create;
end;
destructor TActivationScheduler.Destroy;
begin
FDataReady.SetEvent;
FDataReady.Free;
FActivationQueue.Free;
FGuardedMethodRequests.Free;
inherited;
end;
procedure TActivationScheduler.enqueue(AMethodRequest: TMethodRequest);
begin
system.TMonitor.Enter(FActivationQueue);
try
FActivationQueue.enqueue(AMethodRequest);
finally
system.TMonitor.Exit(FActivationQueue);
end;
FDataReady.SetEvent;
end;
procedure TActivationScheduler.CallPreviouslyGuarded;
var
i: integer;
LMethodRequest: TMethodRequest;
begin
// deal with previously guarded requests
for i := 0 to FGuardedMethodRequests.Count - 1 do
begin
LMethodRequest := FGuardedMethodRequests[i];
if LMethodRequest.guard then
begin
LMethodRequest.call;
LMethodRequest.Free;
FGuardedMethodRequests[i] := nil;
end;
if Terminated then
exit;
end;
FGuardedMethodRequests.Pack;
end;
procedure TActivationScheduler.Execute;
var
LMethodRequest: TMethodRequest;
i, LCount: integer;
begin
inherited;
while not terminated do
begin
FDataReady.WaitFor();
CallPreviouslyGuarded;
system.TMonitor.Enter(FActivationQueue);
try
LCount := FActivationQueue.Count;
finally
system.TMonitor.Exit(FActivationQueue);
end;
for i := 1 to LCount do
begin
system.TMonitor.Enter(FActivationQueue);
try
LMethodRequest := FActivationQueue.Dequeue;
finally
system.TMonitor.Exit(FActivationQueue);
end;
if not LMethodRequest.guard then
begin
FGuardedMethodRequests.Add(LMethodRequest);
LMethodRequest := nil;
end;
if LMethodRequest <> nil then
begin
LMethodRequest.call;
// we need to check if the call lifted any guards
CallPreviouslyGuarded;
end;
if Terminated then
exit;
end;
system.TMonitor.Enter(FActivationQueue);
try
if FActivationQueue.Count = 0 then
FDataReady.ResetEvent;
finally
system.TMonitor.Exit(FActivationQueue);
end;
end;
end;
{ TMethodRequest }
procedure TMethodRequest.call;
begin
FCall;
end;
constructor TMethodRequest.Create(const ACall: TProc;
const AGuard: TFunc<boolean> = nil);
begin
inherited Create;
FCall := ACall;
FGuard := AGuard;
FGuardless := not Assigned(AGuard);
end;
function TMethodRequest.guard: boolean;
begin
if FGuardless then
result := true
else
result := FGuard;
end;
end.