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 #include <grpc/support/port_platform.h>
20
21 #if defined(GPR_POSIX_SYNC) && !defined(GPR_ABSEIL_SYNC) && \
22 !defined(GPR_CUSTOM_SYNC)
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/sync.h>
27 #include <grpc/support/time.h>
28
29 #include <errno.h>
30 #include <time.h>
31
32 #include "src/core/lib/profiling/timers.h"
33
34 #ifdef GPR_LOW_LEVEL_COUNTERS
35 gpr_atm gpr_mu_locks = 0;
36 gpr_atm gpr_counter_atm_cas = 0;
37 gpr_atm gpr_counter_atm_add = 0;
38 #endif
39
gpr_mu_init(gpr_mu * mu)40 void gpr_mu_init(gpr_mu* mu) {
41 #ifdef GRPC_ASAN_ENABLED
42 GPR_ASSERT(pthread_mutex_init(&mu->mutex, nullptr) == 0);
43 mu->leak_checker = static_cast<int*>(malloc(sizeof(*mu->leak_checker)));
44 GPR_ASSERT(mu->leak_checker != nullptr);
45 #else
46 GPR_ASSERT(pthread_mutex_init(mu, nullptr) == 0);
47 #endif
48 }
49
gpr_mu_destroy(gpr_mu * mu)50 void gpr_mu_destroy(gpr_mu* mu) {
51 #ifdef GRPC_ASAN_ENABLED
52 GPR_ASSERT(pthread_mutex_destroy(&mu->mutex) == 0);
53 free(mu->leak_checker);
54 #else
55 GPR_ASSERT(pthread_mutex_destroy(mu) == 0);
56 #endif
57 }
58
gpr_mu_lock(gpr_mu * mu)59 void gpr_mu_lock(gpr_mu* mu) {
60 #ifdef GPR_LOW_LEVEL_COUNTERS
61 GPR_ATM_INC_COUNTER(gpr_mu_locks);
62 #endif
63 GPR_TIMER_SCOPE("gpr_mu_lock", 0);
64 #ifdef GRPC_ASAN_ENABLED
65 GPR_ASSERT(pthread_mutex_lock(&mu->mutex) == 0);
66 #else
67 GPR_ASSERT(pthread_mutex_lock(mu) == 0);
68 #endif
69 }
70
gpr_mu_unlock(gpr_mu * mu)71 void gpr_mu_unlock(gpr_mu* mu) {
72 GPR_TIMER_SCOPE("gpr_mu_unlock", 0);
73 #ifdef GRPC_ASAN_ENABLED
74 GPR_ASSERT(pthread_mutex_unlock(&mu->mutex) == 0);
75 #else
76 GPR_ASSERT(pthread_mutex_unlock(mu) == 0);
77 #endif
78 }
79
gpr_mu_trylock(gpr_mu * mu)80 int gpr_mu_trylock(gpr_mu* mu) {
81 GPR_TIMER_SCOPE("gpr_mu_trylock", 0);
82 int err = 0;
83 #ifdef GRPC_ASAN_ENABLED
84 err = pthread_mutex_trylock(&mu->mutex);
85 #else
86 err = pthread_mutex_trylock(mu);
87 #endif
88 GPR_ASSERT(err == 0 || err == EBUSY);
89 return err == 0;
90 }
91
92 /*----------------------------------------*/
93
gpr_cv_init(gpr_cv * cv)94 void gpr_cv_init(gpr_cv* cv) {
95 pthread_condattr_t attr;
96 GPR_ASSERT(pthread_condattr_init(&attr) == 0);
97 #if GPR_LINUX
98 GPR_ASSERT(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0);
99 #endif // GPR_LINUX
100
101 #ifdef GRPC_ASAN_ENABLED
102 GPR_ASSERT(pthread_cond_init(&cv->cond_var, &attr) == 0);
103 cv->leak_checker = static_cast<int*>(malloc(sizeof(*cv->leak_checker)));
104 GPR_ASSERT(cv->leak_checker != nullptr);
105 #else
106 GPR_ASSERT(pthread_cond_init(cv, &attr) == 0);
107 #endif
108 }
109
gpr_cv_destroy(gpr_cv * cv)110 void gpr_cv_destroy(gpr_cv* cv) {
111 #ifdef GRPC_ASAN_ENABLED
112 GPR_ASSERT(pthread_cond_destroy(&cv->cond_var) == 0);
113 free(cv->leak_checker);
114 #else
115 GPR_ASSERT(pthread_cond_destroy(cv) == 0);
116 #endif
117 }
118
119 #define gpr_convert_clock_type_debug(t, clock_type, now1, now2, add_result, \
120 sub_result) \
121 gpr_convert_clock_type((t), (clock_type))
122
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)123 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
124 int err = 0;
125 if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
126 0) {
127 #ifdef GRPC_ASAN_ENABLED
128 err = pthread_cond_wait(&cv->cond_var, &mu->mutex);
129 #else
130 err = pthread_cond_wait(cv, mu);
131 #endif
132 } else {
133 struct timespec abs_deadline_ts;
134 #if GPR_LINUX
135 abs_deadline = gpr_convert_clock_type_debug(
136 abs_deadline, GPR_CLOCK_MONOTONIC, now1, now2, add_result, sub_result);
137 #else
138 abs_deadline = gpr_convert_clock_type_debug(
139 abs_deadline, GPR_CLOCK_REALTIME, now1, now2, add_result, sub_result);
140 #endif // GPR_LINUX
141 abs_deadline_ts.tv_sec = static_cast<time_t>(abs_deadline.tv_sec);
142 abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
143 #ifdef GRPC_ASAN_ENABLED
144 err = pthread_cond_timedwait(&cv->cond_var, &mu->mutex, &abs_deadline_ts);
145 #else
146 err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
147 #endif
148 }
149 GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
150 return err == ETIMEDOUT;
151 }
152
gpr_cv_signal(gpr_cv * cv)153 void gpr_cv_signal(gpr_cv* cv) {
154 #ifdef GRPC_ASAN_ENABLED
155 GPR_ASSERT(pthread_cond_signal(&cv->cond_var) == 0);
156 #else
157 GPR_ASSERT(pthread_cond_signal(cv) == 0);
158 #endif
159 }
160
gpr_cv_broadcast(gpr_cv * cv)161 void gpr_cv_broadcast(gpr_cv* cv) {
162 #ifdef GRPC_ASAN_ENABLED
163 GPR_ASSERT(pthread_cond_broadcast(&cv->cond_var) == 0);
164 #else
165 GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
166 #endif
167 }
168
169 /*----------------------------------------*/
170
gpr_once_init(gpr_once * once,void (* init_function)(void))171 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
172 GPR_ASSERT(pthread_once(once, init_function) == 0);
173 }
174
175 #endif /* defined(GPR_POSIX_SYNC) && !defined(GPR_ABSEIL_SYNC) && \
176 !defined(GPR_CUSTOM_SYNC) */
177