| 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 |
|
| 21 |
#include <grpc/support/port_platform.h> |
| 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 |
|
| 32 |
#include "src/core/lib/gpr/time_precise.h" |
| 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; |
| 60 |
case GPR_CLOCK_MONOTONIC: |
| 61 |
case GPR_CLOCK_PRECISE: |
| 62 |
QueryPerformanceCounter(×tamp); |
| 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 |
|
| 75 |
gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type) = now_impl; |
| 76 |
|
| 77 |
gpr_timespec gpr_now(gpr_clock_type clock_type) { |
| 78 |
return gpr_now_impl(clock_type); |
| 79 |
} |
| 80 |
|
| 81 |
void gpr_sleep_until(gpr_timespec until) { |
| 82 |
gpr_timespec now; |
| 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 */ |