blob: 495fffcfc3e0fd98502fa35b530349e0c6948f97 [file] [log] [blame]
David Garcia Quintas08a0a332016-01-21 01:04:36 -08001/*
2 *
David Garcia Quintasa43aadd2016-01-21 10:01:28 -08003 * Copyright 2015-2016, Google Inc.
David Garcia Quintas08a0a332016-01-21 01:04:36 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef GRPC_IMPL_CODEGEN_TIME_H
35#define GRPC_IMPL_CODEGEN_TIME_H
36/* Time support.
37 We use gpr_timespec, which is analogous to struct timespec. On some
38 machines, absolute times may be in local time. */
39
40#include <grpc/impl/codegen/port_platform.h>
41#include <stddef.h>
42#include <time.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/* The clocks we support. */
49typedef enum {
50 /* Monotonic clock. Epoch undefined. Always moves forwards. */
51 GPR_CLOCK_MONOTONIC = 0,
52 /* Realtime clock. May jump forwards or backwards. Settable by
53 the system administrator. Has its epoch at 0:00:00 UTC 1 Jan 1970. */
54 GPR_CLOCK_REALTIME,
55 /* CPU cycle time obtained by rdtsc instruction on x86 platforms. Epoch
56 undefined. Degrades to GPR_CLOCK_REALTIME on other platforms. */
57 GPR_CLOCK_PRECISE,
58 /* Unmeasurable clock type: no base, created by taking the difference
59 between two times */
60 GPR_TIMESPAN
61} gpr_clock_type;
62
63typedef struct gpr_timespec {
64 int64_t tv_sec;
65 int32_t tv_nsec;
66 /** Against which clock was this time measured? (or GPR_TIMESPAN if
67 this is a relative time meaure) */
68 gpr_clock_type clock_type;
69} gpr_timespec;
70
71/* Time constants. */
Craig Tiller9b426372016-01-29 07:58:22 -080072GPR_API gpr_timespec gpr_time_0(gpr_clock_type type); /* The zero time interval. */
73GPR_API gpr_timespec gpr_inf_future(gpr_clock_type type); /* The far future */
74GPR_API gpr_timespec gpr_inf_past(gpr_clock_type type); /* The far past. */
David Garcia Quintas08a0a332016-01-21 01:04:36 -080075
76#define GPR_MS_PER_SEC 1000
77#define GPR_US_PER_SEC 1000000
78#define GPR_NS_PER_SEC 1000000000
79#define GPR_NS_PER_MS 1000000
80#define GPR_NS_PER_US 1000
81#define GPR_US_PER_MS 1000
82
83/* initialize time subsystem */
Craig Tiller9b426372016-01-29 07:58:22 -080084GPR_API void gpr_time_init(void);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080085
86/* Return the current time measured from the given clocks epoch. */
Craig Tiller9b426372016-01-29 07:58:22 -080087GPR_API gpr_timespec gpr_now(gpr_clock_type clock);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080088
89/* Convert a timespec from one clock to another */
Craig Tiller9b426372016-01-29 07:58:22 -080090GPR_API gpr_timespec gpr_convert_clock_type(gpr_timespec t,
David Garcia Quintas08a0a332016-01-21 01:04:36 -080091 gpr_clock_type target_clock);
92
93/* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b
94 respectively. */
Craig Tiller9b426372016-01-29 07:58:22 -080095GPR_API int gpr_time_cmp(gpr_timespec a, gpr_timespec b);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080096
Craig Tiller9b426372016-01-29 07:58:22 -080097GPR_API gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b);
98GPR_API gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080099
100/* Add and subtract times. Calculations saturate at infinities. */
Craig Tiller9b426372016-01-29 07:58:22 -0800101GPR_API gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b);
102GPR_API gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800103
104/* Return a timespec representing a given number of time units. LONG_MIN is
105 interpreted as gpr_inf_past, and LONG_MAX as gpr_inf_future. */
Craig Tiller9b426372016-01-29 07:58:22 -0800106GPR_API gpr_timespec gpr_time_from_micros(long x, gpr_clock_type clock_type);
107GPR_API gpr_timespec gpr_time_from_nanos(long x, gpr_clock_type clock_type);
108GPR_API gpr_timespec gpr_time_from_millis(long x, gpr_clock_type clock_type);
109GPR_API gpr_timespec gpr_time_from_seconds(long x, gpr_clock_type clock_type);
110GPR_API gpr_timespec gpr_time_from_minutes(long x, gpr_clock_type clock_type);
111GPR_API gpr_timespec gpr_time_from_hours(long x, gpr_clock_type clock_type);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800112
Craig Tiller9b426372016-01-29 07:58:22 -0800113GPR_API int32_t gpr_time_to_millis(gpr_timespec timespec);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800114
115/* Return 1 if two times are equal or within threshold of each other,
116 0 otherwise */
Craig Tiller9b426372016-01-29 07:58:22 -0800117GPR_API int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800118
119/* Sleep until at least 'until' - an absolute timeout */
Craig Tiller9b426372016-01-29 07:58:22 -0800120GPR_API void gpr_sleep_until(gpr_timespec until);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800121
Craig Tiller9b426372016-01-29 07:58:22 -0800122GPR_API double gpr_timespec_to_micros(gpr_timespec t);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800123
124#ifdef __cplusplus
125}
126#endif
127
128#endif /* GRPC_IMPL_CODEGEN_TIME_H */