• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef GPR_POSIX_SYNC
22 
23 #include <errno.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 #include <grpc/support/time.h>
27 #include <time.h>
28 #include "src/core/lib/profiling/timers.h"
29 
30 #ifdef GPR_LOW_LEVEL_COUNTERS
31 gpr_atm gpr_mu_locks = 0;
32 gpr_atm gpr_counter_atm_cas = 0;
33 gpr_atm gpr_counter_atm_add = 0;
34 #endif
35 
gpr_mu_init(gpr_mu * mu)36 void gpr_mu_init(gpr_mu* mu) {
37   GPR_ASSERT(pthread_mutex_init(mu, nullptr) == 0);
38 }
39 
gpr_mu_destroy(gpr_mu * mu)40 void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); }
41 
gpr_mu_lock(gpr_mu * mu)42 void gpr_mu_lock(gpr_mu* mu) {
43 #ifdef GPR_LOW_LEVEL_COUNTERS
44   GPR_ATM_INC_COUNTER(gpr_mu_locks);
45 #endif
46   GPR_TIMER_SCOPE("gpr_mu_lock", 0);
47   GPR_ASSERT(pthread_mutex_lock(mu) == 0);
48 }
49 
gpr_mu_unlock(gpr_mu * mu)50 void gpr_mu_unlock(gpr_mu* mu) {
51   GPR_TIMER_SCOPE("gpr_mu_unlock", 0);
52   GPR_ASSERT(pthread_mutex_unlock(mu) == 0);
53 }
54 
gpr_mu_trylock(gpr_mu * mu)55 int gpr_mu_trylock(gpr_mu* mu) {
56   GPR_TIMER_SCOPE("gpr_mu_trylock", 0);
57   int err = pthread_mutex_trylock(mu);
58   GPR_ASSERT(err == 0 || err == EBUSY);
59   return err == 0;
60 }
61 
62 /*----------------------------------------*/
63 
gpr_cv_init(gpr_cv * cv)64 void gpr_cv_init(gpr_cv* cv) {
65   pthread_condattr_t attr;
66   GPR_ASSERT(pthread_condattr_init(&attr) == 0);
67 #if GPR_LINUX
68   GPR_ASSERT(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) == 0);
69 #endif  // GPR_LINUX
70   GPR_ASSERT(pthread_cond_init(cv, &attr) == 0);
71 }
72 
gpr_cv_destroy(gpr_cv * cv)73 void gpr_cv_destroy(gpr_cv* cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); }
74 
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)75 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
76   int err = 0;
77   if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
78       0) {
79     err = pthread_cond_wait(cv, mu);
80   } else {
81     struct timespec abs_deadline_ts;
82 #if GPR_LINUX
83     abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_MONOTONIC);
84 #else
85     abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
86 #endif  // GPR_LINUX
87     abs_deadline_ts.tv_sec = static_cast<time_t>(abs_deadline.tv_sec);
88     abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
89     err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
90   }
91   GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
92   return err == ETIMEDOUT;
93 }
94 
gpr_cv_signal(gpr_cv * cv)95 void gpr_cv_signal(gpr_cv* cv) { GPR_ASSERT(pthread_cond_signal(cv) == 0); }
96 
gpr_cv_broadcast(gpr_cv * cv)97 void gpr_cv_broadcast(gpr_cv* cv) {
98   GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
99 }
100 
101 /*----------------------------------------*/
102 
gpr_once_init(gpr_once * once,void (* init_function)(void))103 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
104   GPR_ASSERT(pthread_once(once, init_function) == 0);
105 }
106 
107 #endif /* GRP_POSIX_SYNC */
108