time_windows.cc
Go to the documentation of this file.
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 
19 /* Win32 code for gpr time support. */
20 
22 
23 #ifdef GPR_WINDOWS_TIME
24 
25 #include <limits.h>
26 #include <process.h>
27 #include <sys/timeb.h>
28 
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 
33 
34 static LARGE_INTEGER g_start_time = []() {
35  LARGE_INTEGER x;
36  QueryPerformanceCounter(&x);
37  return x;
38 }();
39 static double g_time_scale = []() {
40  LARGE_INTEGER frequency;
41  QueryPerformanceFrequency(&frequency);
42  return 1.0 / (double)frequency.QuadPart;
43 }();
44 
45 void gpr_time_init(void) {}
46 
47 static gpr_timespec now_impl(gpr_clock_type clock) {
48  gpr_timespec now_tv;
49  LONGLONG diff;
50  struct _timeb now_tb;
51  LARGE_INTEGER timestamp;
52  double now_dbl;
53  now_tv.clock_type = clock;
54  switch (clock) {
55  case GPR_CLOCK_REALTIME:
56  _ftime_s(&now_tb);
57  now_tv.tv_sec = (int64_t)now_tb.time;
58  now_tv.tv_nsec = now_tb.millitm * 1000000;
59  break;
61  case GPR_CLOCK_PRECISE:
62  QueryPerformanceCounter(&timestamp);
63  diff = timestamp.QuadPart - g_start_time.QuadPart;
64  now_dbl = (double)diff * g_time_scale;
65  now_tv.tv_sec = (int64_t)now_dbl;
66  now_tv.tv_nsec = (int32_t)((now_dbl - (double)now_tv.tv_sec) * 1e9);
67  break;
68  case GPR_TIMESPAN:
69  abort();
70  break;
71  }
72  return now_tv;
73 }
74 
76 
78  return gpr_now_impl(clock_type);
79 }
80 
81 void gpr_sleep_until(gpr_timespec until) {
83  gpr_timespec delta;
84  int64_t sleep_millis;
85 
86  for (;;) {
87  /* We could simplify by using clock_nanosleep instead, but it might be
88  * slightly less portable. */
89  now = gpr_now(until.clock_type);
90  if (gpr_time_cmp(until, now) <= 0) {
91  return;
92  }
93 
94  delta = gpr_time_sub(until, now);
95  sleep_millis =
96  delta.tv_sec * GPR_MS_PER_SEC + delta.tv_nsec / GPR_NS_PER_MS;
97  GPR_ASSERT((sleep_millis >= 0) && (sleep_millis <= INT_MAX));
98  Sleep((DWORD)sleep_millis);
99  }
100 }
101 
102 #endif /* GPR_WINDOWS_TIME */
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: gpr_types.h:52
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
now
static double now(void)
Definition: test/core/fling/client.cc:130
log.h
gpr_now_impl
gpr_timespec(* gpr_now_impl)(gpr_clock_type clock_type)
time.h
now_impl
static gpr_timespec now_impl(gpr_clock_type clock_type)
Definition: filter_fuzzer.cc:41
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:168
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
bm_diff.diff
diff
Definition: bm_diff.py:274
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
GPR_NS_PER_MS
#define GPR_NS_PER_MS
Definition: include/grpc/support/time.h:42
gpr_timespec::clock_type
gpr_clock_type clock_type
Definition: gpr_types.h:55
GPR_CLOCK_PRECISE
@ GPR_CLOCK_PRECISE
Definition: gpr_types.h:42
gpr_timespec
struct gpr_timespec gpr_timespec
GPR_MS_PER_SEC
#define GPR_MS_PER_SEC
Definition: include/grpc/support/time.h:39
gpr_timespec
Definition: gpr_types.h:50
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
gpr_time_init
GPRAPI void gpr_time_init(void)
gpr_clock_type
gpr_clock_type
Definition: gpr_types.h:34
time_precise.h
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:37