time.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef GRPC_SUPPORT_TIME_H
  19. #define GRPC_SUPPORT_TIME_H
  20. #include <grpc/support/port_platform.h>
  21. #include <stddef.h>
  22. #include <time.h>
  23. #include <grpc/impl/codegen/gpr_types.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /** Time constants. */
  28. GPRAPI gpr_timespec
  29. gpr_time_0(gpr_clock_type type); /** The zero time interval. */
  30. GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type); /** The far future */
  31. GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type); /** The far past. */
  32. #define GPR_MS_PER_SEC 1000
  33. #define GPR_US_PER_SEC 1000000
  34. #define GPR_NS_PER_SEC 1000000000
  35. #define GPR_NS_PER_MS 1000000
  36. #define GPR_NS_PER_US 1000
  37. #define GPR_US_PER_MS 1000
  38. /** initialize time subsystem */
  39. GPRAPI void gpr_time_init(void);
  40. /** Return the current time measured from the given clocks epoch. */
  41. GPRAPI gpr_timespec gpr_now(gpr_clock_type clock);
  42. /** Convert a timespec from one clock to another */
  43. GPRAPI gpr_timespec gpr_convert_clock_type(gpr_timespec t,
  44. gpr_clock_type clock_type);
  45. /** Return -ve, 0, or +ve according to whether a < b, a == b, or a > b
  46. respectively. */
  47. GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b);
  48. GPRAPI gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b);
  49. GPRAPI gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b);
  50. /** Add and subtract times. Calculations saturate at infinities. */
  51. GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b);
  52. GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b);
  53. /** Return a timespec representing a given number of time units. INT64_MIN is
  54. interpreted as gpr_inf_past, and INT64_MAX as gpr_inf_future. */
  55. GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type);
  56. GPRAPI gpr_timespec gpr_time_from_nanos(int64_t ns, gpr_clock_type clock_type);
  57. GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type);
  58. GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type);
  59. GPRAPI gpr_timespec gpr_time_from_minutes(int64_t m, gpr_clock_type clock_type);
  60. GPRAPI gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type clock_type);
  61. GPRAPI int32_t gpr_time_to_millis(gpr_timespec timespec);
  62. /** Return 1 if two times are equal or within threshold of each other,
  63. 0 otherwise */
  64. GPRAPI int gpr_time_similar(gpr_timespec a, gpr_timespec b,
  65. gpr_timespec threshold);
  66. /** Sleep until at least 'until' - an absolute timeout */
  67. GPRAPI void gpr_sleep_until(gpr_timespec until);
  68. GPRAPI double gpr_timespec_to_micros(gpr_timespec t);
  69. #ifdef __cplusplus
  70. }
  71. #endif
  72. #endif /* GRPC_SUPPORT_TIME_H */