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