Debian Codegraph @

src/core/lib/gpr/sync_posix.cc

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