0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Timer.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #ifndef Common_Timer_h
27 #define Common_Timer_h
28 
29 #include <Common/fast_clock.h>
30 
31 #include <cassert>
32 #include <chrono>
33 #include <cstring>
34 
35 namespace Hypertable {
36 
44  class Timer {
45  public:
54  Timer(uint32_t millis, bool start_timer = false)
55  : m_duration(std::chrono::milliseconds(millis)),
56  m_remaining(std::chrono::milliseconds(millis)) {
57  if (start_timer)
58  start();
59  }
60 
64  void start() {
65  if (!m_running) {
67  m_running = true;
68  if (!m_started)
69  m_started = true;
70  }
71  }
72 
77  void stop() {
78  assert(m_started);
79  auto adjustment = std::chrono::fast_clock::now() - start_time;
80  m_remaining = (adjustment < m_remaining) ?
81  m_remaining - adjustment :
82  std::chrono::fast_clock::duration::zero();
83  m_running = false;
84  }
85 
89  void reset(bool start_timer = false) {
90  m_running = false;
91  m_started = false;
93  if (start_timer)
94  start();
95  }
96 
101  uint32_t remaining() {
102  if (m_running) {
103  stop();
104  start();
105  }
106  return std::chrono::duration_cast<std::chrono::milliseconds>(m_remaining).count();
107  }
108 
112  bool expired() {
113  return remaining() == 0;
114  }
115 
119  bool is_running() {
120  return m_running;
121  }
122 
127  uint32_t duration() {
128  return (uint32_t)std::chrono::duration_cast<std::chrono::milliseconds>(m_duration).count();
129  }
130 
131 
132  private:
133 
136 
138  bool m_running {};
139 
141  bool m_started {};
142 
145 
148  };
149 
152 }
153 
154 #endif // Common_Timer_h
chrono::time_point< fast_clock > time_point
Definition: fast_clock.h:42
void stop()
Stops the timer.
Definition: Timer.h:77
Timer(uint32_t millis, bool start_timer=false)
Constructor; assigns number of milliseconds after which the timer will expire.
Definition: Timer.h:54
std::chrono::fast_clock::duration m_duration
The duration of the timer.
Definition: Timer.h:144
Declarations for fast_clock.
STL namespace.
bool m_running
True if the timer is running.
Definition: Timer.h:138
uint32_t remaining()
Returns the remaining time till expiry.
Definition: Timer.h:101
bool expired()
Returns true if the timer is expired.
Definition: Timer.h:112
void reset(bool start_timer=false)
Resets the timer.
Definition: Timer.h:89
static time_point now() noexcept
Definition: fast_clock.cc:37
uint32_t duration()
Returns the duration of the timer.
Definition: Timer.h:127
Hypertable definitions
bool m_started
True if the timer was started.
Definition: Timer.h:141
void start()
Starts the timer.
Definition: Timer.h:64
A timer class to keep timeout states across AsyncComm related calls.
Definition: Timer.h:44
std::chrono::fast_clock::time_point start_time
The time when the timer was started.
Definition: Timer.h:135
bool is_running()
Returns true if the timer is still running (not yet expired.
Definition: Timer.h:119
std::chrono::fast_clock::duration m_remaining
The remaining time till expiration.
Definition: Timer.h:147
microseconds duration
Definition: fast_clock.h:39