1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // 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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/util/time_precise.h"
22
23 #ifdef GPR_POSIX_TIME
24
25 #include <stdlib.h>
26 #include <time.h>
27 #include <unistd.h>
28 #ifdef __linux__
29 #include <sys/syscall.h>
30 #endif
31 #include <grpc/support/atm.h>
32 #include <grpc/support/time.h>
33
34 #include "absl/log/check.h"
35 #include "src/core/util/crash.h"
36
timespec_from_gpr(gpr_timespec gts)37 static struct timespec timespec_from_gpr(gpr_timespec gts) {
38 struct timespec rv;
39 if (sizeof(time_t) < sizeof(int64_t)) {
40 // fine to assert, as this is only used in gpr_sleep_until
41 CHECK(gts.tv_sec <= INT32_MAX);
42 CHECK(gts.tv_sec >= INT32_MIN);
43 }
44 rv.tv_sec = static_cast<time_t>(gts.tv_sec);
45 rv.tv_nsec = gts.tv_nsec;
46 return rv;
47 }
48
gpr_from_timespec(struct timespec ts,gpr_clock_type clock_type)49 static gpr_timespec gpr_from_timespec(struct timespec ts,
50 gpr_clock_type clock_type) {
51 //
52 // timespec.tv_sec can have smaller size than gpr_timespec.tv_sec,
53 // but we are only using this function to implement gpr_now
54 // so there's no need to handle "infinity" values.
55 //
56 gpr_timespec rv;
57 rv.tv_sec = ts.tv_sec;
58 rv.tv_nsec = static_cast<int32_t>(ts.tv_nsec);
59 rv.clock_type = clock_type;
60 return rv;
61 }
62
63 /// maps gpr_clock_type --> clockid_t for clock_gettime
64 static const clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC,
65 CLOCK_REALTIME};
66
gpr_time_init(void)67 void gpr_time_init(void) { gpr_precise_clock_init(); }
68
now_impl(gpr_clock_type clock_type)69 static gpr_timespec now_impl(gpr_clock_type clock_type) {
70 struct timespec now;
71 CHECK(clock_type != GPR_TIMESPAN);
72 if (clock_type == GPR_CLOCK_PRECISE) {
73 gpr_timespec ret;
74 gpr_precise_clock_now(&ret);
75 return ret;
76 } else {
77 clock_gettime(clockid_for_gpr_clock[clock_type], &now);
78 if (clock_type == GPR_CLOCK_MONOTONIC) {
79 // Add 5 seconds arbitrarily: avoids weird conditions in
80 // src/core/util/time.cc when there's a small number of seconds returned.
81 now.tv_sec += 5;
82 }
83 return gpr_from_timespec(now, clock_type);
84 }
85 }
86
87 gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl;
88
gpr_now(gpr_clock_type clock_type)89 gpr_timespec gpr_now(gpr_clock_type clock_type) {
90 // validate clock type
91 CHECK(clock_type == GPR_CLOCK_MONOTONIC || clock_type == GPR_CLOCK_REALTIME ||
92 clock_type == GPR_CLOCK_PRECISE);
93 gpr_timespec ts = gpr_now_impl(clock_type);
94 // tv_nsecs must be in the range [0, 1e9).
95 CHECK(ts.tv_nsec >= 0);
96 CHECK(ts.tv_nsec < 1e9);
97 return ts;
98 }
99
gpr_sleep_until(gpr_timespec until)100 void gpr_sleep_until(gpr_timespec until) {
101 gpr_timespec now;
102 gpr_timespec delta;
103 struct timespec delta_ts;
104 int ns_result;
105
106 for (;;) {
107 // We could simplify by using clock_nanosleep instead, but it might be
108 // slightly less portable.
109 now = gpr_now(until.clock_type);
110 if (gpr_time_cmp(until, now) <= 0) {
111 return;
112 }
113
114 delta = gpr_time_sub(until, now);
115 delta_ts = timespec_from_gpr(delta);
116 ns_result = nanosleep(&delta_ts, nullptr);
117 if (ns_result == 0) {
118 break;
119 }
120 }
121 }
122
123 #endif // GPR_POSIX_TIME
124