• 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   int ret = reinterpret_cast<absl::Mutex*>(mu)->TryLock() == true;
66   return ret;
67 }
68 
69 /*----------------------------------------*/
70 
gpr_cv_init(gpr_cv * cv)71 void gpr_cv_init(gpr_cv* cv) {
72   static_assert(sizeof(gpr_cv) == sizeof(absl::CondVar),
73                 "gpr_cv and CondVar must be the same size");
74   new (cv) absl::CondVar;
75 }
76 
gpr_cv_destroy(gpr_cv * cv)77 void gpr_cv_destroy(gpr_cv* cv) {
78   reinterpret_cast<absl::CondVar*>(cv)->~CondVar();
79 }
80 
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)81 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
82   GPR_TIMER_SCOPE("gpr_cv_wait", 0);
83   if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
84       0) {
85     reinterpret_cast<absl::CondVar*>(cv)->Wait(
86         reinterpret_cast<absl::Mutex*>(mu));
87     return 0;
88   }
89   abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
90   timespec ts = {static_cast<decltype(ts.tv_sec)>(abs_deadline.tv_sec),
91                  static_cast<decltype(ts.tv_nsec)>(abs_deadline.tv_nsec)};
92   int ret = reinterpret_cast<absl::CondVar*>(cv)->WaitWithDeadline(
93                 reinterpret_cast<absl::Mutex*>(mu),
94                 absl::TimeFromTimespec(ts)) == true;
95   return ret;
96 }
97 
gpr_cv_signal(gpr_cv * cv)98 void gpr_cv_signal(gpr_cv* cv) {
99   GPR_TIMER_MARK("gpr_cv_signal", 0);
100   reinterpret_cast<absl::CondVar*>(cv)->Signal();
101 }
102 
gpr_cv_broadcast(gpr_cv * cv)103 void gpr_cv_broadcast(gpr_cv* cv) {
104   GPR_TIMER_MARK("gpr_cv_broadcast", 0);
105   reinterpret_cast<absl::CondVar*>(cv)->SignalAll();
106 }
107 
108 /*----------------------------------------*/
109 
gpr_once_init(gpr_once * once,void (* init_function)(void))110 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
111   static_assert(sizeof(gpr_once) == sizeof(absl::once_flag),
112                 "gpr_once and absl::once_flag must be the same size");
113   absl::call_once(*reinterpret_cast<absl::once_flag*>(once), init_function);
114 }
115 
116 #endif /* defined(GPR_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC) */
117