This repository was archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutil.h
96 lines (85 loc) · 2.72 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CLERK_UTIL_H_
#define CLERK_UTIL_H_
#include <condition_variable>
#include <mutex>
using namespace std;
class Notification {
public:
Notification() : done_(false) {}
bool HasBeenNotified() {
std::unique_lock<mutex> ml(mu_);
return done_;
}
void Notify() {
std::unique_lock<mutex> ml(mu_);
done_ = true;
}
private:
bool done_;
std::mutex mu_;
std::condition_variable cond_;
};
#define DISALLOW_COPY_AND_ASSIGN(Type) \
Type(const Type&) {} \
Type(const Type&&) {} \
void operator=(const Type&) {}
#define FALLTHROUGH_INTENDED \
do { \
} while (0)
extern struct timespec clerk_clock_realtime, clerk_clock_monotonic;
extern clockid_t clerk_clock_mono_id;
inline bool InitTime() {
clock_gettime(CLOCK_REALTIME, &clerk_clock_realtime);
#ifdef CLOCK_MONOTONIC_RAW
// If monotonic raw clock is supported and available, let's use that.
if (!clock_gettime(CLOCK_MONOTONIC_RAW, &clerk_clock_monotonic)) {
clerk_clock_mono_id = CLOCK_MONOTONIC_RAW;
return true;
}
#endif
clock_gettime(CLOCK_MONOTONIC, &clerk_clock_monotonic);
return true;
}
extern bool clerk_run_init_time;
const int64_t kNumMillisPerSecond = 1000LL;
const int64_t kNumNanosPerMilli = 1000000LL;
const int64_t kNumNanosPerSecond = 1000000000LL;
inline int64_t GetCurrentTimeNanos() {
struct timespec tv;
clock_gettime(clerk_clock_mono_id, &tv);
int64_t secs =
clerk_clock_realtime.tv_sec - clerk_clock_monotonic.tv_sec + tv.tv_sec;
int64_t nsecs =
clerk_clock_realtime.tv_nsec - clerk_clock_monotonic.tv_nsec + tv.tv_nsec;
return secs * kNumNanosPerSecond + nsecs;
}
inline double GetCurrentTimeSeconds() {
return GetCurrentTimeNanos() * 1.0L / kNumNanosPerSecond;
}
inline void SleepForNanoseconds(int64_t nanos) {
if (nanos <= 0) {
return;
}
struct timespec tv;
tv.tv_sec = nanos / kNumNanosPerSecond;
tv.tv_nsec = nanos % kNumNanosPerSecond;
while (EINTR == clock_nanosleep(CLOCK_MONOTONIC, 0, &tv, &tv)) {
}
}
inline void SleepForSeconds(double seconds) {
SleepForNanoseconds(seconds * kNumNanosPerSecond);
}
#endif // CLERK_UTIL_H_