sync_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 synchronization support. */
20 
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 void gpr_mu_init(gpr_mu* mu) {
31  InitializeCriticalSection(&mu->cs);
32  mu->locked = 0;
33 }
34 
35 void gpr_mu_destroy(gpr_mu* mu) { DeleteCriticalSection(&mu->cs); }
36 
37 void gpr_mu_lock(gpr_mu* mu) {
38  EnterCriticalSection(&mu->cs);
39  GPR_ASSERT(!mu->locked);
40  mu->locked = 1;
41 }
42 
43 void gpr_mu_unlock(gpr_mu* mu) {
44  mu->locked = 0;
45  LeaveCriticalSection(&mu->cs);
46 }
47 
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 
62 void gpr_cv_init(gpr_cv* cv) { InitializeConditionVariable(cv); }
63 
64 void gpr_cv_destroy(gpr_cv* cv) {
65  /* Condition variables don't need destruction in Win32. */
66 }
67 
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 
97 void gpr_cv_signal(gpr_cv* cv) { WakeConditionVariable(cv); }
98 
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 };
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 
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) */
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: gpr_types.h:52
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
now
static double now(void)
Definition: test/core/fling/client.cc:130
log.h
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
time.h
BOOL
int BOOL
Definition: undname.c:46
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
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_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
arg
Definition: cmdline.cc:40
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
gpr_timespec::clock_type
gpr_clock_type clock_type
Definition: gpr_types.h:55
once
absl::once_flag once
Definition: bm_opencensus_plugin.cc:38
cv
unsigned cv
Definition: cxa_demangle.cpp:4908
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
gpr_convert_clock_type
GPRAPI gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:241
gpr_mu_trylock
GPRAPI int gpr_mu_trylock(gpr_mu *mu)
gpr_cv_broadcast
GPRAPI void gpr_cv_broadcast(gpr_cv *cv)
gpr_timespec
Definition: gpr_types.h:50
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
sync.h
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
port_platform.h


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