time.h 3.1 KB

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