E-MailRelay

gtimer.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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( EventState 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( !m_active )
50 {
51 return false ;
52 }
53 else if( immediate() )
54 {
55 return true ;
56 }
57 else
58 {
59 // lazy evaluation of caller's idea of now -- no call
60 // to TimerTime::now() if there is a zero-length
61 // timer or no timers at all
62 if( now == G::TimerTime::zero() )
63 now = G::TimerTime::now() ;
64
65 return m_time <= now ;
66 }
67}
68
69 void GNet::TimerBase::startTimer( unsigned int time , unsigned int time_us )
70{
71 m_active = true ;
72 m_immediate = time == 0U && time_us == 0U ;
73 m_time = m_immediate ? G::TimerTime::zero() : ( G::TimerTime::now() + G::TimeInterval(time,time_us) ) ;
74 TimerList::instance().updateOnStart( *this ) ; // adjust()
75}
76
77 void GNet::TimerBase::startTimer( const G::TimeInterval & i )
78{
79 m_active = true ;
80 m_immediate = i == G::TimeInterval(0U) ;
81 m_time = m_immediate ? G::TimerTime::zero() : ( G::TimerTime::now() + i ) ;
82 TimerList::instance().updateOnStart( *this ) ; // adjust()
83}
84
85 bool GNet::TimerBase::immediate() const
86{
87 return m_immediate ;
88}
89
90 void GNet::TimerBase::adjust( unsigned long order )
91{
92 G_ASSERT( m_active && m_immediate ) ;
93 m_time += G::TimeInterval( 0 , order ) ;
94}
95
96 void GNet::TimerBase::cancelTimer()
97{
98 if( m_active )
99 {
100 m_active = false ;
101 TimerList::instance().updateOnCancel( *this ) ;
102 }
103}
104
105 void GNet::TimerBase::doTimeout()
106{
107 G_ASSERT( m_active ) ;
108 m_active = false ;
109 onTimeout() ;
110}
111
112 G::TimerTime GNet::TimerBase::t() const
113{
114 return m_time ;
115}
116
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::TimerBase::doTimeout
void doTimeout()
Used by TimerList to execute the onTimeout() callback.
Definition: gtimer.cpp:105
GNet::TimerBase::adjust
void adjust(unsigned long)
Used by TimerList to set the order of immedate() timer expiry.
Definition: gtimer.cpp:90
GNet::TimerBase::immediate
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:85
GNet::TimerBase::cancelTimer
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.cpp:96
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:69
GNet::TimerBase::expired
bool expired(G::TimerTime &) const
Used by TimerList.
Definition: gtimer.cpp:47
GNet::TimerBase::TimerBase
TimerBase(EventState es)
Constructor.
Definition: gtimer.cpp:29
GNet::TimerBase::t
G::TimerTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:112
GNet::TimerBase::~TimerBase
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:35
GNet::TimerList::remove
void remove(TimerBase &) noexcept
Removes a timer from the list.
Definition: gtimerlist.cpp:92
GNet::TimerList::updateOnStart
void updateOnStart(TimerBase &)
Called by Timer when a timer is started.
Definition: gtimerlist.cpp:118
GNet::TimerList::add
void add(TimerBase &, EventState)
Adds a timer. Called from the Timer constructor.
Definition: gtimerlist.cpp:87
GNet::TimerList::instance
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:182
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:130
G::TimeInterval
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:305
G::TimerTime
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:231
G::TimerTime::now
static TimerTime now()
Factory function for the current steady-clock time.
Definition: gdatetime.cpp:479
G::TimerTime::zero
static TimerTime zero()
Factory function for the start of the epoch, guaranteed to be less than any now().
Definition: gdatetime.cpp:486
gassert.h
gevent.h
glog.h
gtimer.h
gtimerlist.h
G
Low-level classes.
Definition: garg.h:36