David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | a43aadd | 2016-01-21 10:01:28 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 4 | * 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 |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /* The clocks we support. */ |
| 49 | typedef 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 | |
| 63 | typedef 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 Tiller | d6546c9 | 2016-01-29 07:59:35 -0800 | [diff] [blame] | 72 | GPR_API gpr_timespec |
| 73 | gpr_time_0(gpr_clock_type type); /* The zero time interval. */ |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 74 | GPR_API gpr_timespec gpr_inf_future(gpr_clock_type type); /* The far future */ |
| 75 | GPR_API gpr_timespec gpr_inf_past(gpr_clock_type type); /* The far past. */ |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 76 | |
| 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 Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 85 | GPR_API void gpr_time_init(void); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 86 | |
| 87 | /* Return the current time measured from the given clocks epoch. */ |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 88 | GPR_API gpr_timespec gpr_now(gpr_clock_type clock); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 89 | |
| 90 | /* Convert a timespec from one clock to another */ |
Craig Tiller | d6546c9 | 2016-01-29 07:59:35 -0800 | [diff] [blame] | 91 | GPR_API gpr_timespec |
| 92 | gpr_convert_clock_type(gpr_timespec t, gpr_clock_type target_clock); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 93 | |
| 94 | /* Return -ve, 0, or +ve according to whether a < b, a == b, or a > b |
| 95 | respectively. */ |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 96 | GPR_API int gpr_time_cmp(gpr_timespec a, gpr_timespec b); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 97 | |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 98 | GPR_API gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b); |
| 99 | GPR_API gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 100 | |
| 101 | /* Add and subtract times. Calculations saturate at infinities. */ |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 102 | GPR_API gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b); |
| 103 | GPR_API gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 104 | |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame^] | 105 | /* 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. */ |
| 107 | GPR_API gpr_timespec gpr_time_from_micros(int64_t x, gpr_clock_type clock_type); |
| 108 | GPR_API gpr_timespec gpr_time_from_nanos(int64_t x, gpr_clock_type clock_type); |
| 109 | GPR_API gpr_timespec gpr_time_from_millis(int64_t x, gpr_clock_type clock_type); |
| 110 | GPR_API gpr_timespec gpr_time_from_seconds(int64_t x, gpr_clock_type clock_type); |
| 111 | GPR_API gpr_timespec gpr_time_from_minutes(int64_t x, gpr_clock_type clock_type); |
| 112 | GPR_API gpr_timespec gpr_time_from_hours(int64_t x, gpr_clock_type clock_type); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 113 | |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 114 | GPR_API int32_t gpr_time_to_millis(gpr_timespec timespec); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 115 | |
| 116 | /* Return 1 if two times are equal or within threshold of each other, |
| 117 | 0 otherwise */ |
Craig Tiller | d6546c9 | 2016-01-29 07:59:35 -0800 | [diff] [blame] | 118 | GPR_API int gpr_time_similar(gpr_timespec a, gpr_timespec b, |
| 119 | gpr_timespec threshold); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 120 | |
| 121 | /* Sleep until at least 'until' - an absolute timeout */ |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 122 | GPR_API void gpr_sleep_until(gpr_timespec until); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 123 | |
Craig Tiller | 9b42637 | 2016-01-29 07:58:22 -0800 | [diff] [blame] | 124 | GPR_API double gpr_timespec_to_micros(gpr_timespec t); |
David Garcia Quintas | 08a0a33 | 2016-01-21 01:04:36 -0800 | [diff] [blame] | 125 | |
| 126 | #ifdef __cplusplus |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #endif /* GRPC_IMPL_CODEGEN_TIME_H */ |