blob: 62fdd40af704c007cd0f6d74730c9391ef8b4220 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19/* Win32 code for gpr synchronization support. */
20
jtattermusch98bffb72014-12-09 12:47:19 -080021#include <grpc/support/port_platform.h>
22
Yuchen Zeng12dfdc32016-04-26 22:05:41 -070023#ifdef GPR_WINDOWS
jtattermusch98bffb72014-12-09 12:47:19 -080024
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025#include <grpc/support/log.h>
26#include <grpc/support/sync.h>
27#include <grpc/support/time.h>
28
Craig Tillera82950e2015-09-22 12:33:20 -070029void gpr_mu_init(gpr_mu *mu) {
30 InitializeCriticalSection(&mu->cs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080031 mu->locked = 0;
32}
33
Craig Tillera82950e2015-09-22 12:33:20 -070034void gpr_mu_destroy(gpr_mu *mu) { DeleteCriticalSection(&mu->cs); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Craig Tillera82950e2015-09-22 12:33:20 -070036void gpr_mu_lock(gpr_mu *mu) {
37 EnterCriticalSection(&mu->cs);
38 GPR_ASSERT(!mu->locked);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039 mu->locked = 1;
40}
41
Craig Tillera82950e2015-09-22 12:33:20 -070042void gpr_mu_unlock(gpr_mu *mu) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 mu->locked = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070044 LeaveCriticalSection(&mu->cs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045}
46
Craig Tillera82950e2015-09-22 12:33:20 -070047int gpr_mu_trylock(gpr_mu *mu) {
48 int result = TryEnterCriticalSection(&mu->cs);
49 if (result) {
50 if (mu->locked) { /* This thread already holds the lock. */
51 LeaveCriticalSection(&mu->cs); /* Decrement lock count. */
52 result = 0; /* Indicate failure */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053 }
Craig Tillera82950e2015-09-22 12:33:20 -070054 mu->locked = 1;
55 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056 return result;
57}
58
59/*----------------------------------------*/
60
Craig Tillera82950e2015-09-22 12:33:20 -070061void gpr_cv_init(gpr_cv *cv) { InitializeConditionVariable(cv); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Craig Tillera82950e2015-09-22 12:33:20 -070063void gpr_cv_destroy(gpr_cv *cv) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064 /* Condition variables don't need destruction in Win32. */
65}
66
Craig Tillera82950e2015-09-22 12:33:20 -070067int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068 int timeout = 0;
jtattermusch275b3ac2014-12-09 15:54:14 -080069 DWORD timeout_max_ms;
nnoble4eec0172014-12-11 11:07:51 -080070 mu->locked = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070071 if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
72 0) {
73 SleepConditionVariableCS(cv, &mu->cs, INFINITE);
74 } else {
Alistair Veitch0a7468a2016-02-05 13:13:13 -080075 abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
Craig Tillera82950e2015-09-22 12:33:20 -070076 gpr_timespec now = gpr_now(abs_deadline.clock_type);
Craig Tiller7536af02015-12-22 13:49:30 -080077 int64_t now_ms = (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000;
78 int64_t deadline_ms =
79 (int64_t)abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000;
Craig Tillera82950e2015-09-22 12:33:20 -070080 if (now_ms >= deadline_ms) {
81 timeout = 1;
82 } else {
Nicolas "Pixel" Noble742eac12016-01-26 22:41:19 +010083 if ((deadline_ms - now_ms) >= INFINITE) {
84 timeout_max_ms = INFINITE - 1;
85 } else {
86 timeout_max_ms = (DWORD)(deadline_ms - now_ms);
87 }
Craig Tillera82950e2015-09-22 12:33:20 -070088 timeout = (SleepConditionVariableCS(cv, &mu->cs, timeout_max_ms) == 0 &&
89 GetLastError() == ERROR_TIMEOUT);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 }
Craig Tillera82950e2015-09-22 12:33:20 -070091 }
nnoble4eec0172014-12-11 11:07:51 -080092 mu->locked = 1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 return timeout;
94}
95
Craig Tillera82950e2015-09-22 12:33:20 -070096void gpr_cv_signal(gpr_cv *cv) { WakeConditionVariable(cv); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097
Craig Tillera82950e2015-09-22 12:33:20 -070098void gpr_cv_broadcast(gpr_cv *cv) { WakeAllConditionVariable(cv); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099
100/*----------------------------------------*/
101
102static void *dummy;
Craig Tillera82950e2015-09-22 12:33:20 -0700103struct run_once_func_arg {
104 void (*init_function)(void);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105};
Craig Tillera82950e2015-09-22 12:33:20 -0700106static BOOL CALLBACK run_once_func(gpr_once *once, void *v, void **pv) {
Yash Tibrewal922bc8a2017-09-22 16:32:41 -0700107 struct run_once_func_arg *arg = (struct run_once_func_arg *)v;
Craig Tillera82950e2015-09-22 12:33:20 -0700108 (*arg->init_function)();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 return 1;
110}
111
Craig Tillera82950e2015-09-22 12:33:20 -0700112void gpr_once_init(gpr_once *once, void (*init_function)(void)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 struct run_once_func_arg arg;
114 arg.init_function = init_function;
Craig Tillera82950e2015-09-22 12:33:20 -0700115 InitOnceExecuteOnce(once, run_once_func, &arg, &dummy);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116}
jtattermusch98bffb72014-12-09 12:47:19 -0800117
Yuchen Zeng12dfdc32016-04-26 22:05:41 -0700118#endif /* GPR_WINDOWS */