-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.PortableTimer.pas
More file actions
423 lines (366 loc) · 9.31 KB
/
Copy pathMaxLogic.PortableTimer.pas
File metadata and controls
423 lines (366 loc) · 9.31 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
unit MaxLogic.PortableTimer;
{$I fpc_delphimode.inc}
interface
uses
Classes, SysUtils, SyncObjs,
maxSignal, // iSignal / TSignal
maxlogic.fpc.compatibility, // CS & helpers
CancelToken; // iCancelToken / TCancelToken
type
TTimerMode = (tmFixedDelay, tmFixedRate);
// Non-visual, cross-platform threaded timer.
// Fires OnTimer on its own worker thread.
TThreadedTimer = class(TComponent)
private
type
TTimerThread = class(TThread)
private
fOwner: TObject; // backref, used only for callback target
fWake: iSignal; // auto-reset event (wake/sleep)
fCancel: iCancelToken; // cooperative cancellation
protected
procedure Execute; override;
public
constructor Create(aOwner: TObject; const aWake: iSignal; const aCancel: iCancelToken);
end;
private
fIntervalMs: Cardinal;
fMode: TTimerMode;
fEnabled: Boolean;
fOnTimer: TNotifyEvent;
fLock: TCriticalSection;
fThread: TTimerThread;
fWake: iSignal;
fCancel: iCancelToken;
// user data
fTag: NativeInt;
fDataPtr: Pointer;
fDataIntf: IInterface;
function GetInterval: Cardinal;
procedure SetInterval(const aValue: Cardinal);
procedure SetEnabled(const aValue: Boolean);
procedure SetOnTimer(const aValue: TNotifyEvent);
function GetMode: TTimerMode;
procedure SetMode(const aValue: TTimerMode);
function GetTag: NativeInt;
procedure SetTag(const aValue: NativeInt);
function GetDataPtr: Pointer;
procedure SetDataPtr(const aValue: Pointer);
function GetDataIntf: IInterface;
procedure SetDataIntf(const aValue: IInterface);
procedure Kick; inline;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
// milliseconds, coerced to >=1
property Interval: Cardinal read GetInterval write SetInterval;
// run state
property Enabled: Boolean read fEnabled write SetEnabled;
// timer cadence
property SchedulingMode: TTimerMode read GetMode write SetMode; // tmFixedDelay/tmFixedRate
// callback
property OnTimer: TNotifyEvent read fOnTimer write SetOnTimer;
// read-only exposures
property CancelToken: iCancelToken read fCancel;
// user payloads
property Tag: NativeInt read GetTag write SetTag;
property DataPtr: Pointer read GetDataPtr write SetDataPtr;
property DataIntf: IInterface read GetDataIntf write SetDataIntf;
end;
TPortableTimer = class(TThreadedTimer)
public
constructor Create; reintroduce;
procedure Start(const aIntervalMs: Cardinal);
procedure Stop;
end;
implementation
{ TThreadedTimer.TTimerThread }
constructor TThreadedTimer.TTimerThread.Create(aOwner: TObject; const aWake: iSignal; const aCancel: iCancelToken);
begin
inherited Create(False);
FreeOnTerminate := False;
fOwner := aOwner;
fWake := aWake;
fCancel := aCancel;
end;
procedure TThreadedTimer.TTimerThread.Execute;
var
lHandler: TNotifyEvent;
lInterval: Cardinal;
lEnabled: Boolean;
lMode: TTimerMode;
lRes: TWaitResult;
lOwner: TThreadedTimer;
function NowMs: QWord; inline;
begin
Result := GetTickCount64;
end;
procedure SnapshotState;
begin
lOwner.fLock.Acquire;
try
lEnabled := lOwner.fEnabled;
lInterval := lOwner.fIntervalMs;
lHandler := lOwner.fOnTimer;
lMode := lOwner.fMode;
finally
lOwner.fLock.Release;
end;
if lInterval = 0 then
lInterval := 1;
end;
var
lNextDue: QWord;
lNow: QWord;
lWait: Cardinal;
begin
lOwner := TThreadedTimer(fOwner);
// initial snapshot & next due time
SnapshotState;
lNextDue := NowMs + lInterval;
while (not fCancel.Canceled) and (not Terminated) do
begin
// if disabled or no handler, idle with a longer wait but still wake-able
if (not lEnabled) or (not Assigned(lHandler)) then
begin
lOwner.fWake.WaitForSignaled(1000);
if fCancel.Canceled or Terminated then Break;
// state might have changed; refresh and set next due relative to now
SnapshotState;
lNextDue := NowMs + lInterval;
Continue;
end;
// compute remaining time until next due (for fixed-rate alignment)
lNow := NowMs;
if lNow >= lNextDue then
lWait := 0
else
begin
// clamp to Cardinal range for WaitForSignaled
if (lNextDue - lNow) > High(Cardinal) then
lWait := High(Cardinal)
else
lWait := Cardinal(lNextDue - lNow);
end;
lRes := lOwner.fWake.WaitForSignaled(lWait);
if fCancel.Canceled or Terminated then Break;
// refresh state after the wait (OnTimer could have been reassigned)
SnapshotState;
// Time to fire if we actually timed out (i.e., reached due time)
if (lRes = wrTimeout) and lEnabled and Assigned(lHandler) then
begin
try
lHandler(lOwner);
except
// swallow/log to keep the timer alive
end;
// schedule next tick based on selected mode
if lMode = tmFixedDelay then
lNextDue := NowMs + lInterval // fixed delay after completion
else
begin
// fixed rate: keep cadence relative to prior scheduled time
// advance by one or more intervals to avoid drift if we overran
repeat
lNextDue := lNextDue + lInterval;
until lNextDue > NowMs;
end;
end
else
begin
// woke early (Kick or state change) -> recompute next due conservatively
// so changes apply promptly
lNextDue := NowMs + lInterval;
end;
end;
end;
{ TThreadedTimer }
constructor TThreadedTimer.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
fLock := TCriticalSection.Create;
fWake := TSignal.Create(True{auto-reset});
fCancel := TCancelToken.Create;
fIntervalMs := 1000;
fMode := tmFixedDelay;
fEnabled := False;
fTag := 0;
fDataPtr := nil;
fDataIntf := nil;
fThread := TTimerThread.Create(Self, fWake, fCancel);
end;
destructor TThreadedTimer.Destroy;
var
lThread: TTimerThread;
lIsSelf: Boolean;
begin
// Detach local reference to the thread under lock
fLock.Acquire;
try
lThread := fThread;
fThread := nil;
finally
fLock.Release;
end;
if lThread <> nil then
begin
// cooperatively cancel & wake
fCancel.Cancel;
fWake.SetSignaled;
lIsSelf := (TThread.CurrentThread.ThreadID = lThread.ThreadID);
if lIsSelf then
begin
// Freeing from inside OnTimer: let the worker free itself
lThread.FreeOnTerminate := True;
// no WaitFor on self-thread
end else begin
lThread.WaitFor; // deterministic shutdown (Delphi/FPC)
lThread.Free;
end;
end;
fWake := nil; // release signal
fCancel := nil; // release token
fLock.Free;
inherited Destroy;
end;
function TThreadedTimer.GetInterval: Cardinal;
begin
fLock.Acquire;
try
Result := fIntervalMs;
finally
fLock.Release;
end;
end;
procedure TThreadedTimer.SetInterval(const aValue: Cardinal);
var
lValue: Cardinal;
begin
lValue := aValue;
if lValue = 0 then
lValue := 1;
fLock.Acquire;
try
if fIntervalMs = lValue then Exit;
fIntervalMs := lValue;
finally
fLock.Release;
end;
Kick; // wake so the new interval applies immediately
end;
procedure TThreadedTimer.SetEnabled(const aValue: Boolean);
begin
fLock.Acquire;
try
if fEnabled = aValue then Exit;
fEnabled := aValue;
finally
fLock.Release;
end;
Kick; // wake to apply state change now
end;
function TThreadedTimer.GetMode: TTimerMode;
begin
fLock.Acquire;
try
Result := fMode;
finally
fLock.Release;
end;
end;
procedure TThreadedTimer.SetMode(const aValue: TTimerMode);
begin
fLock.Acquire;
try
if fMode = aValue then Exit;
fMode := aValue;
finally
fLock.Release;
end;
Kick; // re-align cadence immediately
end;
procedure TThreadedTimer.SetOnTimer(const aValue: TNotifyEvent);
begin
fLock.Acquire;
try
fOnTimer := aValue;
finally
fLock.Release;
end;
Kick; // wake so the new handler is seen immediately
end;
function TThreadedTimer.GetTag: NativeInt;
begin
fLock.Acquire;
try
Result := fTag;
finally
fLock.Release;
end;
end;
procedure TThreadedTimer.SetTag(const aValue: NativeInt);
begin
fLock.Acquire;
try
fTag := aValue;
finally
fLock.Release;
end;
end;
function TThreadedTimer.GetDataPtr: Pointer;
begin
fLock.Acquire;
try
Result := fDataPtr;
finally
fLock.Release;
end;
end;
procedure TThreadedTimer.SetDataPtr(const aValue: Pointer);
begin
fLock.Acquire;
try
fDataPtr := aValue;
finally
fLock.Release;
end;
end;
function TThreadedTimer.GetDataIntf: IInterface;
begin
fLock.Acquire;
try
Result := fDataIntf;
finally
fLock.Release;
end;
end;
procedure TThreadedTimer.SetDataIntf(const aValue: IInterface);
begin
fLock.Acquire;
try
fDataIntf := aValue; // interface refcounting handled automatically
finally
fLock.Release;
end;
// no Kick needed
end;
procedure TThreadedTimer.Kick;
begin
if fWake <> nil then
fWake.SetSignaled;
end;
{ TPortableTimer }
constructor TPortableTimer.Create;
begin
inherited Create(nil);
end;
procedure TPortableTimer.Start(const aIntervalMs: Cardinal);
begin
Interval := aIntervalMs;
Enabled := True;
end;
procedure TPortableTimer.Stop;
begin
Enabled := False;
end;
end.