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 <errno.h>
24 #include <time.h>
25
26 #include "absl/base/call_once.h"
27 #include "absl/synchronization/mutex.h"
28 #include "absl/time/clock.h"
29 #include "absl/time/time.h"
30
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34 #include <grpc/support/time.h>
35
36 #include "src/core/lib/gprpp/crash.h"
37 #include "src/core/lib/gprpp/time_util.h"
38
gpr_mu_init(gpr_mu * mu)39 void gpr_mu_init(gpr_mu* mu) {
40 static_assert(sizeof(gpr_mu) == sizeof(absl::Mutex),
41 "gpr_mu and Mutex must be the same size");
42 new (mu) absl::Mutex;
43 }
44
gpr_mu_destroy(gpr_mu * mu)45 void gpr_mu_destroy(gpr_mu* mu) {
46 reinterpret_cast<absl::Mutex*>(mu)->~Mutex();
47 }
48
gpr_mu_lock(gpr_mu * mu)49 void gpr_mu_lock(gpr_mu* mu) ABSL_NO_THREAD_SAFETY_ANALYSIS {
50 reinterpret_cast<absl::Mutex*>(mu)->Lock();
51 }
52
gpr_mu_unlock(gpr_mu * mu)53 void gpr_mu_unlock(gpr_mu* mu) ABSL_NO_THREAD_SAFETY_ANALYSIS {
54 reinterpret_cast<absl::Mutex*>(mu)->Unlock();
55 }
56
gpr_mu_trylock(gpr_mu * mu)57 int gpr_mu_trylock(gpr_mu* mu) {
58 return reinterpret_cast<absl::Mutex*>(mu)->TryLock();
59 }
60
61 //----------------------------------------
62
gpr_cv_init(gpr_cv * cv)63 void gpr_cv_init(gpr_cv* cv) {
64 static_assert(sizeof(gpr_cv) == sizeof(absl::CondVar),
65 "gpr_cv and CondVar must be the same size");
66 new (cv) absl::CondVar;
67 }
68
gpr_cv_destroy(gpr_cv * cv)69 void gpr_cv_destroy(gpr_cv* cv) {
70 reinterpret_cast<absl::CondVar*>(cv)->~CondVar();
71 }
72
gpr_cv_wait(gpr_cv * cv,gpr_mu * mu,gpr_timespec abs_deadline)73 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
74 absl::CondVar* const c = reinterpret_cast<absl::CondVar*>(cv);
75 absl::Mutex* const m = reinterpret_cast<absl::Mutex*>(mu);
76 if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
77 0) {
78 c->Wait(m);
79 return 0;
80 }
81 // Use WaitWithTimeout if possible instead of WaitWithDeadline hoping that
82 // it's going to use a monotonic clock.
83 if (abs_deadline.clock_type == GPR_TIMESPAN) {
84 return c->WaitWithTimeout(m, grpc_core::ToAbslDuration(abs_deadline));
85 } else if (abs_deadline.clock_type == GPR_CLOCK_MONOTONIC) {
86 absl::Duration duration = grpc_core::ToAbslDuration(
87 gpr_time_sub(abs_deadline, gpr_now(GPR_CLOCK_MONOTONIC)));
88 return c->WaitWithTimeout(m, duration);
89 } else {
90 return c->WaitWithDeadline(m, grpc_core::ToAbslTime(abs_deadline));
91 }
92 }
93
gpr_cv_signal(gpr_cv * cv)94 void gpr_cv_signal(gpr_cv* cv) {
95 reinterpret_cast<absl::CondVar*>(cv)->Signal();
96 }
97
gpr_cv_broadcast(gpr_cv * cv)98 void gpr_cv_broadcast(gpr_cv* cv) {
99 reinterpret_cast<absl::CondVar*>(cv)->SignalAll();
100 }
101
102 //----------------------------------------
103
gpr_once_init(gpr_once * once,void (* init_function)(void))104 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
105 static_assert(sizeof(gpr_once) == sizeof(absl::once_flag),
106 "gpr_once and absl::once_flag must be the same size");
107 absl::call_once(*reinterpret_cast<absl::once_flag*>(once), init_function);
108 }
109
110 #endif // defined(GPR_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC)
111