blob: 21fd9dedef9718b207ae7180878e3ffdfb0ca7c7 [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 Tillerd6546c92016-01-29 07:59:35 -080072GPR_API gpr_timespec
73gpr_time_0(gpr_clock_type type); /* The zero time interval. */
Craig Tiller9b426372016-01-29 07:58:22 -080074GPR_API gpr_timespec gpr_inf_future(gpr_clock_type type); /* The far future */
75GPR_API gpr_timespec gpr_inf_past(gpr_clock_type type); /* The far past. */
David Garcia Quintas08a0a332016-01-21 01:04:36 -080076
77#define GPR_MS_PER_SEC 1000
78#define GPR_US_PER_SEC 1000000
79#define GPR_NS_PER_SEC 1000000000
80#define GPR_NS_PER_MS 1000000
81#define GPR_NS_PER_US 1000
82#define GPR_US_PER_MS 1000
83
84/* initialize time subsystem */
Craig Tiller9b426372016-01-29 07:58:22 -080085GPR_API void gpr_time_init(void);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080086
87/* Return the current time measured from the given clocks epoch. */
Craig Tiller9b426372016-01-29 07:58:22 -080088GPR_API gpr_timespec gpr_now(gpr_clock_type clock);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080089
90/* Convert a timespec from one clock to another */
Craig Tillerd6546c92016-01-29 07:59:35 -080091GPR_API gpr_timespec
92gpr_convert_clock_type(gpr_timespec t, gpr_clock_type target_clock);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080093
94/* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b
95 respectively. */
Craig Tiller9b426372016-01-29 07:58:22 -080096GPR_API int gpr_time_cmp(gpr_timespec a, gpr_timespec b);
David Garcia Quintas08a0a332016-01-21 01:04:36 -080097
Craig Tiller9b426372016-01-29 07:58:22 -080098GPR_API gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b);
99GPR_API gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800100
101/* Add and subtract times. Calculations saturate at infinities. */
Craig Tiller9b426372016-01-29 07:58:22 -0800102GPR_API gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b);
103GPR_API gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800104
murgatroid99309830f2016-02-05 11:30:00 -0800105/* Return a timespec representing a given number of time units. INT64_MIN is
106 interpreted as gpr_inf_past, and INT64_MAX as gpr_inf_future. */
107GPR_API gpr_timespec gpr_time_from_micros(int64_t x, gpr_clock_type clock_type);
108GPR_API gpr_timespec gpr_time_from_nanos(int64_t x, gpr_clock_type clock_type);
109GPR_API gpr_timespec gpr_time_from_millis(int64_t x, gpr_clock_type clock_type);
110GPR_API gpr_timespec gpr_time_from_seconds(int64_t x, gpr_clock_type clock_type);
111GPR_API gpr_timespec gpr_time_from_minutes(int64_t x, gpr_clock_type clock_type);
112GPR_API gpr_timespec gpr_time_from_hours(int64_t x, gpr_clock_type clock_type);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800113
Craig Tiller9b426372016-01-29 07:58:22 -0800114GPR_API int32_t gpr_time_to_millis(gpr_timespec timespec);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800115
116/* Return 1 if two times are equal or within threshold of each other,
117 0 otherwise */
Craig Tillerd6546c92016-01-29 07:59:35 -0800118GPR_API int gpr_time_similar(gpr_timespec a, gpr_timespec b,
119 gpr_timespec threshold);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800120
121/* Sleep until at least 'until' - an absolute timeout */
Craig Tiller9b426372016-01-29 07:58:22 -0800122GPR_API void gpr_sleep_until(gpr_timespec until);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800123
Craig Tiller9b426372016-01-29 07:58:22 -0800124GPR_API double gpr_timespec_to_micros(gpr_timespec t);
David Garcia Quintas08a0a332016-01-21 01:04:36 -0800125
126#ifdef __cplusplus
127}
128#endif
129
130#endif /* GRPC_IMPL_CODEGEN_TIME_H */