E-MailRelay

gtimer.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gtimer.cpp
19///
20
21#include "gdef.h"
22#include "gtimer.h"
23#include "gtimerlist.h"
24#include "gevent.h"
25#include "glog.h"
26#include "gassert.h"
27#include <algorithm>
28
29 GNet::TimerBase::TimerBase( ExceptionSink es ) :
30 m_time(G::TimerTime::zero())
31{
32 TimerList::instance().add( *this , es ) ;
33}
34
35 GNet::TimerBase::~TimerBase()
36{
37 try
38 {
39 if( TimerList::ptr() != nullptr )
40 TimerList::ptr()->remove( *this ) ;
41 }
42 catch(...) // dtor
43 {
44 }
45}
46
47 bool GNet::TimerBase::expired( G::TimerTime & now ) const
48{
49 if( !active() )
50 {
51 return false ;
52 }
53 else if( immediate() )
54 {
55 return true ;
56 }
57 else
58 {
59 if( now == G::TimerTime::zero() ) now = G::TimerTime::now() ; // lazy optimisation
60 return m_time <= now ;
61 }
62}
63
64 void GNet::TimerBase::startTimer( unsigned int time , unsigned int time_us )
65{
66 m_time = (time==0U && time_us==0U) ? history() : ( G::TimerTime::now() + G::TimeInterval(time,time_us) ) ;
67 TimerList::instance().updateOnStart( *this ) ; // adjust()
68}
69
70 void GNet::TimerBase::startTimer( const G::TimeInterval & i )
71{
72 m_time = i == G::TimeInterval(0U) ? history() : ( G::TimerTime::now() + i ) ;
73 TimerList::instance().updateOnStart( *this ) ; // adjust()
74}
75
76 G::TimerTime GNet::TimerBase::history()
77{
78 // base for an arbitrary historical one-second non-zero epoch
79 // time interval used for all immediate() timers
80 return G::TimerTime::zero() + G::TimeInterval( 1U ) ;
81}
82
83 bool GNet::TimerBase::immediate() const
84{
85 return m_time.sameSecond( history() ) ; // history() ignoring any adjust()ment
86}
87
88 void GNet::TimerBase::adjust( unsigned int us )
89{
90 m_time = history() + G::TimeInterval( 0U , std::min( 999999U , us ) ) ;
91 G_ASSERT( immediate() ) ;
92}
93
94 void GNet::TimerBase::cancelTimer()
95{
96 if( active() )
97 {
98 m_time = G::TimerTime::zero() ;
99 TimerList::instance().updateOnCancel( *this ) ;
100 }
101 G_ASSERT( !active() ) ;
102}
103
104 void GNet::TimerBase::doTimeout()
105{
106 G_ASSERT( active() ) ;
107 m_time = G::TimerTime::zero() ;
108 onTimeout() ;
109}
110
111 G::TimerTime GNet::TimerBase::t() const
112{
113 return m_time ;
114}
115
GNet::ExceptionSink
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
Definition: gexceptionsink.h:100
GNet::TimerBase::doTimeout
void doTimeout()
Used by TimerList to execute the onTimeout() callback.
Definition: gtimer.cpp:104
GNet::TimerBase::immediate
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:83
GNet::TimerBase::cancelTimer
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.cpp:94
GNet::TimerBase::TimerBase
TimerBase(ExceptionSink es)
Constructor.
Definition: gtimer.cpp:29
GNet::TimerBase::startTimer
void startTimer(unsigned int interval_s, unsigned int interval_us=0U)
Starts or restarts the timer so that it expires after the given interval.
Definition: gtimer.cpp:64
GNet::TimerBase::expired
bool expired(G::TimerTime &) const
Used by TimerList.
Definition: gtimer.cpp:47
GNet::TimerBase::t
G::TimerTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:111
GNet::TimerBase::~TimerBase
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:35
GNet::TimerBase::adjust
void adjust(unsigned int us)
Used by TimerList to set the fractional part of the expiry time of immediate() timers so that t() is ...
Definition: gtimer.cpp:88
GNet::TimerList::remove
void remove(TimerBase &) noexcept
Removes a timer from the list.
Definition: gtimerlist.cpp:93
GNet::TimerList::updateOnStart
void updateOnStart(TimerBase &)
Called by Timer when a timer is started.
Definition: gtimerlist.cpp:119
GNet::TimerList::instance
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:180
GNet::TimerList::ptr
static TimerList * ptr() noexcept
Singleton access. Returns nullptr if none.
Definition: gtimerlist.cpp:170
GNet::TimerList::updateOnCancel
void updateOnCancel(TimerBase &)
Called by Timer when a timer is cancelled.
Definition: gtimerlist.cpp:131
GNet::TimerList::add
void add(TimerBase &, ExceptionSink)
Adds a timer. Called from the Timer constructor.
Definition: gtimerlist.cpp:88
G::TimeInterval
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:289
G::TimerTime
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:213
G::TimerTime::now
static TimerTime now()
Factory function for the current steady-clock time.
Definition: gdatetime.cpp:385
G::TimerTime::zero
static TimerTime zero()
Factory function for the start of the epoch.
Definition: gdatetime.cpp:390
gassert.h
gevent.h
glog.h
gtimer.h
gtimerlist.h
G
Low-level classes.
Definition: galign.h:28