-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimpleTimer.pas
520 lines (438 loc) · 16.6 KB
/
SimpleTimer.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
Simple timer
Simple non-visual interval timer.
In Windows, it uses standard timer (SetTimer, KillTimer) that signals
timeout using windows message (WM_TIMER). The on-timer event is called
directly from handler of this message.
In main thread, the messages are dispatched automatically, but if the
timer is created in non-main thread, the method ProcessMessages must be
called to process timer message and fire the on-timer event.
In Linux, it is based around POSIX interval timer (timer_create). The
timeout is handled using signals, but the on-timer event is NOT called
from signal handler. Instead, signal handler only sets a flag indicating
timeout, and event is called from ProcessMessages method.
In GUI application, if the timer is created in the main thread, method
ProcessMessages is called automatically from application's on-idle event.
In non-main thread, you are responsible to call this method - so the
behavior is technically the same as in Windows OS.
Version 1.2.4 (2024-08-19)
Last change 2024-08-23
©2015-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.SimpleTimer
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* UtilitySignal - github.com/TheLazyTomcat/Lib.UtilitySignal
* UtilityWindow - github.com/TheLazyTomcat/Lib.UtilityWindow
Library AuxExceptions is required only when rebasing local exception classes
(see symbol SimpleTimer_UseAuxExceptions for details).
Library UtilitySignal is required only when compiling for Linux OS.
Library UtilityWindow is required only when compiling for Windows OS.
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
InterlockedOps - github.com/TheLazyTomcat/Lib.InterlockedOps
MulticastEvent - github.com/TheLazyTomcat/Lib.MulticastEvent
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
WndAlloc - github.com/TheLazyTomcat/Lib.WndAlloc
===============================================================================}
unit SimpleTimer;
{
SimpleTimer_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
SimpleTimer_UseAuxExceptions to achieve this.
}
{$IF Defined(SimpleTimer_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported operating system.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH ClassicProcVars+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
{$IFDEF Windows}Windows, Messages,{$ENDIF} SysUtils,
AuxTypes, AuxClasses{$IFDEF Windows}, UtilityWindow{$ENDIF}
{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ESTException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESTTimerSetupError = class(ESTException);
{$IFNDEF Windows}
ESTTimerCreationError = class(ESTException);
ESTTimerDeletionError = class(ESTException);
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TSimpleTimer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSimpleTimer - class declaration
===============================================================================}
type
TSimpleTimer = class(TCustomObject)
protected
{$IFDEF Windows}
fOwnsWindow: Boolean;
fWindow: TUtilityWindow;
{$ELSE}
fTimerExpired: Integer; // only for internal use
fInMainThread: Boolean; // -//-
fTimerCreated: Boolean;
{$ENDIF}
fTimerID: PtrUInt;
fInterval: UInt32;
fEnabled: Boolean;
fTag: Integer;
fOnTimerEvent: TNotifyEvent;
fOnTimerCallback: TNotifyCallback;
procedure SetInterval(Value: UInt32); virtual;
procedure SetEnabled(Value: Boolean); virtual;
{$IFDEF Windows}
procedure Initialize(Window: TUtilityWindow; TimerID: PtrUInt); virtual;
{$ELSE}
procedure Initialize; virtual;
{$ENDIF}
procedure Finalize; virtual;
procedure SetupTimer; virtual;
{$IFDEF Windows}
procedure MessagesHandler(var Msg: TMessage; var Handled: Boolean; Sent: Boolean); virtual;
{$ELSE}
procedure TimerExpired; virtual;
procedure OnAppIdleHandler(Sender: TObject; var Done: Boolean); virtual;
{$ENDIF}
procedure DoOnTimer; virtual;
public
{$IFDEF Windows}
constructor Create(Window: TUtilityWindow = nil; TimerID: PtrUInt = 1);
{$ELSE}
constructor Create;
{$ENDIF}
destructor Destroy; override;
procedure ProcessMessages; virtual;
{$IFDEF Windows}
property OwnsWindow: Boolean read fOwnsWindow;
property Window: TUtilityWindow read fWindow;
{$ENDIF}
property TimerID: PtrUInt read fTimerID;
property Interval: UInt32 read fInterval write SetInterval;
property Enabled: Boolean read fEnabled write SetEnabled;
property Tag: Integer read fTag write fTag;
property OnTimerCallback: TNotifyCallback read fOnTimerCallback write fOnTimerCallback;
property OnTimerEvent: TNotifyEvent read fOnTimerEvent write fOnTimerEvent;
property OnTimer: TNotifyEvent read fOnTimerEvent write fOnTimerEvent;
end;
implementation
{$IFNDEF Windows}
uses
Classes, BaseUnix, Linux{$IFDEF LCL}, Forms{$ENDIF},
UtilitySignal;
{$LINKLIB RT}
{$ENDIF}
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TSimpleTimer
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSimpleTimer - internals
===============================================================================}
{$IFDEF Windows}
const
USER_TIMER_MAXIMUM = $7FFFFFFF;
USER_TIMER_MINIMUM = $0000000A;
{$ELSE}
const
SIGEV_SIGNAL = 0;
SI_TIMER = -2;
type
timer_t = PtrUInt; // in glic it is defined as void * (so pointer)
ptimer_t = ^timer_t;
sigval_t = record
case Integer of
0: (sigval_int: cint); // Integer value
1: (sigval_ptr: Pointer) // Pointer value
end;
sigevent_t = record
sigev_value: sigval_t; // Data passed with notification
sigev_signo: cint; // Notification signal
sigev_notify: cint; // Notification method
sigev_notify_function: procedure(sigval: sigval_t); cdecl; // Function used for thread notification (SIGEV_THREAD)
sigev_notify_attributes: Pointer; // Attributes for notification thread (SIGEV_THREAD)
end;
psigevent_t = ^sigevent_t;
timespec_t = record
tv_sec: time_t;
tv_nsec: clong;
end;
itimerspec_t = record
it_interval: timespec_t; // Timer interval
it_value: timespec_t; // Initial expiration
end;
pitimerspec_t = ^itimerspec_t;
Function timer_create(clockid: clockid_t; sevp: psigevent_t; timerid: ptimer_t): cint; cdecl; external;
Function timer_delete(timerid: timer_t): cint; cdecl; external;
Function timer_settime(timerid: timer_t; flags: cint; new_value,old_value: pitimerspec_t): cint; cdecl; external;
Function errno_ptr: pcint; cdecl; external name '__errno_location';
{-------------------------------------------------------------------------------
TSimpleTimer - signal handler
-------------------------------------------------------------------------------}
procedure SignalHandler(const Info: TUSSignalInfo; var BreakProcessing: Boolean);
begin
If Assigned(Info.Value.PtrValue) then
(TObject(Info.Value.PtrValue) as TSimpleTimer).TimerExpired;
BreakProcessing := False;
end;
//------------------------------------------------------------------------------
procedure RegisterSignalHandler;
begin
UtilitySignal.RegisterHandler(SI_TIMER,SignalHandler);
end;
//------------------------------------------------------------------------------
procedure UnregisterSignalHandler;
begin
UtilitySignal.UnregisterHandler(SI_TIMER,SignalHandler);
end;
{$ENDIF}
{===============================================================================
TSimpleTimer - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSimpleTimer - protected methods
-------------------------------------------------------------------------------}
procedure TSimpleTimer.SetInterval(Value: UInt32);
begin
{$IFDEF Windows}
If Value < USER_TIMER_MINIMUM then
fInterval := USER_TIMER_MINIMUM
else If Value > USER_TIMER_MAXIMUM then
fInterval := USER_TIMER_MAXIMUM
else
{$ENDIF}
fInterval := Value;
SetupTimer;
end;
//------------------------------------------------------------------------------
procedure TSimpleTimer.SetEnabled(Value: Boolean);
begin
fEnabled := Value;
SetupTimer;
end;
//------------------------------------------------------------------------------
{$IFDEF Windows}
procedure TSimpleTimer.Initialize(Window: TUtilityWindow; TimerID: PtrUInt);
begin
If Assigned(Window) then
begin
fOwnsWindow := False;
fWindow := Window;
end
else
begin
fOwnsWindow := True;
fWindow := TUtilityWindow.Create;
end;
fWindow.OnMessage.Add(MessagesHandler);
fTimerID := TimerID;
{$ELSE}
procedure TSimpleTimer.Initialize;
var
SignalEvent: sigevent_t;
NewTimerID: timer_t;
begin
InterlockedExchange(fTimerExpired,0);
fInMainThread := MainThreadID = GetCurrentThreadID;
fTimerCreated := False; // just to be sure
// setup and create timer
FillChar(Addr(SignalEvent)^,SizeOf(sigevent_t),0);
SignalEvent.sigev_value.sigval_ptr := Pointer(Self);
SignalEvent.sigev_signo := cint(UtilitySignal.SignalNumber);
SignalEvent.sigev_notify := SIGEV_SIGNAL;
If timer_create(CLOCK_MONOTONIC,@SignalEvent,@NewTimerID) = 0 then
begin
fTimerID := PtrUInt(NewTimerID);
fTimerCreated := True;
end
else raise ESTTimerCreationError.CreateFmt('TSimpleTimer.Initialize: Failed to create timer (%d).',[errno_ptr^]);
{$ENDIF}
// set properties to default values
fInterval := 1000;
fEnabled := False;
fTag := 0;
fOnTimerEvent := nil;
fOnTimerCallback := nil;
end;
//------------------------------------------------------------------------------
procedure TSimpleTimer.Finalize;
begin
fEnabled := False;
{$IFDEF Windows}
If Assigned(fWindow) then
begin
SetupTimer;
If fOwnsWindow then
fWindow.Free
else
fWindow.OnMessage.Remove(MessagesHandler);
end;
{$ELSE}
If fTimerCreated then
begin
SetupTimer;
If timer_delete(timer_t(fTimerID)) <> 0 then
raise ESTTimerDeletionError.CreateFmt('Finalize.Initialize: Failed to delete timer (%d).',[errno_ptr^]);
end;
{
Note that signal handler stays assigned, but it should pose no problem as
the timer is destroyed and will not invoke the signal handler anymore.
}
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TSimpleTimer.SetupTimer;
{$IFDEF Windows}
begin
KillTimer(fWindow.WindowHandle,fTimerID);
If (fInterval > 0) and fEnabled then
If SetTimer(fWindow.WindowHandle,fTimerID,fInterval,nil) = 0 then
raise ESTTimerSetupError.CreateFmt('TSimpleTimer.SetupTimer: Failed to setup timer (0x%.8x).',[GetLastError]);
{$ELSE}
var
TimerTime: itimerspec_t;
begin
// disarm timer
FillChar(Addr(TimerTime)^,SizeOf(itimerspec_t),0);
If timer_settime(timer_t(fTimerID),0,@TimerTime,nil) <> 0 then
raise ESTTimerSetupError.CreateFmt('TSimpleTimer.SetupTimer: Failed to disarm timer (%d).',[errno_ptr^]);
{$IFDEF LCL}
If fInMainThread then
Application.RemoveOnIdleHandler(OnAppIdleHandler);
{$ENDIF}
// arm timer
If (fInterval > 0) and fEnabled then
begin
TimerTime.it_interval.tv_sec := fInterval div 1000;
TimerTime.it_interval.tv_nsec := (fInterval mod 1000) * 1000000;
TimerTime.it_value.tv_sec := TimerTime.it_interval.tv_sec;
TimerTime.it_value.tv_nsec := TimerTime.it_interval.tv_nsec;
If timer_settime(timer_t(fTimerID),0,@TimerTime,nil) <> 0 then
raise ESTTimerSetupError.CreateFmt('TSimpleTimer.SetupTimer: Failed to arm timer (%d).',[errno_ptr^]);
{$IFDEF LCL}
If fInMainThread then
Application.AddOnIdleHandler(OnAppIdleHandler,False);
{$ENDIF}
end;
{$ENDIF}
end;
{$IFDEF Windows}
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TSimpleTimer.MessagesHandler(var Msg: TMessage; var Handled: Boolean; Sent: Boolean);
begin
If (Msg.Msg = WM_TIMER) and (PtrUInt(Msg.wParam) = fTimerID) then
begin
DoOnTimer;
Msg.Result := 0;
Handled := True;
end
else Handled := False;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
{$ELSE}
//------------------------------------------------------------------------------
procedure TSimpleTimer.TimerExpired;
begin
InterlockedExchange(fTimerExpired,1);
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TSimpleTimer.OnAppIdleHandler(Sender: TObject; var Done: Boolean);
begin
ProcessMessages;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
{$ENDIF}
//------------------------------------------------------------------------------
procedure TSimpleTimer.DoOnTimer;
begin
If Assigned(fOnTimerEvent) then
fOnTimerEvent(Self)
else If Assigned(fOnTimerCallback) then
fOnTimerCallback(Self);
end;
{-------------------------------------------------------------------------------
TSimpleTimer - public methods
-------------------------------------------------------------------------------}
{$IFDEF Windows}
constructor TSimpleTimer.Create(Window: TUtilityWindow = nil; TimerID: PtrUInt = 1);
{$ELSE}
constructor TSimpleTimer.Create;
{$ENDIF}
begin
inherited Create;
Initialize{$IFDEF Windows}(Window,TimerID){$ENDIF};
end;
//------------------------------------------------------------------------------
destructor TSimpleTimer.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
procedure TSimpleTimer.ProcessMessages;
begin
{$IFDEF Windows}
fWindow.ProcessMessages(False);
{$ELSE}
If InterlockedExchange(fTimerExpired,0) <> 0 then
DoOnTimer;
{$ENDIF}
end;
{===============================================================================
--------------------------------------------------------------------------------
Unit initialization/finalization
--------------------------------------------------------------------------------
===============================================================================}
{$IFNDEF Windows}
initialization
RegisterSignalHandler;
finalization
UnregisterSignalHandler;
{$ENDIF}
end.