summaryrefslogtreecommitdiff
path: root/utils/Timer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'utils/Timer.cc')
-rw-r--r--utils/Timer.cc169
1 files changed, 80 insertions, 89 deletions
diff --git a/utils/Timer.cc b/utils/Timer.cc
index 8342854..c2b4d11 100644
--- a/utils/Timer.cc
+++ b/utils/Timer.cc
@@ -26,8 +26,8 @@
#include <sys/time.h>
#include <stdio.h>
-/*!class CcuCoreTimer
-The implementation of \typ{CcuCoreTimer} is a bit tricky, because it tries to be fast.
+/*!class IvlCoreTimer
+The implementation of \typ{IvlCoreTimer} is a bit tricky, because it tries to be fast.
Here is the basic idea: active timers are stored in a list, sorted by increasing time-out
times. When a signal occurs, all timers whose time-out time is smaller than the current
time are notified and removed from the list, then they are inserted again.
@@ -43,15 +43,15 @@ and if we make sure that a timer is flagged as inactive when we insert it, all w
occurences before the right one will be removed.
!*/
-/*?class CcuCoreTimer
+/*?class IvlCoreTimer
Timers are a means to manage or one more periodic tasks in a program.
-The class \typ{CcuCoreTimer} is a base class for all kinds of timers. It provides
+The class \typ{IvlCoreTimer} is a base class for all kinds of timers. It provides
the basic storing and scheduling of timers, as well as a set of member functions
available to the user, but does not provide the actual, system dependent, timing
scheme.
-\typ{CcuCoreTimers} were not designed to be used as is, but only to be derived
+\typ{IvlCoreTimers} were not designed to be used as is, but only to be derived
in order to implement a specific timing scheme.
-For instance, the class \var{CcuBaseTimer} is derived from it and implements
+For instance, the class \var{IvlBaseTimer} is derived from it and implements
signal-based timers (see next section). Building such derived class is very rare,
and it has only be done for timers based on the Unix \com{select} system call,
in the Unix Channel library.
@@ -67,42 +67,41 @@ current (absolute) time when the signal was received from the system. However,
several timers may be triggered at the same time.
If one of them has a slow \fun{Handle}, the time passed to the next ones no more
corrresponds to the current time. If you want to know the exact time, and you are
-not sure of the behaviour of the other timers, it is preferable to use a \typ{CcuTimeStamp}.
+not sure of the behaviour of the other timers, it is preferable to use a \typ{IvlTimeStamp}.
?*/
-/*?class CcuTimerSet
-The class \typ{CcuTimerSet} is the data structure used by the class \typ{CcuCoreTimer}
+/*?class IvlTimerSet
+The class \typ{IvlTimerSet} is the data structure used by the class \typ{IvlCoreTimer}
to manage a set of timers. Each different implementation of timers should work
-with one such timer set, which is passed to the constructor of \typ{CcuCoreTimer}.
+with one such timer set, which is passed to the constructor of \typ{IvlCoreTimer}.
?*/
#ifdef DOC
/*?
-Create an empty \typ{CcuTimerSet}.
+Create an empty \typ{IvlTimerSet}.
?*/
-CcuTimerSet :: CcuTimerSet ()
+IvlTimerSet :: IvlTimerSet ()
{
}
#endif /* DOC */
-
/*!
Remove and return the first active timer in the active timer list.
Timers in that list can actually be inactive.
!*/
/*?hidden?*/
-CcuCoreTimer*
-CcuTimerSet :: ExtractNextActive ()
+IvlCoreTimer*
+IvlTimerSet :: ExtractNextActive ()
{
- CcuCoreTimer* t;
+ IvlCoreTimer* t;
/* all entries of OtherTimers are valid pointers,
hence t == 0 iff OtherTimers is empty */
#ifndef CPLUS_BUG19
- while ((t = OtherTimers.RemoveFirst ()) && (t->StatusFlag != CcuCoreTimer::Active))
+ while ((t = OtherTimers.RemoveFirst ()) && (t->StatusFlag != IvlCoreTimer::Active))
#else
- while ((t = (CcuCoreTimer*) OtherTimers.RemoveFirst ()) && (t->StatusFlag != CcuCoreTimer::Active))
+ while ((t = (IvlCoreTimer*) OtherTimers.RemoveFirst ()) && (t->StatusFlag != IvlCoreTimer::Active))
#endif
;
@@ -124,15 +123,15 @@ or more timer has expired. It decides which timers should be triggered,
and prepares the set of timers for the next call.
?*/
void
-CcuCoreTimer :: Fire (CcuTimerSet* s)
+IvlCoreTimer :: Fire (IvlTimerSet* s)
{
- CcuCoreTimer*& first = s->FirstTimer;
+ IvlCoreTimer*& first = s->FirstTimer;
/* If the first timer is not mature yet, ignore this call */
if (!first)
return;
- CcuTimeStamp now;
+ IvlTimeStamp now;
#if defined(sun) && defined(__svr4__)
/* add tolerance because Solaris sends signals before the expected time (bug #1164919)*/
if (first->NextDate > now + 5) {
@@ -154,7 +153,7 @@ CcuCoreTimer :: Fire (CcuTimerSet* s)
first->Handle (now);
/* handle all other expired timers. */
- CcuCoreTimer* t;
+ IvlCoreTimer* t;
while (t = s->ExtractNextActive ()) {
/* Problem : if one Handle () is long, "now" will then be wrong. */
if (t->NextDate > now) {
@@ -171,7 +170,7 @@ CcuCoreTimer :: Fire (CcuTimerSet* s)
return;
if (t) {
- CcuTimeStamp then;
+ IvlTimeStamp then;
t->SetAlarm (t->NextDate - then);
} else if (first) {
/* we need a valid timer just to be able to call StopAlarm */
@@ -182,12 +181,11 @@ CcuCoreTimer :: Fire (CcuTimerSet* s)
first = t;
}
-
#ifndef CPLUS_BUG19
/*?hidden?*/
int
-CcuCoreTimer :: IsInactive (CcuCoreTimer* t)
+IvlCoreTimer :: IsInactive (IvlCoreTimer* t)
{
return (t->StatusFlag != Active);
}
@@ -196,21 +194,20 @@ CcuCoreTimer :: IsInactive (CcuCoreTimer* t)
/*?hidden?*/
int
-CcuCoreTimer :: IsInactive (CcuListItem* t)
+IvlCoreTimer :: IsInactive (IvlListItem* t)
{
- return (((CcuCoreTimer*) t)->StatusFlag != Active);
+ return (((IvlCoreTimer*) t)->StatusFlag != Active);
}
#endif /* CPLUS_BUG19 */
-
/*?
Create a timer with that will time out every \var{period} milliseconds, and at most
\var{pulses} times. This timer will be managed by the timer set \var{s}.
{\em This constructor is only useful when deriving new timers based on a new
timing scheme.}
?*/
-CcuCoreTimer :: CcuCoreTimer (Millisecond period, int pulses, CcuTimerSet* s)
+IvlCoreTimer :: IvlCoreTimer (Millisecond period, int pulses, IvlTimerSet* s)
: MySet (s),
StatusFlag (Active),
Period (period > 0 ? period : 10),
@@ -218,14 +215,13 @@ CcuCoreTimer :: CcuCoreTimer (Millisecond period, int pulses, CcuTimerSet* s)
{
}
-
/*?nodoc?*/
-CcuCoreTimer :: ~CcuCoreTimer ()
+IvlCoreTimer :: ~IvlCoreTimer ()
{
/* the timer has to be stopped in the derived class */
/* Remove all entries pointing to inactive timers, including this one */
if (MySet)
- MySet->OtherTimers.Remove (IsInactive, CcuList::All);
+ MySet->OtherTimers.Remove (IsInactive, IvlList::All);
}
/*?
@@ -233,9 +229,9 @@ Change the period of a timer. The new period will not be taken into account
before the next time-out.
?*/
void
-CcuCoreTimer :: ChangePeriod (Millisecond period)
+IvlCoreTimer :: ChangePeriod (Millisecond period)
{
- CcuSignalBlocker b (SigAlrm);
+ IvlSignalBlocker b (SigAlrm);
Period = period;
}
@@ -244,25 +240,24 @@ Change the number of times a timer will go off before stopping. If
the timer is stopped, it is not restarted.
?*/
void
-CcuCoreTimer :: ChangeNbPulses (int nb)
+IvlCoreTimer :: ChangeNbPulses (int nb)
{
- CcuSignalBlocker b (SigAlrm);
+ IvlSignalBlocker b (SigAlrm);
PulsesLeft = nb;
}
-
/*?
Stop this timer if it was running, then start it with its current period.
This function can be used to reset a timer to the beginning of a period.
?*/
void
-CcuCoreTimer :: Restart ()
+IvlCoreTimer :: Restart ()
{
if (PulsesLeft == 0)
return;
/* this function could be optimized: sometimes SetAlarm is called twice. */
- CcuSignalBlocker b (SigAlrm);
+ IvlSignalBlocker b (SigAlrm);
if (StatusFlag == Active)
Stop ();
Activate ();
@@ -275,13 +270,13 @@ first one, the alarm is updated.
!*/
/*?hidden?*/
void
-CcuCoreTimer :: Activate ()
+IvlCoreTimer :: Activate ()
{
if (!MySet)
return;
- CcuTimeStamp now;
+ IvlTimeStamp now;
NextDate = now + Period;
- CcuCoreTimer*& first = MySet->FirstTimer;
+ IvlCoreTimer*& first = MySet->FirstTimer;
if (!first) {
first = this;
SetAlarm (Period);
@@ -297,7 +292,7 @@ CcuCoreTimer :: Activate ()
/*?hidden?*/
void
-CcuCoreTimer :: Reschedule ()
+IvlCoreTimer :: Reschedule ()
{
if (PulsesLeft == 0) // this should not happen...
return;
@@ -311,29 +306,29 @@ CcuCoreTimer :: Reschedule ()
/*?hidden?*/
void
-CcuCoreTimer :: Schedule (Millisecond when)
+IvlCoreTimer :: Schedule (Millisecond when)
{
NextDate = when;
/* temporarily set status to inactive so that obsolete entries
in OtherTimers pointing to this timer can be removed */
StatusFlag = Inactive;
- CcuListOf <CcuCoreTimer>& others = MySet->OtherTimers;
+ IvlListOf <IvlCoreTimer>& others = MySet->OtherTimers;
#ifndef CPLUS_BUG19
- CcuListIterOf <CcuCoreTimer> li (others);
- CcuListIterOf <CcuCoreTimer> lj (others);
+ IvlListIterOf <IvlCoreTimer> li (others);
+ IvlListIterOf <IvlCoreTimer> lj (others);
#else
- CcuListIter li (others);
- CcuListIter lj (others);
+ IvlListIter li (others);
+ IvlListIter lj (others);
#endif
while (++li) {
/* while we're at it, remove inactive timers from the list */
#ifndef CPLUS_BUG19
- CcuCoreTimer* cur = *li;
+ IvlCoreTimer* cur = *li;
#else
- CcuCoreTimer* cur = (CcuCoreTimer*) *li;
+ IvlCoreTimer* cur = (IvlCoreTimer*) *li;
#endif
if (cur->StatusFlag != Active) {
others.RemoveAfter (lj);
@@ -352,13 +347,13 @@ Stop a timer.
This timer will not deliver any signal until \fun{Restart} is called.
?*/
void
-CcuCoreTimer :: Stop ()
+IvlCoreTimer :: Stop ()
{
if (StatusFlag != Active)
return;
- CcuSignalBlocker b (SigAlrm);
- CcuCoreTimer*& first = MySet->FirstTimer;
+ IvlSignalBlocker b (SigAlrm);
+ IvlCoreTimer*& first = MySet->FirstTimer;
StatusFlag = Inactive;
@@ -368,7 +363,7 @@ CcuCoreTimer :: Stop ()
if (first == 0)
StopAlarm ();
else {
- CcuTimeStamp now;
+ IvlTimeStamp now;
first->SetAlarm (first->NextDate - now);
}
}
@@ -379,7 +374,7 @@ CcuCoreTimer :: Stop ()
Wait for this timer to expire. If it is stopped, return immediately.
?*/
void
-CcuCoreTimer :: Wait ()
+IvlCoreTimer :: Wait ()
{
if (StatusFlag != Active)
return;
@@ -396,7 +391,7 @@ CcuCoreTimer :: Wait ()
#ifdef DOC
/*?nextdoc?*/
Millisecond
-CcuCoreTimer :: GetPeriod () const
+IvlCoreTimer :: GetPeriod () const
{
}
@@ -404,7 +399,7 @@ CcuCoreTimer :: GetPeriod () const
Get the period (resp. the number of pulses to go) of a timer.
?*/
int
-CcuCoreTimer :: GetNbPulses () const
+IvlCoreTimer :: GetNbPulses () const
{
}
@@ -412,13 +407,13 @@ CcuCoreTimer :: GetNbPulses () const
Get the status of a timer. The returned value is \var{Active} or \var{Inactive}.
?*/
timer_status
-CcuCoreTimer :: GetStatus () const
+IvlCoreTimer :: GetStatus () const
{
}
/*?nextdoc?*/
void
-CcuCoreTimer :: SetAlarm (Millisecond delay)
+IvlCoreTimer :: SetAlarm (Millisecond delay)
{
}
@@ -429,7 +424,7 @@ scheme. \fun{SetAlarm} should ensure that the function \fun{Fire} will be
called in \var{delay} milliseconds from now. \fun{StopAlarm} should disable such calls.
?*/
void
-CcuCoreTimer :: StopAlarm ()
+IvlCoreTimer :: StopAlarm ()
{
}
@@ -440,14 +435,13 @@ This virtual function is called when the timer is triggered. It should be
redefined in derived classes to attach a behaviour to a timer.
?*/
void
-CcuCoreTimer :: Handle (Millisecond)
+IvlCoreTimer :: Handle (Millisecond)
{
}
-
-/*?class CcuBaseTimer
-The class \typ{CcuBaseTimer} is provided as a base class for signal-based timers.
-It comes with a derived class \typ{CcuTimer} that can be used as is.
+/*?class IvlBaseTimer
+The class \typ{IvlBaseTimer} is provided as a base class for signal-based timers.
+It comes with a derived class \typ{IvlTimer} that can be used as is.
A timer is created with a period expressed in milliseconds. It will then
periodically call the member function \fun{Handle}, which should be redefined
in derived classes.
@@ -458,16 +452,15 @@ Using them concurrently with system calls such as \fun{sleep} yields
unexpected results.
?*/
-
-CcuSignalHandler* CcuBaseTimer::TimeOutHandler = 0;
-CcuTimerSet* CcuBaseTimer::TimerSet = 0;
+IvlSignalHandler* IvlBaseTimer::TimeOutHandler = 0;
+IvlTimerSet* IvlBaseTimer::TimerSet = 0;
/*?hidden?*/
void
-CcuBaseTimer :: ClassInit ()
+IvlBaseTimer :: ClassInit ()
{
- TimeOutHandler = new CcuSignalHandler (SigAlrm, &CcuBaseTimer::HandleSignal);
- TimerSet = new CcuTimerSet;
+ TimeOutHandler = new IvlSignalHandler (SigAlrm, &IvlBaseTimer::HandleSignal);
+ TimerSet = new IvlTimerSet;
}
/*?
@@ -476,17 +469,17 @@ If \var{pulses} is negative, the timer will send signals forever.
Timers are activated at creation time. They are disactivated, but not destroyed, after
their last pulse.
?*/
-CcuBaseTimer :: CcuBaseTimer (Millisecond period, int pulses)
-: CcuCoreTimer (period, pulses, (TimerSet ? TimerSet : (ClassInit (), TimerSet)))
+IvlBaseTimer :: IvlBaseTimer (Millisecond period, int pulses)
+: IvlCoreTimer (period, pulses, (TimerSet ? TimerSet : (ClassInit (), TimerSet)))
{
- CcuSignalBlocker b (SigAlrm);
+ IvlSignalBlocker b (SigAlrm);
if (PulsesLeft != 0)
Activate ();
}
/*?nodoc?*/
-CcuBaseTimer :: ~CcuBaseTimer ()
+IvlBaseTimer :: ~IvlBaseTimer ()
{
/* stop it */
if (StatusFlag == Active)
@@ -495,7 +488,7 @@ CcuBaseTimer :: ~CcuBaseTimer ()
/*?hidden?*/
void
-CcuBaseTimer :: StopAlarm ()
+IvlBaseTimer :: StopAlarm ()
{
struct itimerval itval;
timerclear (&itval.it_value);
@@ -505,7 +498,7 @@ CcuBaseTimer :: StopAlarm ()
/*?hidden?*/
void
-CcuBaseTimer :: SetAlarm (Millisecond delay)
+IvlBaseTimer :: SetAlarm (Millisecond delay)
{
if (delay <= 0) {
StopAlarm ();
@@ -521,7 +514,6 @@ CcuBaseTimer :: SetAlarm (Millisecond delay)
fprintf (stderr, "setitimer failed for interval %d\n", delay);
}
-
/*!
This function is called by the signal handler. The first active timer is expired
and scheduled again. Then all the timers whose expiration time is earlier
@@ -533,16 +525,15 @@ active timer.
!*/
/*?hidden?*/
void
-CcuBaseTimer :: HandleSignal (int)
+IvlBaseTimer :: HandleSignal (int)
{
Fire (TimerSet);
}
-
-/*?class CcuTimer
-The class \typ{CcuTimer} is a derived class of \typ{CcuBaseTimer} that
+/*?class IvlTimer
+The class \typ{IvlTimer} is a derived class of \typ{IvlBaseTimer} that
can be used without deriving a new class.
-Each \typ{CcuTimer} holds a pointer to a function which is called when the timer
+Each \typ{IvlTimer} holds a pointer to a function which is called when the timer
expires. This function, which is passed to the constructor, must
take a \typ{Millisecond} argument and return \typ{void}.
?*/
@@ -551,20 +542,20 @@ take a \typ{Millisecond} argument and return \typ{void}.
Create a timer that will expire every \var{period} milliseconds and call
the function \var{handler}.
?*/
-CcuTimer :: CcuTimer (Millisecond period, void (*handler) (Millisecond), int pulses)
-: CcuBaseTimer (period, pulses),
+IvlTimer :: IvlTimer (Millisecond period, void (*handler) (Millisecond), int pulses)
+: IvlBaseTimer (period, pulses),
Handler (handler)
{
}
/*?nodoc?*/
-CcuTimer :: ~CcuTimer ()
+IvlTimer :: ~IvlTimer ()
{
}
/*?hidden?*/
void
-CcuTimer :: Handle (Millisecond ref)
+IvlTimer :: Handle (Millisecond ref)
{
(*Handler) (ref);
}
@@ -574,7 +565,7 @@ Change the handling function of a timer.
?*/
#ifdef DOC
void
-CcuTimer :: SetHandler ((void)(*h)(Millisecond))
+IvlTimer :: SetHandler ((void)(*h)(Millisecond))
{
}
#endif