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
52
53
54class TSingleShotCleaner : public TTimer {
55private:
57public:
59 virtual ~TSingleShotCleaner() { fGarbage->Delete(); delete fGarbage; }
60 void TurnOn() override
61 {
62 TObject *obj = (TObject *)gTQSender;
63 fGarbage->Add(obj);
64 Reset();
65 if (gSystem)
66 gSystem->AddTimer(this);
67 }
68 Bool_t Notify() override
69 {
71 Reset();
72 if (gSystem)
73 gSystem->RemoveTimer(this);
74 return kTRUE;
75 }
76};
77
78////////////////////////////////////////////////////////////////////////////////
79/// Create timer that times out in ms milliseconds. If milliSec is 0
80/// then the timeout will be the minimum timeout (see TSystem::ESysConstants,
81/// i.e. 10 ms). If mode == kTRUE then the timer is synchronous else
82/// a-synchronous. The default is synchronous. Add a timer to the system
83/// eventloop by calling TurnOn(). Set command to be executed from Notify()
84/// or set the object whose HandleTimer() method will be called via Notify(),
85/// derive from TTimer and override Notify() or connect slots to the
86/// signals Timeout(), TurnOn() and TurnOff().
87
89{
90 fObject = nullptr;
91 fCommand = "";
92 fSync = mode;
94 Reset();
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Create timer that times out in ms milliseconds. If mode == kTRUE then
99/// the timer is synchronous else a-synchronous. The default is synchronous.
100/// Add a timer to the system eventloop by calling TurnOn().
101/// The object's HandleTimer() will be called by Notify().
102
104{
105 fObject = obj;
106 fCommand = "";
107 fSync = mode;
109 Reset();
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Create timer that times out in ms milliseconds. If mode == kTRUE then
114/// the timer is synchronous else a-synchronous. The default is synchronous.
115/// Add a timer to the system eventloop by calling TurnOn().
116/// The interpreter will execute command from Notify().
117
118TTimer::TTimer(const char *command, Long_t ms, Bool_t mode) : fTime(ms)
119{
120 fObject = nullptr;
121 fCommand = command;
122 fSync = mode;
124 Reset();
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Check if timer timed out.
129
131{
132 if (fAbsTime <= now) {
133 fTimeout = kTRUE;
134 Notify();
135 return kTRUE;
136 }
137 return kFALSE;
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Notify when timer times out. The timer is always reset. To stop
142/// the timer call TurnOff(). Make sure to call Reset() also in derived
143/// Notify() so timers will keep working repeatedly.
144
146{
147 Timeout(); // emit Timeout() signal
148 if (fObject) fObject->HandleTimer(this);
149 if (fCommand && fCommand.Length() > 0)
150 gROOT->ProcessLine(fCommand);
151
152 Reset();
153 return kTRUE;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Reset the timer.
158
160{
161 // make sure gSystem exists
163
165 fAbsTime = fTime;
166 if (gSystem) {
167 fAbsTime += gSystem->Now();
168 if (!fSync) gSystem->ResetTimer(this);
169 }
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Set the interpreter command to be executed at time out. Removes the
174/// object to be notified (if it was set).
175
176void TTimer::SetCommand(const char *command)
177{
178 fObject = nullptr;
179 fCommand = command;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Set the object to be notified at time out. Removes the command to
184/// be executed (if it was set).
185
187{
188 fObject = object;
189 fCommand = "";
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// When the argument is true the a-synchronous timer (SIGALRM) signal
194/// handler is set so that interrupted syscalls will not be restarted
195/// by the kernel. This is typically used in case one wants to put a
196/// timeout on an I/O operation. By default interrupted syscalls will
197/// be restarted.
198
200{
201 fIntSyscalls = set;
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Starts the timer with a milliSec timeout. If milliSec is 0
206/// then the timeout will be the minimum timeout (see TSystem::ESysConstants,
207/// i.e. 10 ms), if milliSec is -1 then the time interval as previously
208/// specified (in ctor or SetTime()) will be used.
209/// If singleShot is kTRUE, the timer will be activated only once,
210/// otherwise it will continue until it is stopped.
211/// See also TurnOn(), Stop(), TurnOff().
212
213void TTimer::Start(Long_t milliSec, Bool_t singleShot)
214{
215 if (milliSec >= 0)
216 SetTime(milliSec);
217 Reset();
218 TurnOn();
219 if (singleShot)
220 Connect(this, "Timeout()", "TTimer", this, "TurnOff()");
221 else
222 Disconnect(this, "Timeout()", this, "TurnOff()");
223}
224
225////////////////////////////////////////////////////////////////////////////////
226/// Remove timer from system timer list. This requires that a timer
227/// has been placed in the system timer list (using TurnOn()).
228/// If a TTimer subclass is placed on another list, override TurnOff() to
229/// remove the timer from the correct list.
230
232{
233 if (gSystem)
234 if (gSystem->RemoveTimer(this))
235 Emit("TurnOff()");
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Add the timer to the system timer list. If a TTimer subclass has to be
240/// placed on another list, override TurnOn() to add the timer to the correct
241/// list.
242
244{
245 // might have been set in a previous Start()
246 Disconnect(this, "Timeout()", this, "TurnOff()");
247
248 if (gSystem) {
249 gSystem->AddTimer(this);
250 Emit("TurnOn()");
251 }
252}
253
254////////////////////////////////////////////////////////////////////////////////
255/// This static function calls a slot after a given time interval.
256/// Created internal timer will be deleted after that.
257
258void TTimer::SingleShot(Int_t milliSec, const char *receiver_class,
259 void *receiver, const char *method)
260{
261 TTimer *singleShotTimer = new TTimer(milliSec);
262 TQObject::Connect(singleShotTimer, "Timeout()",
263 receiver_class, receiver, method);
264
265 static TSingleShotCleaner singleShotCleaner; // single shot timer cleaner
266
267 // gSingleShotCleaner will delete singleShotTimer a
268 // short period after Timeout() signal is emitted
269 TQObject::Connect(singleShotTimer, "Timeout()",
270 "TTimer", &singleShotCleaner, "TurnOn()");
271
272 singleShotTimer->Start(milliSec, kTRUE);
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// This function checks if the timer is running within gSystem
277/// (Has been started and did not finish yet).
278
280{
282 return gSystem->GetListOfTimers()->IndexOf(this) != -1;
283 return false;
284}
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t TPoint TPoint const char mode
R__EXTERN void * gTQSender
Definition TQObject.h:46
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
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:468
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:498
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:869
Bool_t Disconnect(const char *signal=nullptr, void *receiver=nullptr, const char *slot=nullptr)
Disconnects signal of this object from slot of receiver.
virtual Int_t IndexOf(const TObject *obj) const
Return index of object in collection.
virtual ~TSingleShotCleaner()
Definition TTimer.cxx:59
void TurnOn() override
Add the timer to the system timer list.
Definition TTimer.cxx:60
Bool_t Notify() override
This method must be overridden to handle object notification (the base implementation is no-op).
Definition TTimer.cxx:68
Ssiz_t Length() const
Definition TString.h:417
virtual void ResetTimer(TTimer *)
Definition TSystem.h:396
virtual TList * GetListOfTimers() const
Definition TSystem.h:393
virtual TTime Now()
Get current time in milliseconds since 0:00 Jan 1 1995.
Definition TSystem.cxx:463
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition TSystem.cxx:471
virtual TTimer * RemoveTimer(TTimer *t)
Remove timer from list of system timers.
Definition TSystem.cxx:481
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:231
virtual void Start(Long_t milliSec=-1, Bool_t singleShot=kFALSE)
Starts the timer with a milliSec timeout.
Definition TTimer.cxx:213
virtual void TurnOn()
Add the timer to the system timer list.
Definition TTimer.cxx:243
void SetCommand(const char *command)
Set the interpreter command to be executed at time out.
Definition TTimer.cxx:176
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:199
TTime fTime
Definition TTimer.h:54
void Reset()
Reset the timer.
Definition TTimer.cxx:159
Bool_t CheckTimer(const TTime &now)
Check if timer timed out.
Definition TTimer.cxx:130
void SetObject(TObject *object)
Set the object to be notified at time out.
Definition TTimer.cxx:186
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:258
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:145
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:279
Bool_t fIntSyscalls
Definition TTimer.h:58
virtual void Timeout()
Definition TTimer.h:97
TROOT * GetROOT()
Definition TROOT.cxx:472