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 synchronization support.
20
21 #include <grpc/support/port_platform.h>
22
23 #if defined(GPR_WINDOWS) && !defined(GPR_ABSEIL_SYNC) && \
24 !defined(GPR_CUSTOM_SYNC)
25
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 #include <grpc/support/time.h>
29
30 #include "src/core/lib/gprpp/crash.h"
31
gpr_mu_init(gpr_mu * mu)32 void gpr_mu_init(gpr_mu* mu) {
33 InitializeCriticalSection(&mu->cs);
34 mu->locked = 0;
35 }
36
gpr_mu_destroy(gpr_mu * mu)37 void gpr_mu_destroy(gpr_mu* mu) { DeleteCriticalSection(&mu->cs); }
38
gpr_mu_lock(gpr_mu * mu)39 void gpr_mu_lock(gpr_mu* mu) {
40 EnterCriticalSection(&mu->cs);
41 GPR_ASSERT(!mu->locked);
42 mu->locked = 1;
43 }
44
gpr_mu_unlock(gpr_mu * mu)45 void gpr_mu_unlock(gpr_mu* mu) {
46 mu->locked = 0;
47 LeaveCriticalSection(&mu->cs);
48 }
49
gpr_mu_trylock(gpr_mu * mu)50 int gpr_mu_trylock(gpr_mu* mu) {
51 int result = TryEnterCriticalSection(&mu->cs);
52 if (result) {
53 if (mu->locked) { // This thread already holds the lock.
54 LeaveCriticalSection(&mu->cs); // Decrement lock count.
55 result = 0; // Indicate failure
56 }
57 mu->locked = 1;
58 }
59 return result;
60 }
61
62 //----------------------------------------
63
gpr_cv_init(gpr_cv * cv)64 void gpr_cv_init(gpr_cv* cv) { InitializeConditionVariable(cv); }
65
gpr_cv_destroy(gpr_cv * cv)66 void gpr_cv_destroy(gpr_cv* cv) {
67 // Condition variables don't need destruction in Win32.
68 }
69
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)70 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
71 int timeout = 0;
72 DWORD timeout_max_ms;
73 mu->locked = 0;
74 if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
75 0) {
76 SleepConditionVariableCS(cv, &mu->cs, INFINITE);
77 } else {
78 abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
79 gpr_timespec now = gpr_now(abs_deadline.clock_type);
80 int64_t now_ms = (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000;
81 int64_t deadline_ms =
82 (int64_t)abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000;
83 if (now_ms >= deadline_ms) {
84 timeout = 1;
85 } else {
86 if ((deadline_ms - now_ms) >= INFINITE) {
87 timeout_max_ms = INFINITE - 1;
88 } else {
89 timeout_max_ms = (DWORD)(deadline_ms - now_ms);
90 }
91 timeout = (SleepConditionVariableCS(cv, &mu->cs, timeout_max_ms) == 0 &&
92 GetLastError() == ERROR_TIMEOUT);
93 }
94 }
95 mu->locked = 1;
96 return timeout;
97 }
98
gpr_cv_signal(gpr_cv * cv)99 void gpr_cv_signal(gpr_cv* cv) { WakeConditionVariable(cv); }
100
gpr_cv_broadcast(gpr_cv * cv)101 void gpr_cv_broadcast(gpr_cv* cv) { WakeAllConditionVariable(cv); }
102
103 //----------------------------------------
104
105 static void* phony;
106 struct run_once_func_arg {
107 void (*init_function)(void);
108 };
run_once_func(gpr_once * once,void * v,void ** pv)109 static BOOL CALLBACK run_once_func(gpr_once* once, void* v, void** pv) {
110 struct run_once_func_arg* arg = (struct run_once_func_arg*)v;
111 (*arg->init_function)();
112 return 1;
113 }
114
gpr_once_init(gpr_once * once,void (* init_function)(void))115 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
116 struct run_once_func_arg arg;
117 arg.init_function = init_function;
118 InitOnceExecuteOnce(once, run_once_func, &arg, &phony);
119 }
120
121 #endif // defined(GPR_WINDOWS) && !defined(GPR_ABSEIL_SYNC) &&
122 // !defined(GPR_CUSTOM_SYNC)
123