forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FIBSafeTimer.pas
315 lines (272 loc) · 7.03 KB
/
FIBSafeTimer.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2009 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit FIBSafeTimer;
interface
{$I FIBPlus.inc}
uses
{$IFDEF WINDOWS}
Windows,
{$ELSE}
{$IFDEF MACOS}
Macapi.ObjCRuntime, Macapi.CocoaTypes,
Macapi.ObjectiveC, Macapi.Foundation,
{$ENDIF}
{$ENDIF}
FIBPlatforms, SyncObjs, SysUtils, Classes;
type
{ TFIBCustomTimer }
TFIBCustomTimer = class(TComponent)
private
FActive: Boolean;
FSynchronizing:boolean;
{$IFNDEF MACOS}
FHandle: Integer;
{$ENDIF}
FInterval: Integer;
FOnTimer: TNotifyEvent;
procedure SetActive(const Value: Boolean);
procedure SetInterval(const Value: Integer);
protected
{$IFDEF MACOS}
User:TObject;
{$ENDIF}
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Synchronizing:boolean read FSynchronizing write FSynchronizing;
property Enabled: Boolean read FActive write SetActive;
property Interval: Integer read FInterval write SetInterval;
property OnTimer: TNotifyEvent read FOnTimer write FOnTimer;
end;
implementation
{$IFDEF D_XE3}
uses System.Types,System.TypInfo; // for inline funcs
{$ENDIF}
var
TimerList: TThreadList;
type
TSyncThread=class(TThread)
protected
FTimer:TFIBCustomTimer;
procedure Execute; override;
procedure DoOnTimer;
end;
{$IFDEF WINDOWS}
procedure TimerProc(HWND, uMsg, idEvent, dwTime : Integer); stdcall;
var
I: Integer;
Timer: TFIBCustomTimer;
List:TList;
st:TSyncThread;
begin
if TimerList=nil then
Exit;
I := 0;
List:=TimerList.LockList;
try
while I < List.Count do
begin
Timer := List.Items[I];
if (Timer <> nil) and (idEvent = Timer.FHandle) and
(Assigned(Timer.FOnTimer))
then
begin
if not Timer.FSynchronizing or (CurrentThreadID = MainThreadID) then
Timer.FOnTimer(Timer)
else
begin
st:=TSyncThread.Create(True);
try
st.FTimer:=Timer;
st.Synchronize(st.DoOnTimer);
finally
st.Free
end;
end ;
Exit;
end;
Inc(I);
end;
finally
TimerList.UnlockList
end
end;
{$ENDIF}
{$IFDEF MACOS}
type
TTimerProc = procedure of object;
ICocoaTimer = interface(NSObject)
['{5AF4F553-FFB8-4ED1-AAC0-D6F33DCB6AAC}']
procedure timerEvent; cdecl;
procedure release; cdecl;
end;
TFIBWrapMacTimer = class(TOCLocal)
private
Owner:TFIBCustomTimer;
MacNativeTimer: NSTimer;
public
function GetObjectiveCClass: PTypeInfo; override;
procedure timerEvent; cdecl;
procedure release; cdecl;
end;
{ threadvar
FHandleCounter:Cardinal;}
function TFIBWrapMacTimer.GetObjectiveCClass: PTypeInfo;
begin
Result := TypeInfo(ICocoaTimer);
end;
procedure TFIBWrapMacTimer.timerEvent;
var
st:TSyncThread;
begin
if Assigned(Owner.FOnTimer) then
if not Owner.FSynchronizing or (CurrentThreadID=MainThreadID) then
Owner.FOnTimer(Owner)
else
begin
st:=TSyncThread.Create(True);
try
st.FTimer:=Owner;
st.Synchronize(st.DoOnTimer);
finally
st.Free
end;
end
end;
procedure TFIBWrapMacTimer.release;
var
RC: Integer;
begin
RC := NSObject(Super).retainCount;
NSObject(Super).release;
if RC = 1 then
Destroy;
end;
procedure MACCreateTimer(AOwner:TFIBCustomTimer;Interval: Integer);
var
User: TFIBWrapMacTimer;
LInterval: NSTimeInterval;
begin
User := TFIBWrapMacTimer.Create;
try
LInterval := Interval/1000;
User.MacNativeTimer := TNSTimer.Wrap(TNSTimer.OCClass.scheduledTimerWithTimeInterval(LInterval,
User.GetObjectID, sel_getUid('timerEvent'), User.GetObjectID, True));
AOwner.User:=User;
User.Owner:=AOwner;
finally
{user is retained (twice, because it's target), by the timer and }
{released (twice) on timer invalidation}
NSObject(User.Super).release;
end;
end;
function MACDestroyTimer(Timer: TFIBCustomTimer): Boolean;
var
NativeTimer: NSTimer;
List:TList;
i:Integer;
begin
Result := False;
List:=TimerList.LockList;
try
i:=List.IndexOf(Timer);
if i>=0 then
begin
NativeTimer:=TFIBWrapMacTimer(Timer.User).MacNativeTimer;
if Assigned(NativeTimer) then
begin
Result := True;
NativeTimer.invalidate;
TFIBWrapMacTimer(Timer.User).MacNativeTimer:=nil;
NativeTimer:=nil;
end;
end;
finally
TimerList.UnlockList
end;
end;
{$ENDIF}
{ TFIBCustomTimer }
constructor TFIBCustomTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// TThread.CurrentThread.ThreadID;
FActive := False;
FInterval := 1000; {}
FSynchronizing:=True;
if TimerList<>nil then
TimerList.Add(Self);
end;
destructor TFIBCustomTimer.Destroy;
begin
Enabled := False;
if TimerList<>nil then
TimerList.Remove(Self);
inherited;
end;
procedure TFIBCustomTimer.SetActive(const Value: Boolean);
begin
if FActive = Value then
Exit;
if FActive then
begin
{$IFDEF WINDOWS}
KillTimer(0, FHandle);
FHandle := 0;
{$ENDIF}
{$IFDEF MACOS}
MACDestroyTimer(Self);
{$ENDIF}
end;
if Value then
begin
{$IFDEF WINDOWS}
FHandle := SetTimer(0, 0, FInterval, @TimerProc);
{$ENDIF}
{$IFDEF MACOS}
MACCreateTimer(Self,Interval)
{$ENDIF}
end;
FActive := Value;
end;
procedure TFIBCustomTimer.SetInterval(const Value: Integer);
var
OldEnabled: Boolean;
begin
OldEnabled := Enabled;
Enabled := False;
FInterval := Value;
Enabled := OldEnabled;
end;
{ TSyncThread }
procedure TSyncThread.DoOnTimer;
begin
if Assigned(FTimer.OnTimer) then
FTimer.OnTimer(FTimer)
end;
procedure TSyncThread.Execute;
begin
end;
initialization
TimerList := TThreadList.Create;
finalization
TimerList.Free;
TimerList:=nil;
end.