25 |
|
* |
26 |
|
*) |
27 |
|
unit FB30Events; |
28 |
+ |
{$IFDEF MSWINDOWS} |
29 |
+ |
{$DEFINE WINDOWS} |
30 |
+ |
{$ENDIF} |
31 |
|
|
32 |
|
{$IFDEF FPC} |
33 |
< |
{$mode objfpc}{$H+} |
33 |
> |
{$mode delphi} |
34 |
|
{$interfaces COM} |
35 |
|
{$ENDIF} |
36 |
|
|
37 |
+ |
{ $DEFINE EVENTDEBUG} |
38 |
+ |
|
39 |
|
interface |
40 |
|
|
41 |
|
uses |
50 |
|
TEventhandlerInterface = class(Firebird.IEventCallbackImpl) |
51 |
|
private |
52 |
|
FOwner: TFB30Events; |
53 |
< |
FName: string; |
53 |
> |
FName: AnsiString; |
54 |
|
FRef: integer; |
55 |
|
{$IFDEF WINDOWS} |
56 |
|
{Make direct use of Windows API as TEventObject don't seem to work under |
60 |
|
FEventWaiting: TEventObject; |
61 |
|
{$ENDIF} |
62 |
|
public |
63 |
< |
constructor Create(aOwner: TFB30Events; aName: string); |
63 |
> |
constructor Create(aOwner: TFB30Events; aName: AnsiString); |
64 |
|
destructor Destroy; override; |
65 |
|
procedure addRef(); override; |
66 |
|
function release(): Integer; override; |
110 |
|
procedure Terminate; |
111 |
|
end; |
112 |
|
|
113 |
< |
constructor TEventhandlerInterface.Create(aOwner: TFB30Events; aName: string); |
113 |
> |
constructor TEventhandlerInterface.Create(aOwner: TFB30Events; aName: AnsiString); |
114 |
|
var |
115 |
|
PSa : PSecurityAttributes; |
116 |
|
{$IFDEF WINDOWS} |
138 |
|
FOWner := aOwner; |
139 |
|
FName := aName; |
140 |
|
addRef; |
141 |
+ |
{$IFDEF EVENTDEBUG} writeln(FName,' TEventhandlerInterface: Create'); {$ENDIF} |
142 |
|
end; |
143 |
|
|
144 |
|
destructor TEventhandlerInterface.Destroy; |
148 |
|
{$ELSE} |
149 |
|
if assigned(FEventWaiting) then FEventWaiting.Free; |
150 |
|
{$ENDIF} |
151 |
+ |
{$IFDEF EVENTDEBUG} writeln(FName,' TEventhandlerInterface: Destroy'); {$ENDIF} |
152 |
|
inherited Destroy; |
153 |
|
end; |
154 |
|
|
155 |
|
procedure TEventhandlerInterface.addRef; |
156 |
|
begin |
157 |
|
Inc(FRef); |
158 |
< |
// writeln(FName,': ref count = ',FRef); |
158 |
> |
{$IFDEF EVENTDEBUG} writeln(FName,': ref count = ',FRef);{$ENDIF} |
159 |
|
end; |
160 |
|
|
161 |
|
function TEventhandlerInterface.release: Integer; |
162 |
|
begin |
163 |
|
Dec(FRef); |
164 |
< |
// writeln(FName,': ref count = ',FRef); |
164 |
> |
{$IFDEF EVENTDEBUG} writeln(FName,': ref count = ',FRef);{$ENDIF} |
165 |
|
if FRef = 0 then Free; |
166 |
|
Result := FRef; |
167 |
|
end; |
169 |
|
procedure TEventhandlerInterface.eventCallbackFunction(length: Cardinal; |
170 |
|
events: BytePtr); |
171 |
|
begin |
172 |
+ |
{$IFDEF EVENTDEBUG} writeln(FName,' TEventhandlerInterface: Event Callback'); {$ENDIF} |
173 |
|
FOwner.FCriticalSection.Enter; |
174 |
|
try |
175 |
|
if FOwner.FResultBuffer <> nil then |
176 |
< |
Move(events[0], FOwner.FResultBuffer[0], Length); |
176 |
> |
Move(events^, FOwner.FResultBuffer^, Length); |
177 |
|
finally |
178 |
|
FOwner.FCriticalSection.Leave |
179 |
|
end; |
180 |
< |
// writeln('Set Event'); |
180 |
> |
{$IFDEF EVENTDEBUG}writeln(FName,' TEventhandlerInterface: Set Event Called'); {$ENDIF} |
181 |
|
{$IFDEF WINDOWS} |
182 |
|
SetEvent(FEventHandler); |
183 |
|
{$ELSE} |
187 |
|
|
188 |
|
procedure TEventhandlerInterface.WaitForEvent; |
189 |
|
begin |
190 |
+ |
{$IFDEF EVENTDEBUG} writeln(FName,' TEventhandlerInterface: Start Event Wait'); {$ENDIF} |
191 |
|
{$IFDEF WINDOWS} |
192 |
|
WaitForSingleObject(FEventHandler,INFINITE); |
193 |
|
{$ELSE} |
194 |
|
FEventWaiting.WaitFor(INFINITE); |
195 |
|
{$ENDIF} |
196 |
< |
// writeln('Event Wait Ends'); |
196 |
> |
{$IFDEF EVENTDEBUG} writeln(FName,' TEventhandlerInterface: Event Wait Ends');{$ENDIF} |
197 |
|
end; |
198 |
|
|
199 |
|
procedure TEventhandlerInterface.CancelWait; |
209 |
|
|
210 |
|
procedure TEventHandlerThread.Execute; |
211 |
|
begin |
212 |
+ |
{$IFDEF EVENTDEBUG} writeln('Event Handler Thread Starts'); {$ENDIF} |
213 |
|
while not Terminated do |
214 |
|
begin |
215 |
|
FEventHandler.WaitForEvent; |
216 |
+ |
{$IFDEF EVENTDEBUG} writeln('Event Handler Ends Wait ',Terminated); {$ENDIF} |
217 |
|
|
218 |
|
if not Terminated then |
219 |
|
FOwner.EventSignaled; |
220 |
|
end; |
221 |
+ |
{$IFDEF EVENTDEBUG} writeln('Event Handler Thread Ends'); {$ENDIF} |
222 |
|
end; |
223 |
|
|
224 |
|
constructor TEventHandlerThread.Create(Owner: TFB30Events; |
225 |
|
EventHandler: TEventhandlerInterface); |
226 |
|
begin |
227 |
< |
inherited Create(true); |
227 |
> |
inherited Create(false); |
228 |
|
FOwner := Owner; |
229 |
|
FEventHandler := EventHandler; |
230 |
|
FreeOnTerminate := true; |
219 |
– |
Start; |
231 |
|
end; |
232 |
|
|
233 |
|
procedure TEventHandlerThread.Terminate; |
234 |
|
begin |
235 |
|
inherited Terminate; |
236 |
|
FEventHandler.CancelWait; |
237 |
+ |
{$IFDEF EVENTDEBUG} writeln('Event Handler Thread Cancelled'); {$ENDIF} |
238 |
|
end; |
239 |
|
|
240 |
|
{ TFB30Events } |