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