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 <errno.h>
25 #include <time.h>
26 
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/sync.h>
30 #include <grpc/support/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 
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)119 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
120   int err = 0;
121   if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
122       0) {
123 #ifdef GRPC_ASAN_ENABLED
124     err = pthread_cond_wait(&cv->cond_var, &mu->mutex);
125 #else
126     err = pthread_cond_wait(cv, mu);
127 #endif
128   } else {
129     struct timespec abs_deadline_ts;
130 #if GPR_LINUX
131     abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_MONOTONIC);
132 #else
133     abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
134 #endif  // GPR_LINUX
135     abs_deadline_ts.tv_sec = static_cast<time_t>(abs_deadline.tv_sec);
136     abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
137 #ifdef GRPC_ASAN_ENABLED
138     err = pthread_cond_timedwait(&cv->cond_var, &mu->mutex, &abs_deadline_ts);
139 #else
140     err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
141 #endif
142   }
143   GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
144   return err == ETIMEDOUT;
145 }
146 
gpr_cv_signal(gpr_cv * cv)147 void gpr_cv_signal(gpr_cv* cv) {
148 #ifdef GRPC_ASAN_ENABLED
149   GPR_ASSERT(pthread_cond_signal(&cv->cond_var) == 0);
150 #else
151   GPR_ASSERT(pthread_cond_signal(cv) == 0);
152 #endif
153 }
154 
gpr_cv_broadcast(gpr_cv * cv)155 void gpr_cv_broadcast(gpr_cv* cv) {
156 #ifdef GRPC_ASAN_ENABLED
157   GPR_ASSERT(pthread_cond_broadcast(&cv->cond_var) == 0);
158 #else
159   GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
160 #endif
161 }
162 
163 /*----------------------------------------*/
164 
gpr_once_init(gpr_once * once,void (* init_function)(void))165 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
166   GPR_ASSERT(pthread_once(once, init_function) == 0);
167 }
168 
169 #endif /* defined(GPR_POSIX_SYNC) && !defined(GPR_ABSEIL_SYNC) && \
170           !defined(GPR_CUSTOM_SYNC) */
171