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/gprpp/crash.h"
33 
gpr_mu_init(gpr_mu * mu)34 void gpr_mu_init(gpr_mu* mu) {
35 #ifdef GRPC_ASAN_ENABLED
36   GPR_ASSERT(pthread_mutex_init(&mu->mutex, nullptr) == 0);
37   mu->leak_checker = static_cast<int*>(malloc(sizeof(*mu->leak_checker)));
38   GPR_ASSERT(mu->leak_checker != nullptr);
39 #else
40   GPR_ASSERT(pthread_mutex_init(mu, nullptr) == 0);
41 #endif
42 }
43 
gpr_mu_destroy(gpr_mu * mu)44 void gpr_mu_destroy(gpr_mu* mu) {
45 #ifdef GRPC_ASAN_ENABLED
46   GPR_ASSERT(pthread_mutex_destroy(&mu->mutex) == 0);
47   free(mu->leak_checker);
48 #else
49   GPR_ASSERT(pthread_mutex_destroy(mu) == 0);
50 #endif
51 }
52 
gpr_mu_lock(gpr_mu * mu)53 void gpr_mu_lock(gpr_mu* mu) {
54 #ifdef GRPC_ASAN_ENABLED
55   GPR_ASSERT(pthread_mutex_lock(&mu->mutex) == 0);
56 #else
57   GPR_ASSERT(pthread_mutex_lock(mu) == 0);
58 #endif
59 }
60 
gpr_mu_unlock(gpr_mu * mu)61 void gpr_mu_unlock(gpr_mu* mu) {
62 #ifdef GRPC_ASAN_ENABLED
63   GPR_ASSERT(pthread_mutex_unlock(&mu->mutex) == 0);
64 #else
65   GPR_ASSERT(pthread_mutex_unlock(mu) == 0);
66 #endif
67 }
68 
gpr_mu_trylock(gpr_mu * mu)69 int gpr_mu_trylock(gpr_mu* mu) {
70   int err = 0;
71 #ifdef GRPC_ASAN_ENABLED
72   err = pthread_mutex_trylock(&mu->mutex);
73 #else
74   err = pthread_mutex_trylock(mu);
75 #endif
76   GPR_ASSERT(err == 0 || err == EBUSY);
77   return err == 0;
78 }
79 
80 //----------------------------------------
81 
gpr_cv_init(gpr_cv * cv)82 void gpr_cv_init(gpr_cv* cv) {
83   pthread_condattr_t attr;
84   GPR_ASSERT(pthread_condattr_init(&attr) == 0);
85 #if GPR_LINUX
86   GPR_ASSERT(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0);
87 #endif  // GPR_LINUX
88 
89 #ifdef GRPC_ASAN_ENABLED
90   GPR_ASSERT(pthread_cond_init(&cv->cond_var, &attr) == 0);
91   cv->leak_checker = static_cast<int*>(malloc(sizeof(*cv->leak_checker)));
92   GPR_ASSERT(cv->leak_checker != nullptr);
93 #else
94   GPR_ASSERT(pthread_cond_init(cv, &attr) == 0);
95 #endif
96 }
97 
gpr_cv_destroy(gpr_cv * cv)98 void gpr_cv_destroy(gpr_cv* cv) {
99 #ifdef GRPC_ASAN_ENABLED
100   GPR_ASSERT(pthread_cond_destroy(&cv->cond_var) == 0);
101   free(cv->leak_checker);
102 #else
103   GPR_ASSERT(pthread_cond_destroy(cv) == 0);
104 #endif
105 }
106 
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)107 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
108   int err = 0;
109   if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
110       0) {
111 #ifdef GRPC_ASAN_ENABLED
112     err = pthread_cond_wait(&cv->cond_var, &mu->mutex);
113 #else
114     err = pthread_cond_wait(cv, mu);
115 #endif
116   } else {
117     struct timespec abs_deadline_ts;
118 #if GPR_LINUX
119     abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_MONOTONIC);
120 #else
121     abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
122     abs_deadline = gpr_time_max(abs_deadline, gpr_now(abs_deadline.clock_type));
123 #endif  // GPR_LINUX
124     abs_deadline_ts.tv_sec = static_cast<time_t>(abs_deadline.tv_sec);
125     abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
126 #ifdef GRPC_ASAN_ENABLED
127     err = pthread_cond_timedwait(&cv->cond_var, &mu->mutex, &abs_deadline_ts);
128 #else
129     err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
130 #endif
131   }
132   GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
133   return err == ETIMEDOUT;
134 }
135 
gpr_cv_signal(gpr_cv * cv)136 void gpr_cv_signal(gpr_cv* cv) {
137 #ifdef GRPC_ASAN_ENABLED
138   GPR_ASSERT(pthread_cond_signal(&cv->cond_var) == 0);
139 #else
140   GPR_ASSERT(pthread_cond_signal(cv) == 0);
141 #endif
142 }
143 
gpr_cv_broadcast(gpr_cv * cv)144 void gpr_cv_broadcast(gpr_cv* cv) {
145 #ifdef GRPC_ASAN_ENABLED
146   GPR_ASSERT(pthread_cond_broadcast(&cv->cond_var) == 0);
147 #else
148   GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
149 #endif
150 }
151 
152 //----------------------------------------
153 
gpr_once_init(gpr_once * once,void (* init_function)(void))154 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
155   GPR_ASSERT(pthread_once(once, init_function) == 0);
156 }
157 
158 #endif  // defined(GPR_POSIX_SYNC) && !defined(GPR_ABSEIL_SYNC) && \
159        // !defined(GPR_CUSTOM_SYNC)
160