Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TTimer.cxx
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Fons Rademakers 28/11/96
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TTimer
13\ingroup Base
14
15Handles synchronous and a-synchronous timer events.
161. synchronous timer is registered into TSystem and is processed
17 within the standard ROOT event-loop.
182. asynchronous timer is passed to the operating system which sends
19 an external signal to ROOT and thus interrupts its event-loop.
20
21You can use this class in one of the following ways:
22 - Sub-class TTimer and override the Notify() method.
23 - Re-implement the TObject::HandleTimer() method in your class
24 and pass a pointer to this object to timer, see the SetObject()
25 method.
26 - Pass an interpreter command to timer, see SetCommand() method.
27 - Create a TTimer, connect its Timeout() signal to the
28 appropriate methods. Then when the time is up it will emit a
29 Timeout() signal and call connected slots.
30
31Minimum timeout interval is defined in TSystem::ESysConstants as
32`kItimerResolution` (currently 10 ms).
33
34Signal/slots example:
35~~~{.cpp}
36 TTimer *timer = new TTimer();
37 timer->Connect("Timeout()", "myObjectClassName",
38 myObject, "TimerDone()");
39 timer->Start(2000, kTRUE); // 2 seconds single-shot
40~~~
41To emit the Timeout signal repeatedly with minimum timeout:
42~~~ {.cpp}
43 timer->Start(0, kFALSE);
44~~~
45*/
46
47#include "TTimer.h"
48#include "TSystem.h"
49#include "TROOT.h"
50
51
52
53class TSingleShotCleaner : public TTimer {
54private:
56public:
59 {
61 delete fGarbage;
62 }
63 void TurnOn() override
64 {
65 TObject *obj = (TObject *)gTQSender;
66 fGarbage->Add(obj);
67 Reset();
68 if (gSystem)
69 gSystem->AddTimer(this);
70 }
71 Bool_t Notify() override
72 {
74 Reset();
75 if (gSystem)
76 gSystem->RemoveTimer(this);
77 return kTRUE;
78 }
79};
80
81////////////////////////////////////////////////////////////////////////////////
82/// Create timer that times out in ms milliseconds. If milliSec is 0
83/// then the timeout will be the minimum timeout (see TSystem::ESysConstants,
84/// i.e. 10 ms). If mode == kTRUE then the timer is synchronous else
85/// a-synchronous. The default is synchronous. Add a timer to the system
86/// eventloop by calling TurnOn(). Set command to be executed from Notify()
87/// or set the object whose HandleTimer() method will be called via Notify(),
88/// derive from TTimer and override Notify() or connect slots to the
89/// signals Timeout(), TurnOn() and TurnOff().
90
92{
93 fObject = nullptr;
94 fCommand = "";
95 fSync = mode;
97 Reset();
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Create timer that times out in ms milliseconds. If mode == kTRUE then
102/// the timer is synchronous else a-synchronous. The default is synchronous.
103/// Add a timer to the system eventloop by calling TurnOn().
104/// The object's HandleTimer() will be called by Notify().
105
107{
108 fObject = obj;
109 fCommand = "";
110 fSync = mode;
112 Reset();
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Create timer that times out in ms milliseconds. If mode == kTRUE then
117/// the timer is synchronous else a-synchronous. The default is synchronous.
118/// Add a timer to the system eventloop by calling TurnOn().
119/// The interpreter will execute command from Notify().
120
121TTimer::TTimer(const char *command, Long_t ms, Bool_t mode) : fTime(ms)
122{
123 fObject = nullptr;
125 fSync = mode;
127 Reset();
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Check if timer timed out.
132
134{
135 if (fAbsTime <= now) {
136 fTimeout = kTRUE;
137 Notify();
138 return kTRUE;
139 }
140 return kFALSE;
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Notify when timer times out. The timer is always reset. To stop
145/// the timer call TurnOff(). Make sure to call Reset() also in derived
146/// Notify() so timers will keep working repeatedly.
147
149{
150 Timeout(); // emit Timeout() signal
151 if (fObject) fObject->HandleTimer(this);
152 if (fCommand && fCommand.Length() > 0)
153 gROOT->ProcessLine(fCommand);
154
155 Reset();
156 return kTRUE;
157}
158
159////////////////////////////////////////////////////////////////////////////////
160/// Reset the timer.
161
163{
164 // make sure gSystem exists
166
168 fAbsTime = fTime;
169 if (gSystem) {
170 fAbsTime += gSystem->Now();
171 if (!fSync) gSystem->ResetTimer(this);
172 }
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Set the interpreter command to be executed at time out. Removes the
177/// object to be notified (if it was set).
178
180{
181 fObject = nullptr;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Set the object to be notified at time out. Removes the command to
187/// be executed (if it was set).
188
190{
191 fObject = object;
192 fCommand = "";
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// When the argument is true the a-synchronous timer (SIGALRM) signal
197/// handler is set so that interrupted syscalls will not be restarted
198/// by the kernel. This is typically used in case one wants to put a
199/// timeout on an I/O operation. By default interrupted syscalls will
200/// be restarted.
201
203{
204 fIntSyscalls = set;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// Starts the timer with a milliSec timeout. If milliSec is 0
209/// then the timeout will be the minimum timeout (see TSystem::ESysConstants,
210/// i.e. 10 ms), if milliSec is -1 then the time interval as previously
211/// specified (in ctor or SetTime()) will be used.
212/// If singleShot is kTRUE, the timer will be activated only once,
213/// otherwise it will continue until it is stopped.
214/// See also TurnOn(), Stop(), TurnOff().
215
217{
218 if (milliSec >= 0)
220 Reset();
221 TurnOn();
222 if (singleShot)
223 Connect(this, "Timeout()", "TTimer", this, "TurnOff()");
224 else
225 Disconnect(this, "Timeout()", this, "TurnOff()");
226}
227
228////////////////////////////////////////////////////////////////////////////////
229/// Remove timer from system timer list. This requires that a timer
230/// has been placed in the system timer list (using TurnOn()).
231/// If a TTimer subclass is placed on another list, override TurnOff() to
232/// remove the timer from the correct list.
233
235{
236 if (gSystem)
237 if (gSystem->RemoveTimer(this))
238 Emit("TurnOff()");
239}
240
241////////////////////////////////////////////////////////////////////////////////
242/// Add the timer to the system timer list. If a TTimer subclass has to be
243/// placed on another list, override TurnOn() to add the timer to the correct
244/// list.
245
247{
248 // might have been set in a previous Start()
249 Disconnect(this, "Timeout()", this, "TurnOff()");
250
251 if (gSystem) {
252 gSystem->AddTimer(this);
253 Emit("TurnOn()");
254 }
255}
256
257////////////////////////////////////////////////////////////////////////////////
258/// This static function calls a slot after a given time interval.
259/// Created internal timer will be deleted after that.
260
262 void *receiver, const char *method)
263{
267
268 static TSingleShotCleaner singleShotCleaner; // single shot timer cleaner
269
270 // gSingleShotCleaner will delete singleShotTimer a
271 // short period after Timeout() signal is emitted
273 "TTimer", &singleShotCleaner, "TurnOn()");
274
276}
277
278////////////////////////////////////////////////////////////////////////////////
279/// This function checks if the timer is running within gSystem
280/// (Has been started and did not finish yet).
281
283{
285 return gSystem->GetListOfTimers()->IndexOf(this) != -1;
286 return false;
287}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char mode
R__EXTERN void * gTQSender
Definition TQObject.h:46
#define gROOT
Definition TROOT.h:411
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:467
Mother of all ROOT objects.
Definition TObject.h:41
virtual Bool_t HandleTimer(TTimer *timer)
Execute action in response of a timer timing out.
Definition TObject.cxx:511
void Emit(const char *signal, const T &arg)
Activate signal with single parameter.
Definition TQObject.h:164
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition TQObject.cxx:865
Bool_t Disconnect(const char *signal=nullptr, void *receiver=nullptr, const char *slot=nullptr)
Disconnects signal of this object from slot of receiver.
void TurnOn() override
Add the timer to the system timer list.
Definition TTimer.cxx:63
~TSingleShotCleaner() override
Definition TTimer.cxx:58
Bool_t Notify() override
This method must be overridden to handle object notification (the base implementation is no-op).
Definition TTimer.cxx:71
Ssiz_t Length() const
Definition TString.h:425
virtual void ResetTimer(TTimer *)
Definition TSystem.h:406
virtual TList * GetListOfTimers() const
Definition TSystem.h:403
virtual TTime Now()
Get current time in milliseconds since 0:00 Jan 1 1995.
Definition TSystem.cxx:461
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition TSystem.cxx:469
virtual TTimer * RemoveTimer(TTimer *t)
Remove timer from list of system timers.
Definition TSystem.cxx:479
Basic time type with millisecond precision.
Definition TTime.h:27
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
virtual void TurnOff()
Remove timer from system timer list.
Definition TTimer.cxx:234
virtual void Start(Long_t milliSec=-1, Bool_t singleShot=kFALSE)
Starts the timer with a milliSec timeout.
Definition TTimer.cxx:216
virtual void TurnOn()
Add the timer to the system timer list.
Definition TTimer.cxx:246
void SetCommand(const char *command)
Set the interpreter command to be executed at time out.
Definition TTimer.cxx:179
TString fCommand
Definition TTimer.h:61
void SetInterruptSyscalls(Bool_t set=kTRUE)
When the argument is true the a-synchronous timer (SIGALRM) signal handler is set so that interrupted...
Definition TTimer.cxx:202
TTime fTime
Definition TTimer.h:54
void Reset()
Reset the timer.
Definition TTimer.cxx:162
Bool_t CheckTimer(const TTime &now)
Check if timer timed out.
Definition TTimer.cxx:133
void SetObject(TObject *object)
Set the object to be notified at time out.
Definition TTimer.cxx:189
static void SingleShot(Int_t milliSec, const char *receiver_class, void *receiver, const char *method)
This static function calls a slot after a given time interval.
Definition TTimer.cxx:261
TObject * fObject
Definition TTimer.h:60
void SetTime(Long_t milliSec)
Definition TTimer.h:91
TTime fAbsTime
Definition TTimer.h:55
Bool_t fTimeout
Definition TTimer.h:56
TTimer(const TTimer &)=delete
Bool_t Notify() override
Notify when timer times out.
Definition TTimer.cxx:148
Bool_t fSync
Definition TTimer.h:57
Bool_t IsRunning()
This function checks if the timer is running within gSystem (Has been started and did not finish yet)...
Definition TTimer.cxx:282
Bool_t fIntSyscalls
Definition TTimer.h:58
virtual void Timeout()
Definition TTimer.h:97
TROOT * GetROOT()
Definition TROOT.cxx:477