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/lib/gpr/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/log.h>
33 #include <grpc/support/time.h>
34
35 #include "src/core/lib/gprpp/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 GPR_ASSERT(gts.tv_sec <= INT32_MAX && gts.tv_sec >= INT32_MIN);
42 }
43 rv.tv_sec = static_cast<time_t>(gts.tv_sec);
44 rv.tv_nsec = gts.tv_nsec;
45 return rv;
46 }
47
gpr_from_timespec(struct timespec ts,gpr_clock_type clock_type)48 static gpr_timespec gpr_from_timespec(struct timespec ts,
49 gpr_clock_type clock_type) {
50 //
51 // timespec.tv_sec can have smaller size than gpr_timespec.tv_sec,
52 // but we are only using this function to implement gpr_now
53 // so there's no need to handle "infinity" values.
54 //
55 gpr_timespec rv;
56 rv.tv_sec = ts.tv_sec;
57 rv.tv_nsec = static_cast<int32_t>(ts.tv_nsec);
58 rv.clock_type = clock_type;
59 return rv;
60 }
61
62 /// maps gpr_clock_type --> clockid_t for clock_gettime
63 static const clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC,
64 CLOCK_REALTIME};
65
gpr_time_init(void)66 void gpr_time_init(void) { gpr_precise_clock_init(); }
67
now_impl(gpr_clock_type clock_type)68 static gpr_timespec now_impl(gpr_clock_type clock_type) {
69 struct timespec now;
70 GPR_ASSERT(clock_type != GPR_TIMESPAN);
71 if (clock_type == GPR_CLOCK_PRECISE) {
72 gpr_timespec ret;
73 gpr_precise_clock_now(&ret);
74 return ret;
75 } else {
76 clock_gettime(clockid_for_gpr_clock[clock_type], &now);
77 if (clock_type == GPR_CLOCK_MONOTONIC) {
78 // Add 5 seconds arbitrarily: avoids weird conditions in gprpp/time.cc
79 // when there's a small number of seconds returned.
80 now.tv_sec += 5;
81 }
82 return gpr_from_timespec(now, clock_type);
83 }
84 }
85
86 gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl;
87
gpr_now(gpr_clock_type clock_type)88 gpr_timespec gpr_now(gpr_clock_type clock_type) {
89 // validate clock type
90 GPR_ASSERT(clock_type == GPR_CLOCK_MONOTONIC ||
91 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 GPR_ASSERT(ts.tv_nsec >= 0 && ts.tv_nsec < 1e9);
96 return ts;
97 }
98
gpr_sleep_until(gpr_timespec until)99 void gpr_sleep_until(gpr_timespec until) {
100 gpr_timespec now;
101 gpr_timespec delta;
102 struct timespec delta_ts;
103 int ns_result;
104
105 for (;;) {
106 // We could simplify by using clock_nanosleep instead, but it might be
107 // slightly less portable.
108 now = gpr_now(until.clock_type);
109 if (gpr_time_cmp(until, now) <= 0) {
110 return;
111 }
112
113 delta = gpr_time_sub(until, now);
114 delta_ts = timespec_from_gpr(delta);
115 ns_result = nanosleep(&delta_ts, nullptr);
116 if (ns_result == 0) {
117 break;
118 }
119 }
120 }
121
122 #endif // GPR_POSIX_TIME
123