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