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
30 m_time(G::TimerTime::zero())
31{
32 TimerList::instance().add( *this , es ) ;
33}
34
36{
37 try
38 {
39 if( TimerList::ptr() != nullptr )
40 TimerList::ptr()->remove( *this ) ;
41 }
42 catch(...) // dtor
43 {
44 }
45}
46
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
64void 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
71{
72 m_time = i == G::TimeInterval(0U) ? history() : ( G::TimerTime::now() + i ) ;
73 TimerList::instance().updateOnStart( *this ) ; // adjust()
74}
75
76G::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
84{
85 return m_time.sameSecond( history() ) ; // history() ignoring any adjust()ment
86}
87
88void GNet::TimerBase::adjust( unsigned int us )
89{
90 m_time = history() + G::TimeInterval( 0U , std::min( 999999U , us ) ) ;
91 G_ASSERT( immediate() ) ;
92}
93
95{
96 if( active() )
97 {
98 m_time = G::TimerTime::zero() ;
100 }
101 G_ASSERT( !active() ) ;
102}
103
105{
106 G_ASSERT( active() ) ;
107 m_time = G::TimerTime::zero() ;
108 onTimeout() ;
109}
110
112{
113 return m_time ;
114}
115
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
void doTimeout()
Used by TimerList to execute the onTimeout() callback.
Definition: gtimer.cpp:104
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:83
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.cpp:94
TimerBase(ExceptionSink es)
Constructor.
Definition: gtimer.cpp:29
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
bool expired(G::TimerTime &) const
Used by TimerList.
Definition: gtimer.cpp:47
G::TimerTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:111
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:35
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
void remove(TimerBase &) noexcept
Removes a timer from the list.
Definition: gtimerlist.cpp:93
void updateOnStart(TimerBase &)
Called by Timer when a timer is started.
Definition: gtimerlist.cpp:119
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:180
static TimerList * ptr() noexcept
Singleton access. Returns nullptr if none.
Definition: gtimerlist.cpp:170
void updateOnCancel(TimerBase &)
Called by Timer when a timer is cancelled.
Definition: gtimerlist.cpp:131
void add(TimerBase &, ExceptionSink)
Adds a timer. Called from the Timer constructor.
Definition: gtimerlist.cpp:88
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:289
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:213
static TimerTime now()
Factory function for the current steady-clock time.
Definition: gdatetime.cpp:385
static TimerTime zero()
Factory function for the start of the epoch.
Definition: gdatetime.cpp:390
Low-level classes.
Definition: galign.h:28