Kudu C++ client API
monotime.h
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 #ifndef KUDU_UTIL_MONOTIME_H
18 #define KUDU_UTIL_MONOTIME_H
19 
20 // NOTE: using stdint.h instead of cstdint because this file is supposed
21 // to be processed by a compiler lacking C++11 support.
22 #include <stdint.h>
23 
24 #include <string>
25 
26 #ifdef KUDU_HEADERS_NO_STUBS
27 #include <gtest/gtest_prod.h>
28 
29 #include "kudu/gutil/port.h"
30 #else
31 // This is a poor module interdependency, but the stubs are header-only and
32 // it's only for exported header builds, so we'll make an exception.
33 #include "kudu/client/stubs.h"
34 #endif
35 
36 #include "kudu/util/kudu_export.h"
37 
38 
39 // The 'noexcept' specifier is recognized by a C++11-capable compiler, but this
40 // file is targeted to compile by C++-98 compiler as well. As it turns out,
41 // adding 'noexcept' doesn't affect the generated symbols in the exported
42 // MonoTime class, so it's safe to turn it on when compiling in the C++11 mode.
43 // The 'noexcept' specified is useful in cases when wrapping MonoTime into
44 // std::atomic<> and the standard C++ library explicitly requires that.
45 #ifdef LANG_CXX11
46 #define KUDU_MONOTIME_NOEXCEPT noexcept
47 #else
48 #define KUDU_MONOTIME_NOEXCEPT
49 #endif // #ifdef LANG_CXX11 ... #else ...
50 
51 namespace kudu {
52 
57 class KUDU_EXPORT MonoDelta {
58  public:
67  static MonoDelta FromSeconds(double seconds);
68  static MonoDelta FromMilliseconds(int64_t ms);
69  static MonoDelta FromMicroseconds(int64_t us);
70  static MonoDelta FromNanoseconds(int64_t ns);
72 
77  MonoDelta();
78 
80  bool Initialized() const;
81 
88  bool LessThan(const MonoDelta &rhs) const;
89 
96  bool MoreThan(const MonoDelta &rhs) const;
97 
105  bool Equals(const MonoDelta &rhs) const;
106 
108  std::string ToString() const;
109 
115  double ToSeconds() const;
116  int64_t ToMilliseconds() const;
117  int64_t ToMicroseconds() const;
118  int64_t ToNanoseconds() const;
120 
126  void ToTimeVal(struct timeval *tv) const;
127 
133  void ToTimeSpec(struct timespec *ts) const;
134 
141  static void NanosToTimeSpec(int64_t nanos, struct timespec* ts);
142 
143  private:
144  static const int64_t kUninitialized;
145 
146  friend class MonoTime;
147  friend MonoDelta operator-(const class MonoTime&, const class MonoTime&);
148  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
149 
150  explicit MonoDelta(int64_t delta);
151  int64_t nano_delta_;
152 };
153 
161 class KUDU_EXPORT MonoTime {
162  public:
166  static const int64_t kNanosecondsPerSecond = 1000000000L;
167  static const int64_t kNanosecondsPerMillisecond = 1000000L;
168  static const int64_t kNanosecondsPerMicrosecond = 1000L;
169 
170  static const int64_t kMicrosecondsPerSecond = 1000000L;
172 
176  static MonoTime Now();
177 
179  static MonoTime Max();
180 
182  static MonoTime Min();
183 
193  static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b)
194  ATTRIBUTE_DEPRECATED("use std::min() instead");
195 
198  MonoTime() KUDU_MONOTIME_NOEXCEPT;
199 
201  bool Initialized() const;
202 
213  MonoDelta GetDeltaSince(const MonoTime &rhs) const ATTRIBUTE_DEPRECATED(
214  "use kudu::operator-(const MonoTime&, const MonoTime&) instead");
215 
220  void AddDelta(const MonoDelta &delta);
221 
229  bool ComesBefore(const MonoTime &rhs) const;
230 
232  std::string ToString() const;
233 
239  void ToTimeSpec(struct timespec* ts) const;
240 
247  bool Equals(const MonoTime& other) const;
248 
257  MonoTime& operator+=(const MonoDelta& delta);
258 
264  MonoTime& operator-=(const MonoDelta& delta);
266 
267  private:
268  friend class MonoDelta;
269  friend MonoDelta operator-(const MonoTime&, const MonoTime&);
270  FRIEND_TEST(TestMonoTime, TestTimeSpec);
271  FRIEND_TEST(TestMonoTime, TestDeltaConversions);
272 
273  explicit MonoTime(const struct timespec &ts) KUDU_MONOTIME_NOEXCEPT;
274  explicit MonoTime(int64_t nanos) KUDU_MONOTIME_NOEXCEPT;
275  double ToSeconds() const;
276  int64_t nanos_;
277 };
278 
289 void KUDU_EXPORT SleepFor(const MonoDelta& delta);
290 
300 bool KUDU_EXPORT operator==(const MonoDelta &lhs, const MonoDelta &rhs);
301 
308 bool KUDU_EXPORT operator!=(const MonoDelta &lhs, const MonoDelta &rhs);
309 
316 bool KUDU_EXPORT operator<(const MonoDelta &lhs, const MonoDelta &rhs);
317 
324 bool KUDU_EXPORT operator<=(const MonoDelta &lhs, const MonoDelta &rhs);
325 
332 bool KUDU_EXPORT operator>(const MonoDelta &lhs, const MonoDelta &rhs);
333 
340 bool KUDU_EXPORT operator>=(const MonoDelta &lhs, const MonoDelta &rhs);
342 
356 bool KUDU_EXPORT operator==(const MonoTime& lhs, const MonoTime& rhs);
357 
369 bool KUDU_EXPORT operator!=(const MonoTime& lhs, const MonoTime& rhs);
370 
377 bool KUDU_EXPORT operator<(const MonoTime& lhs, const MonoTime& rhs);
378 
385 bool KUDU_EXPORT operator<=(const MonoTime& lhs, const MonoTime& rhs);
386 
393 bool KUDU_EXPORT operator>(const MonoTime& lhs, const MonoTime& rhs);
394 
401 bool KUDU_EXPORT operator>=(const MonoTime& lhs, const MonoTime& rhs);
403 
414 MonoTime KUDU_EXPORT operator+(const MonoTime& t, const MonoDelta& delta);
415 
423 MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta& delta);
424 
435 MonoDelta KUDU_EXPORT operator-(const MonoTime& t_end, const MonoTime& t_begin);
437 
438 } // namespace kudu
439 
440 #endif
Definition: callbacks.h:28
Representation of a particular point in time.
Definition: monotime.h:161
A representation of a time interval.
Definition: monotime.h:57