E-MailRelay
gtimer.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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_active(false) ,
31 m_immediate(false) ,
32 m_time(G::TimerTime::zero())
33{
34 TimerList::instance().add( *this , es ) ;
35}
36
38{
39 try
40 {
41 if( TimerList::ptr() != nullptr )
42 TimerList::ptr()->remove( *this ) ;
43 }
44 catch(...) // dtor
45 {
46 }
47}
48
50{
51 if( !m_active )
52 {
53 return false ;
54 }
55 else if( immediate() )
56 {
57 return true ;
58 }
59 else
60 {
61 // lazy evaluation of caller's idea of now -- no call
62 // to TimerTime::now() if there is a zero-length
63 // timer or no timers at all
64 if( now == G::TimerTime::zero() )
65 now = G::TimerTime::now() ;
66
67 return m_time <= now ;
68 }
69}
70
71void GNet::TimerBase::startTimer( unsigned int time , unsigned int time_us )
72{
73 m_active = true ;
74 m_immediate = time == 0U && time_us == 0U ;
75 m_time = m_immediate ? G::TimerTime::zero() : ( G::TimerTime::now() + G::TimeInterval(time,time_us) ) ;
76 TimerList::instance().updateOnStart( *this ) ; // adjust()
77}
78
80{
81 m_active = true ;
82 m_immediate = i == G::TimeInterval(0U) ;
83 m_time = m_immediate ? G::TimerTime::zero() : ( G::TimerTime::now() + i ) ;
84 TimerList::instance().updateOnStart( *this ) ; // adjust()
85}
86
88{
89 return m_immediate ;
90}
91
92void GNet::TimerBase::adjust( unsigned long order )
93{
94 G_ASSERT( m_active && m_immediate ) ;
95 m_time += G::TimeInterval( 0 , order ) ;
96}
97
99{
100 if( m_active )
101 {
102 m_active = false ;
104 }
105}
106
108{
109 G_ASSERT( m_active ) ;
110 m_active = false ;
111 onTimeout() ;
112}
113
115{
116 return m_time ;
117}
118
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:107
void adjust(unsigned long)
Used by TimerList to set the order of immedate() timer expiry.
Definition: gtimer.cpp:92
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:87
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.cpp:98
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:71
bool expired(G::TimerTime &) const
Used by TimerList.
Definition: gtimer.cpp:49
G::TimerTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:114
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:37
void remove(TimerBase &) noexcept
Removes a timer from the list.
Definition: gtimerlist.cpp:95
void updateOnStart(TimerBase &)
Called by Timer when a timer is started.
Definition: gtimerlist.cpp:121
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:185
static TimerList * ptr() noexcept
Singleton access. Returns nullptr if none.
Definition: gtimerlist.cpp:173
void updateOnCancel(TimerBase &)
Called by Timer when a timer is cancelled.
Definition: gtimerlist.cpp:133
void add(TimerBase &, ExceptionSink)
Adds a timer. Called from the Timer constructor.
Definition: gtimerlist.cpp:90
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:299
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:225
static TimerTime now()
Factory function for the current steady-clock time.
Definition: gdatetime.cpp:479
static TimerTime zero()
Factory function for the start of the epoch, guaranteed to be less than any now().
Definition: gdatetime.cpp:486
Low-level classes.
Definition: garg.h:30