• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2020 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_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC)
22 
23 #include <grpc/support/alloc.h>
24 
25 #include <errno.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 #include <grpc/support/time.h>
29 #include <time.h>
30 #include "src/core/lib/profiling/timers.h"
31 
32 #include "absl/base/call_once.h"
33 #include "absl/synchronization/mutex.h"
34 #include "absl/time/clock.h"
35 #include "absl/time/time.h"
36 
37 #ifdef GPR_LOW_LEVEL_COUNTERS
38 gpr_atm gpr_mu_locks = 0;
39 gpr_atm gpr_counter_atm_cas = 0;
40 gpr_atm gpr_counter_atm_add = 0;
41 #endif
42 
gpr_mu_init(gpr_mu * mu)43 void gpr_mu_init(gpr_mu* mu) {
44   static_assert(sizeof(gpr_mu) == sizeof(absl::Mutex),
45                 "gpr_mu and Mutex must be the same size");
46   new (mu) absl::Mutex;
47 }
48 
gpr_mu_destroy(gpr_mu * mu)49 void gpr_mu_destroy(gpr_mu* mu) {
50   reinterpret_cast<absl::Mutex*>(mu)->~Mutex();
51 }
52 
gpr_mu_lock(gpr_mu * mu)53 void gpr_mu_lock(gpr_mu* mu) ABSL_NO_THREAD_SAFETY_ANALYSIS {
54   GPR_TIMER_SCOPE("gpr_mu_lock", 0);
55   reinterpret_cast<absl::Mutex*>(mu)->Lock();
56 }
57 
gpr_mu_unlock(gpr_mu * mu)58 void gpr_mu_unlock(gpr_mu* mu) ABSL_NO_THREAD_SAFETY_ANALYSIS {
59   GPR_TIMER_SCOPE("gpr_mu_unlock", 0);
60   reinterpret_cast<absl::Mutex*>(mu)->Unlock();
61 }
62 
gpr_mu_trylock(gpr_mu * mu)63 int gpr_mu_trylock(gpr_mu* mu) {
64   GPR_TIMER_SCOPE("gpr_mu_trylock", 0);
65   return reinterpret_cast<absl::Mutex*>(mu)->TryLock();
66 }
67 
68 /*----------------------------------------*/
69 
gpr_cv_init(gpr_cv * cv)70 void gpr_cv_init(gpr_cv* cv) {
71   static_assert(sizeof(gpr_cv) == sizeof(absl::CondVar),
72                 "gpr_cv and CondVar must be the same size");
73   new (cv) absl::CondVar;
74 }
75 
gpr_cv_destroy(gpr_cv * cv)76 void gpr_cv_destroy(gpr_cv* cv) {
77   reinterpret_cast<absl::CondVar*>(cv)->~CondVar();
78 }
79 
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)80 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
81   GPR_TIMER_SCOPE("gpr_cv_wait", 0);
82   if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
83       0) {
84     reinterpret_cast<absl::CondVar*>(cv)->Wait(
85         reinterpret_cast<absl::Mutex*>(mu));
86     return 0;
87   }
88   abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
89   timespec ts = {static_cast<decltype(ts.tv_sec)>(abs_deadline.tv_sec),
90                  static_cast<decltype(ts.tv_nsec)>(abs_deadline.tv_nsec)};
91   return reinterpret_cast<absl::CondVar*>(cv)->WaitWithDeadline(
92       reinterpret_cast<absl::Mutex*>(mu), absl::TimeFromTimespec(ts));
93 }
94 
gpr_cv_signal(gpr_cv * cv)95 void gpr_cv_signal(gpr_cv* cv) {
96   GPR_TIMER_MARK("gpr_cv_signal", 0);
97   reinterpret_cast<absl::CondVar*>(cv)->Signal();
98 }
99 
gpr_cv_broadcast(gpr_cv * cv)100 void gpr_cv_broadcast(gpr_cv* cv) {
101   GPR_TIMER_MARK("gpr_cv_broadcast", 0);
102   reinterpret_cast<absl::CondVar*>(cv)->SignalAll();
103 }
104 
105 /*----------------------------------------*/
106 
gpr_once_init(gpr_once * once,void (* init_function)(void))107 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
108   static_assert(sizeof(gpr_once) == sizeof(absl::once_flag),
109                 "gpr_once and absl::once_flag must be the same size");
110   absl::call_once(*reinterpret_cast<absl::once_flag*>(once), init_function);
111 }
112 
113 #endif /* defined(GPR_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC) */
114