ROOT  6.06/09
Reference Guide
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 Handles synchronous and a-synchronous timer events.
14 1. synchronous timer is registered into TSystem and is processed
15  within the standard ROOT event-loop.
16 2. asynchronous timer is passed to the operating system which sends
17  an external signal to ROOT and thus interrupts its event-loop.
18 
19 You can use this class in one of the following ways:
20  - Sub-class TTimer and override the Notify() method.
21  - Re-implement the TObject::HandleTimer() method in your class
22  and pass a pointer to this object to timer, see the SetObject()
23  method.
24  - Pass an interpreter command to timer, see SetCommand() method.
25  - Create a TTimer, connect its Timeout() signal to the
26  appropriate methods. Then when the time is up it will emit a
27  Timeout() signal and call connected slots.
28 
29 Minimum timeout interval is defined in TSystem::ESysConstants as
30 `kItimerResolution` (currently 10 ms).
31 
32 Signal/slots example:
33 ~~~{.cpp}
34  TTimer *timer = new TTimer();
35  timer->Connect("Timeout()", "myObjectClassName",
36  myObject, "TimerDone()");
37  timer->Start(2000, kTRUE); // 2 seconds single-shot
38 ~~~
39 To emit the Timeout signal repeatedly with minimum timeout:
40 ~~~ {.cpp}
41  timer->Start(0, kFALSE);
42 ~~~
43 */
44 
45 #include "TTimer.h"
46 #include "TSystem.h"
47 #include "TROOT.h"
48 
50 
51 
52 class TSingleShotCleaner : public TTimer {
53 private:
54  TList *fGarbage;
55 public:
56  TSingleShotCleaner() : TTimer(10, kTRUE) { fGarbage = new TList(); }
57  virtual ~TSingleShotCleaner() { fGarbage->Delete(); delete fGarbage; }
58  void TurnOn() {
60  fGarbage->Add(obj);
61  Reset();
62  if (gSystem)
63  gSystem->AddTimer(this);
64  }
66  fGarbage->Delete();
67  Reset();
68  if (gSystem)
69  gSystem->RemoveTimer(this);
70  return kTRUE;
71  }
72 };
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Create timer that times out in ms milliseconds. If milliSec is 0
76 /// then the timeout will be the minimum timeout (see TSystem::ESysConstants,
77 /// i.e. 10 ms). If mode == kTRUE then the timer is synchronous else
78 /// a-synchronous. The default is synchronous. Add a timer to the system
79 /// eventloop by calling TurnOn(). Set command to be executed from Notify()
80 /// or set the object whose HandleTimer() method will be called via Notify(),
81 /// derive from TTimer and override Notify() or connect slots to the
82 /// signals Timeout(), TurnOn() and TurnOff().
83 
84 TTimer::TTimer(Long_t ms, Bool_t mode) : fTime(ms)
85 {
86  fObject = 0;
87  fCommand = "";
88  fSync = mode;
90  Reset();
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Create timer that times out in ms milliseconds. If mode == kTRUE then
95 /// the timer is synchronous else a-synchronous. The default is synchronous.
96 /// Add a timer to the system eventloop by calling TurnOn().
97 /// The object's HandleTimer() will be called by Notify().
98 
99 TTimer::TTimer(TObject *obj, Long_t ms, Bool_t mode) : fTime(ms)
100 {
101  fObject = obj;
102  fCommand = "";
103  fSync = mode;
105  Reset();
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Create timer that times out in ms milliseconds. If mode == kTRUE then
110 /// the timer is synchronous else a-synchronous. The default is synchronous.
111 /// Add a timer to the system eventloop by calling TurnOn().
112 /// The interpreter will execute command from Notify().
113 
114 TTimer::TTimer(const char *command, Long_t ms, Bool_t mode) : fTime(ms)
115 {
116  fObject = 0;
117  fCommand = command;
118  fSync = mode;
120  Reset();
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Check if timer timed out.
125 
127 {
128  if (fAbsTime <= now) {
129  fTimeout = kTRUE;
130  Notify();
131  return kTRUE;
132  }
133  return kFALSE;
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Notify when timer times out. The timer is always reset. To stop
138 /// the timer call TurnOff(). Make sure to call Reset() also in derived
139 /// Notify() so timers will keep working repeatedly.
140 
142 {
143  Timeout(); // emit Timeout() signal
144  if (fObject) fObject->HandleTimer(this);
145  if (fCommand && fCommand.Length() > 0)
146  gROOT->ProcessLine(fCommand);
147 
148  Reset();
149  return kTRUE;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Reset the timer.
154 
156 {
157  // make sure gSystem exists
158  ROOT::GetROOT();
159 
160  fTimeout = kFALSE;
161  fAbsTime = fTime;
162  if (gSystem) {
163  fAbsTime += gSystem->Now();
164  if (!fSync) gSystem->ResetTimer(this);
165  }
166 }
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// Set the interpreter command to be executed at time out. Removes the
170 /// object to be notified (if it was set).
171 
172 void TTimer::SetCommand(const char *command)
173 {
174  fObject = 0;
175  fCommand = command;
176 }
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Set the object to be notified at time out. Removes the command to
180 /// be executed (if it was set).
181 
183 {
184  fObject = object;
185  fCommand = "";
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// When the argument is true the a-synchronous timer (SIGALRM) signal
190 /// handler is set so that interrupted syscalls will not be restarted
191 /// by the kernel. This is typically used in case one wants to put a
192 /// timeout on an I/O operation. By default interrupted syscalls will
193 /// be restarted.
194 
196 {
197  fIntSyscalls = set;
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Starts the timer with a milliSec timeout. If milliSec is 0
202 /// then the timeout will be the minimum timeout (see TSystem::ESysConstants,
203 /// i.e. 10 ms), if milliSec is -1 then the time interval as previously
204 /// specified (in ctor or SetTime()) will be used.
205 /// If singleShot is kTRUE, the timer will be activated only once,
206 /// otherwise it will continue until it is stopped.
207 /// See also TurnOn(), Stop(), TurnOff().
208 
209 void TTimer::Start(Long_t milliSec, Bool_t singleShot)
210 {
211  if (milliSec >= 0)
212  SetTime(milliSec);
213  Reset();
214  TurnOn();
215  if (singleShot)
216  Connect(this, "Timeout()", "TTimer", this, "TurnOff()");
217  else
218  Disconnect(this, "Timeout()", this, "TurnOff()");
219 }
220 
221 ////////////////////////////////////////////////////////////////////////////////
222 /// Remove timer from system timer list. This requires that a timer
223 /// has been placed in the system timer list (using TurnOn()).
224 /// If a TTimer subclass is placed on another list, override TurnOff() to
225 /// remove the timer from the correct list.
226 
228 {
229  if (gSystem)
230  if (gSystem->RemoveTimer(this))
231  Emit("TurnOff()");
232 }
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 /// Add the timer to the system timer list. If a TTimer subclass has to be
236 /// placed on another list, override TurnOn() to add the timer to the correct
237 /// list.
238 
240 {
241  // might have been set in a previous Start()
242  Disconnect(this, "Timeout()", this, "TurnOff()");
243 
244  if (gSystem) {
245  gSystem->AddTimer(this);
246  Emit("TurnOn()");
247  }
248 }
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// This static function calls a slot after a given time interval.
252 /// Created internal timer will be deleted after that.
253 
254 void TTimer::SingleShot(Int_t milliSec, const char *receiver_class,
255  void *receiver, const char *method)
256 {
257  TTimer *singleShotTimer = new TTimer(milliSec);
258  TQObject::Connect(singleShotTimer, "Timeout()",
259  receiver_class, receiver, method);
260 
261  static TSingleShotCleaner singleShotCleaner; // single shot timer cleaner
262 
263  // gSingleShotCleaner will delete singleShotTimer a
264  // short period after Timeout() signal is emitted
265  TQObject::Connect(singleShotTimer, "Timeout()",
266  "TTimer", &singleShotCleaner, "TurnOn()");
267 
268  singleShotTimer->Start(milliSec, kTRUE);
269 }
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
void Reset()
Reset the timer.
Definition: TTimer.cxx:155
Ssiz_t Length() const
Definition: TString.h:390
R__EXTERN void * gTQSender
Definition: TQObject.h:49
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:195
Bool_t Notify()
Definition: TTimer.cxx:65
#define gROOT
Definition: TROOT.h:340
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:254
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual TTimer * RemoveTimer(TTimer *t)
Remove timer from list of system timers.
Definition: TSystem.cxx:485
const Bool_t kFALSE
Definition: Rtypes.h:92
Basic time type with millisecond precision.
Definition: TTime.h:29
TSingleShotCleaner()
Definition: TTimer.cxx:56
TString fCommand
Definition: TTimer.h:67
Bool_t fTimeout
Definition: TTimer.h:62
virtual void Start(Long_t milliSec=-1, Bool_t singleShot=kFALSE)
Starts the timer with a milliSec timeout.
Definition: TTimer.cxx:209
virtual Bool_t HandleTimer(TTimer *timer)
Execute action in response of a timer timing out.
Definition: TObject.cxx:469
Bool_t fIntSyscalls
Definition: TTimer.h:64
void Emit(const char *signal)
Acitvate signal without args.
Definition: TQObject.cxx:559
virtual Bool_t Notify()
Notify when timer times out.
Definition: TTimer.cxx:141
A doubly linked list.
Definition: TList.h:47
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:1135
virtual TTime Now()
Get current time in milliseconds since 0:00 Jan 1 1995.
Definition: TSystem.cxx:467
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual void TurnOff()
Remove timer from system timer list.
Definition: TTimer.cxx:227
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:57
Bool_t CheckTimer(const TTime &now)
Check if timer timed out.
Definition: TTimer.cxx:126
void Reset(Detail::TBranchProxy *x)
TROOT * GetROOT()
Definition: TROOT.cxx:375
virtual void ResetTimer(TTimer *)
Definition: TSystem.h:388
Bool_t fSync
Definition: TTimer.h:63
void SetCommand(const char *command)
Set the interpreter command to be executed at time out.
Definition: TTimer.cxx:172
long Long_t
Definition: RtypesCore.h:50
virtual ~TSingleShotCleaner()
Definition: TTimer.cxx:57
#define ClassImp(name)
Definition: Rtypes.h:279
TTimer(const TTimer &)
Bool_t Disconnect(const char *signal=0, void *receiver=0, const char *slot=0)
Disconnects signal of this object from slot of receiver.
Definition: TQObject.cxx:1293
Mother of all ROOT objects.
Definition: TObject.h:58
TTime fTime
Definition: TTimer.h:60
virtual void Add(TObject *obj)
Definition: TList.h:81
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition: TSystem.cxx:475
void SetObject(TObject *object)
Set the object to be notified at time out.
Definition: TTimer.cxx:182
virtual void Timeout()
Definition: TTimer.h:102
void TurnOn()
Definition: TTimer.cxx:58
TObject * fObject
Definition: TTimer.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:239
TTime fAbsTime
Definition: TTimer.h:61
void SetTime(Long_t milliSec)
Definition: TTimer.h:96