libzypp  17.22.0
timer.cc
Go to the documentation of this file.
1 #include "timer.h"
2 #include "private/base_p.h"
3 
4 #include "eventdispatcher.h"
5 
6 #include <time.h>
7 #include <zypp/base/Logger.h>
8 #include <zypp/base/Exception.h>
9 #include <iostream>
10 #include <glib.h>
11 
12 namespace zyppng {
13 
14 using namespace zypp;
15 
16 
18 {
20 public:
21  TimerPrivate();
22  virtual ~TimerPrivate();
23 
24  uint64_t _beginMs = 0;
25  uint64_t _requestedTimeout = 0;
26  std::weak_ptr<EventDispatcher> _ev;
27  bool _isRunning = false;
28 
29  signal<void (Timer &t)> _expired;
30 
31  bool _singleShot = false;
32 
33 };
34 
36 {
37  auto ev = EventDispatcher::instance();
38  if ( !ev )
39  ZYPP_THROW( zypp::Exception( "Creating timers without a EventDispatcher instance is not supported" ) );
40  _ev = ev;
41 }
42 
44 {}
45 
46 std::shared_ptr<Timer> Timer::create()
47 {
48  return std::shared_ptr<Timer>( new Timer() );
49 }
50 
52 { stop(); }
53 
54 void Timer::setSingleShot(bool singleShot)
55 {
56  d_func()->_singleShot = singleShot;
57 }
58 
59 bool Timer::singleShot() const
60 {
61  return d_func()->_singleShot;
62 }
63 
64 uint64_t Timer::now()
65 {
66  return static_cast<uint64_t>( g_get_monotonic_time () / 1000 );
67 #if 0
68  timespec now{0 ,0};
69  if ( clock_gettime( CLOCK_MONOTONIC_RAW, &now ) ) {
70  WAR << "Unable to get current monotonic time, timers will not work" << std::endl;
71  return 1;
72  }
73  return ( uint(now.tv_sec) * 1000 ) + uint( now.tv_nsec * 1e-6 );
74 #endif
75 }
76 
77 uint64_t Timer::started() const
78 {
79  return d_func()->_beginMs;
80 }
81 
82 uint64_t Timer::interval() const
83 {
84  return d_func()->_requestedTimeout;
85 }
86 
87 uint64_t Timer::remaining() const
88 {
89  Z_D();
90 
91  uint64_t elapsed = this->elapsed();
92  if ( elapsed >= d->_requestedTimeout )
93  return 0;
94  return ( d->_requestedTimeout - elapsed );
95 }
96 
97 uint64_t Timer::elapsed() const
98 {
99  Z_D();
100  uint64_t nowMs = now();
101  return ( nowMs - d->_beginMs );
102 }
103 
104 uint64_t Timer::expires() const
105 {
106  return d_func()->_beginMs + d_func()->_requestedTimeout;
107 }
108 
110 {
111  return d_func()->_expired;
112 }
113 
114 uint64_t Timer::expire()
115 {
116  Z_D();
117  //@FIXME, we should not rely on this, maybe a "deleteLater" feature
118  //in the MainLoop?
119  //make sure timer is not deleted during signal emission
120  auto lock = shared_from_this();
121 
122  auto exp = remaining();
123  if ( exp == 0 ) {
124  if ( d->_singleShot )
125  stop();
126  else
127  d->_beginMs = now();
128  d->_expired.emit( *this );
129  }
130  return exp;
131 }
132 
133 bool Timer::isRunning() const
134 {
135  return d_func()->_isRunning;
136 }
137 
139 {
140  start ( d_func()->_requestedTimeout );
141 }
142 
143 void Timer::start( uint64_t timeout )
144 {
145  Z_D();
146 
147  d->_requestedTimeout = timeout;
148  d->_beginMs = now();
149 
150  if ( !d->_isRunning ) {
151  auto ev = d->_ev.lock();
152  //if ev is null we are shutting down
153  if ( !ev )
154  return;
155  ev->registerTimer( this );
156 
157  d->_isRunning = true;
158  }
159 
160 }
161 
163 {
164  Z_D();
165 
166  if ( !d->_isRunning )
167  return;
168 
169  auto ev = d->_ev.lock();
170 
171  //event loop might be shutting down
172  if ( ev )
173  ev->removeTimer( this );
174 
175  d->_isRunning = false;
176 }
177 
179 { }
180 
181 }
uint64_t interval() const
Definition: timer.cc:82
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
std::weak_ptr< EventDispatcher > _ev
Definition: timer.cc:26
virtual ~Timer()
Definition: timer.cc:51
SignalProxy< void(Timer &t)> sigExpired()
This signal is always emitted when the timer expires.
Definition: timer.cc:109
#define Z_D()
Definition: zyppglobal.h:44
time_t timeout
Definition: MediaCurl.cc:66
void stop()
Stops the timer if its running. The.
Definition: timer.cc:162
uint64_t expires() const
Definition: timer.cc:104
void setSingleShot(bool singleShot=true)
Sets the timer to trigger only once, after it has expired once.
Definition: timer.cc:54
void start()
Definition: timer.cc:138
#define WAR
Definition: Logger.h:80
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:42
static std::shared_ptr< EventDispatcher > instance()
uint64_t elapsed() const
Definition: timer.cc:97
uint64_t remaining() const
Definition: timer.cc:87
static std::shared_ptr< Timer > create()
Creates a new Timer object, the timer is not started at this point.
Definition: timer.cc:46
uint64_t expire()
Advances the internal clock of the timer, if the timer expires the sigExpired signal is emitted...
Definition: timer.cc:114
Base class for Exception.
Definition: Exception.h:145
static uint64_t now()
Definition: timer.cc:64
virtual ~TimerPrivate()
Definition: timer.cc:43
signal< void(Timer &t)> _expired
Definition: timer.cc:29
bool singleShot() const
Definition: timer.cc:59
bool isRunning() const
Definition: timer.cc:133
#define ZYPP_DECLARE_PUBLIC(Class)
Definition: zyppglobal.h:39
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
uint64_t started() const
Definition: timer.cc:77