Kudu C++ client API
Loading...
Searching...
No Matches
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 <iosfwd>
25#include <string>
26
27#ifdef KUDU_HEADERS_NO_STUBS
28#include <gtest/gtest_prod.h>
29
30#include "kudu/gutil/port.h"
31#else
32// This is a poor module interdependency, but the stubs are header-only and
33// it's only for exported header builds, so we'll make an exception.
34#include "kudu/client/stubs.h"
35#endif
36
37#include "kudu/util/kudu_export.h"
38
39
40// The 'noexcept' specifier is recognized by a C++11-capable compiler, but this
41// file is targeted to compile by C++-98 compiler as well. As it turns out,
42// adding 'noexcept' doesn't affect the generated symbols in the exported
43// MonoTime class, so it's safe to turn it on when compiling in the C++11 mode.
44// The 'noexcept' specified is useful in cases when wrapping MonoTime into
45// std::atomic<> and the standard C++ library explicitly requires that.
46#ifdef LANG_CXX11
47#define KUDU_MONOTIME_NOEXCEPT noexcept
48#else
49#define KUDU_MONOTIME_NOEXCEPT
50#endif // #ifdef LANG_CXX11 ... #else ...
51
52namespace kudu {
53
58class KUDU_EXPORT MonoDelta {
59 public:
62
67 static MonoDelta FromSeconds(double seconds);
68
73 static MonoDelta FromMilliseconds(int64_t ms);
74
79 static MonoDelta FromMicroseconds(int64_t us);
80
85 static MonoDelta FromNanoseconds(int64_t ns);
87
93
95 bool Initialized() const;
96
103 bool LessThan(const MonoDelta &rhs) const;
104
111 bool MoreThan(const MonoDelta &rhs) const;
112
120 bool Equals(const MonoDelta &rhs) const;
121
123 std::string ToString() const;
124
127
129 double ToSeconds() const;
130 int64_t ToMilliseconds() const;
131 int64_t ToMicroseconds() const;
132 int64_t ToNanoseconds() const;
134
140 void ToTimeVal(struct timeval *tv) const;
141
147 void ToTimeSpec(struct timespec* ts) const;
148
155 static void NanosToTimeSpec(int64_t nanos, struct timespec* ts);
156
159
166
174
175 private:
176 static const int64_t kUninitialized;
177
178 friend class MonoTime;
179
180 friend MonoDelta operator-(const class MonoTime&, const class MonoTime&);
181 friend MonoDelta operator-(const MonoDelta&, const MonoDelta&);
182 friend MonoDelta operator+(const MonoDelta&, const MonoDelta&);
183
184 FRIEND_TEST(TestMonoTime, TestDeltaConversions);
185
186 explicit MonoDelta(int64_t delta);
187 int64_t nano_delta_;
188};
189
197class KUDU_EXPORT MonoTime {
198 public:
201
203 static const int64_t kNanosecondsPerSecond = 1000000000L;
204
206 static const int64_t kNanosecondsPerMillisecond = 1000000L;
207
209 static const int64_t kNanosecondsPerMicrosecond = 1000L;
210
212 static const int64_t kMicrosecondsPerSecond = 1000000L;
214
218 static MonoTime Now();
219
221 static MonoTime Max();
222
224 static MonoTime Min();
225
235 static const MonoTime& Earliest(const MonoTime& a, const MonoTime& b)
236 ATTRIBUTE_DEPRECATED("use std::min() instead");
237
240 MonoTime() KUDU_MONOTIME_NOEXCEPT;
241
243 bool Initialized() const;
244
255 MonoDelta GetDeltaSince(const MonoTime &rhs) const ATTRIBUTE_DEPRECATED(
256 "use kudu::operator-(const MonoTime&, const MonoTime&) instead");
257
262 void AddDelta(const MonoDelta &delta);
263
271 bool ComesBefore(const MonoTime &rhs) const;
272
274 std::string ToString() const;
275
281 void ToTimeSpec(struct timespec* ts) const;
282
289 bool Equals(const MonoTime& other) const;
290
293
299 MonoTime& operator+=(const MonoDelta& delta);
300
306 MonoTime& operator-=(const MonoDelta& delta);
308
309 private:
310 friend class MonoDelta;
311 friend MonoDelta operator-(const MonoTime&, const MonoTime&);
312 FRIEND_TEST(TestMonoTime, TestTimeSpec);
313 FRIEND_TEST(TestMonoTime, TestDeltaConversions);
314
315 explicit MonoTime(const struct timespec& ts) KUDU_MONOTIME_NOEXCEPT;
316 explicit MonoTime(int64_t nanos) KUDU_MONOTIME_NOEXCEPT;
317 double ToSeconds() const;
318 int64_t nanos_;
319};
320
331void KUDU_EXPORT SleepFor(const MonoDelta& delta);
332
335
342bool KUDU_EXPORT operator==(const MonoDelta &lhs, const MonoDelta &rhs);
343
350bool KUDU_EXPORT operator!=(const MonoDelta &lhs, const MonoDelta &rhs);
351
358bool KUDU_EXPORT operator<(const MonoDelta &lhs, const MonoDelta &rhs);
359
366bool KUDU_EXPORT operator<=(const MonoDelta &lhs, const MonoDelta &rhs);
367
374bool KUDU_EXPORT operator>(const MonoDelta &lhs, const MonoDelta &rhs);
375
382bool KUDU_EXPORT operator>=(const MonoDelta &lhs, const MonoDelta &rhs);
383
389MonoDelta KUDU_EXPORT operator-(const MonoDelta& lhs, const MonoDelta& rhs);
390
396MonoDelta KUDU_EXPORT operator+(const MonoDelta& lhs, const MonoDelta& rhs);
398
401
412bool KUDU_EXPORT operator==(const MonoTime& lhs, const MonoTime& rhs);
413
425bool KUDU_EXPORT operator!=(const MonoTime& lhs, const MonoTime& rhs);
426
433bool KUDU_EXPORT operator<(const MonoTime& lhs, const MonoTime& rhs);
434
441bool KUDU_EXPORT operator<=(const MonoTime& lhs, const MonoTime& rhs);
442
449bool KUDU_EXPORT operator>(const MonoTime& lhs, const MonoTime& rhs);
450
457bool KUDU_EXPORT operator>=(const MonoTime& lhs, const MonoTime& rhs);
459
462
470MonoTime KUDU_EXPORT operator+(const MonoTime& t, const MonoDelta& delta);
471
479MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta& delta);
480
491MonoDelta KUDU_EXPORT operator-(const MonoTime& t_end, const MonoTime& t_begin);
493
495
506std::ostream& operator<<(std::ostream& os, const kudu::MonoTime& time);
508
509} // namespace kudu
510
511#endif
A representation of a time interval.
Definition monotime.h:58
friend MonoDelta operator+(const MonoDelta &, const MonoDelta &)
int64_t ToNanoseconds() const
MonoDelta & operator-=(const MonoDelta &delta)
MonoDelta & operator+=(const MonoDelta &delta)
void ToTimeVal(struct timeval *tv) const
bool Initialized() const
int64_t ToMicroseconds() const
static void NanosToTimeSpec(int64_t nanos, struct timespec *ts)
bool LessThan(const MonoDelta &rhs) const
static MonoDelta FromSeconds(double seconds)
std::string ToString() const
friend MonoDelta operator-(const MonoDelta &, const MonoDelta &)
int64_t ToMilliseconds() const
double ToSeconds() const
static MonoDelta FromMilliseconds(int64_t ms)
static MonoDelta FromMicroseconds(int64_t us)
void ToTimeSpec(struct timespec *ts) const
bool MoreThan(const MonoDelta &rhs) const
static MonoDelta FromNanoseconds(int64_t ns)
bool Equals(const MonoDelta &rhs) const
Representation of a particular point in time.
Definition monotime.h:197
static MonoTime Max()
MonoTime() KUDU_MONOTIME_NOEXCEPT
static MonoTime Now()
static MonoTime Min()
static const MonoTime & Earliest(const MonoTime &a, const MonoTime &b)