xref: /aosp_15_r20/external/grpc-grpc/include/grpc/support/time.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 #ifndef GRPC_SUPPORT_TIME_H
20 #define GRPC_SUPPORT_TIME_H
21 
22 #include <stddef.h>
23 #include <time.h>
24 
25 #include <grpc/support/port_platform.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /** The clocks we support. */
32 typedef enum {
33   /** Monotonic clock. Epoch undefined. Always moves forwards. */
34   GPR_CLOCK_MONOTONIC = 0,
35   /** Realtime clock. May jump forwards or backwards. Settable by
36      the system administrator. Has its epoch at 0:00:00 UTC 1 Jan 1970. */
37   GPR_CLOCK_REALTIME,
38   /** CPU cycle time obtained by rdtsc instruction on x86 platforms. Epoch
39      undefined. Degrades to GPR_CLOCK_REALTIME on other platforms. */
40   GPR_CLOCK_PRECISE,
41   /** Unmeasurable clock type: no base, created by taking the difference
42      between two times */
43   GPR_TIMESPAN
44 } gpr_clock_type;
45 
46 /** Analogous to struct timespec. On some machines, absolute times may be in
47  * local time. */
48 typedef struct gpr_timespec {
49   int64_t tv_sec;
50   int32_t tv_nsec;
51   /** Against which clock was this time measured? (or GPR_TIMESPAN if
52       this is a relative time measure) */
53   gpr_clock_type clock_type;
54 } gpr_timespec;
55 
56 /** Time constants. */
57 /** The zero time interval. */
58 GPRAPI gpr_timespec gpr_time_0(gpr_clock_type type);
59 /** The far future */
60 GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type);
61 /** The far past. */
62 GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type);
63 
64 #define GPR_MS_PER_SEC 1000
65 #define GPR_US_PER_SEC 1000000
66 #define GPR_NS_PER_SEC 1000000000
67 #define GPR_NS_PER_MS 1000000
68 #define GPR_NS_PER_US 1000
69 #define GPR_US_PER_MS 1000
70 
71 /** initialize time subsystem */
72 GPRAPI void gpr_time_init(void);
73 
74 /** Return the current time measured from the given clocks epoch. */
75 GPRAPI gpr_timespec gpr_now(gpr_clock_type clock);
76 
77 /** Convert a timespec from one clock to another */
78 GPRAPI gpr_timespec gpr_convert_clock_type(gpr_timespec t,
79                                            gpr_clock_type clock_type);
80 
81 /** Return -ve, 0, or +ve according to whether a < b, a == b, or a > b
82    respectively.  */
83 GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b);
84 
85 GPRAPI gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b);
86 GPRAPI gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b);
87 
88 /** Add and subtract times.  Calculations saturate at infinities. */
89 GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b);
90 GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b);
91 
92 /** Return a timespec representing a given number of time units. INT64_MIN is
93    interpreted as gpr_inf_past, and INT64_MAX as gpr_inf_future.  */
94 GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type);
95 GPRAPI gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type clock_type);
96 GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type);
97 GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type);
98 GPRAPI gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type clock_type);
99 GPRAPI gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type clock_type);
100 
101 GPRAPI int32_t gpr_time_to_millis(gpr_timespec timespec);
102 
103 /** Return 1 if two times are equal or within threshold of each other,
104    0 otherwise */
105 GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b,
106                             gpr_timespec threshold);
107 
108 /** Sleep until at least 'until' - an absolute timeout */
109 GPRAPI void gpr_sleep_until(gpr_timespec until);
110 
111 GPRAPI double gpr_timespec_to_micros(gpr_timespec t);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif /* GRPC_SUPPORT_TIME_H */
118