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
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( !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
69void 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
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
86{
87 return m_immediate ;
88}
89
90void GNet::TimerBase::adjust( unsigned long order )
91{
92 G_ASSERT( m_active && m_immediate ) ;
93 m_time += G::TimeInterval( 0 , order ) ;
94}
95
97{
98 if( m_active )
99 {
100 m_active = false ;
102 }
103}
104
106{
107 G_ASSERT( m_active ) ;
108 m_active = false ;
109 onTimeout() ;
110}
111
113{
114 return m_time ;
115}
116
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
void doTimeout()
Used by TimerList to execute the onTimeout() callback.
Definition: gtimer.cpp:105
void adjust(unsigned long)
Used by TimerList to set the order of immedate() timer expiry.
Definition: gtimer.cpp:90
bool immediate() const
Used by TimerList.
Definition: gtimer.cpp:85
void cancelTimer()
Cancels the timer. Does nothing if not running.
Definition: gtimer.cpp:96
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
bool expired(G::TimerTime &) const
Used by TimerList.
Definition: gtimer.cpp:47
TimerBase(EventState es)
Constructor.
Definition: gtimer.cpp:29
G::TimerTime t() const
Used by TimerList to get the expiry epoch time.
Definition: gtimer.cpp:112
virtual ~TimerBase()
Destructor.
Definition: gtimer.cpp:35
void remove(TimerBase &) noexcept
Removes a timer from the list.
Definition: gtimerlist.cpp:92
void updateOnStart(TimerBase &)
Called by Timer when a timer is started.
Definition: gtimerlist.cpp:118
void add(TimerBase &, EventState)
Adds a timer. Called from the Timer constructor.
Definition: gtimerlist.cpp:87
static TimerList & instance()
Singleton access. Throws an exception if none.
Definition: gtimerlist.cpp:182
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:130
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:305
A monotonically increasing subsecond-resolution timestamp, notionally unrelated to time_t.
Definition: gdatetime.h:231
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:36