3 * Copyright 2019 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_GPRPP_SYNC_H
20 #define GRPC_CORE_LIB_GPRPP_SYNC_H
22 #include <grpc/impl/codegen/port_platform.h>
24 #include <grpc/impl/codegen/log.h>
25 #include <grpc/impl/codegen/sync.h>
26 #include <grpc/support/sync.h>
27 #include <grpc/support/time.h>
29 #include "absl/synchronization/mutex.h"
30 #include "src/core/lib/gprpp/time_util.h"
32 // The core library is not accessible in C++ codegen headers, and vice versa.
33 // Thus, we need to have duplicate headers with similar functionality.
34 // Make sure any change to this file is also reflected in
35 // include/grpcpp/impl/codegen/sync.h.
37 // Whenever possible, prefer using this file over <grpcpp/impl/codegen/sync.h>
38 // since this file doesn't rely on g_core_codegen_interface and hence does not
39 // pay the costs of virtual function calls.
43 #ifdef GPR_ABSEIL_SYNC
45 using Mutex = absl::Mutex;
46 using MutexLock = absl::MutexLock;
47 using ReleasableMutexLock = absl::ReleasableMutexLock;
48 using CondVar = absl::CondVar;
50 // Returns the underlying gpr_mu from Mutex. This should be used only when
51 // it has to like passing the C++ mutex to C-core API.
52 // TODO(veblush): Remove this after C-core no longer uses gpr_mu.
53 inline gpr_mu* GetUnderlyingGprMu(Mutex* mutex) {
54 return reinterpret_cast<gpr_mu*>(mutex);
59 class ABSL_LOCKABLE Mutex {
61 Mutex() { gpr_mu_init(&mu_); }
62 ~Mutex() { gpr_mu_destroy(&mu_); }
64 Mutex(const Mutex&) = delete;
65 Mutex& operator=(const Mutex&) = delete;
67 void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { gpr_mu_lock(&mu_); }
68 void Unlock() ABSL_UNLOCK_FUNCTION() { gpr_mu_unlock(&mu_); }
69 bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
70 return gpr_mu_trylock(&mu_) != 0;
77 friend gpr_mu* GetUnderlyingGprMu(Mutex* mutex);
80 // Returns the underlying gpr_mu from Mutex. This should be used only when
81 // it has to like passing the C++ mutex to C-core API.
82 // TODO(veblush): Remove this after C-core no longer uses gpr_mu.
83 inline gpr_mu* GetUnderlyingGprMu(Mutex* mutex) { return &mutex->mu_; }
85 class ABSL_SCOPED_LOCKABLE MutexLock {
87 explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
90 ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
92 MutexLock(const MutexLock&) = delete;
93 MutexLock& operator=(const MutexLock&) = delete;
99 class ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
101 explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
105 ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
106 if (!released_) mu_->Unlock();
109 ReleasableMutexLock(const ReleasableMutexLock&) = delete;
110 ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
112 void Release() ABSL_UNLOCK_FUNCTION() {
113 GPR_DEBUG_ASSERT(!released_);
120 bool released_ = false;
125 CondVar() { gpr_cv_init(&cv_); }
126 ~CondVar() { gpr_cv_destroy(&cv_); }
128 CondVar(const CondVar&) = delete;
129 CondVar& operator=(const CondVar&) = delete;
131 void Signal() { gpr_cv_signal(&cv_); }
132 void SignalAll() { gpr_cv_broadcast(&cv_); }
134 void Wait(Mutex* mu) { WaitWithDeadline(mu, absl::InfiniteFuture()); }
135 bool WaitWithTimeout(Mutex* mu, absl::Duration timeout) {
136 return gpr_cv_wait(&cv_, &mu->mu_, ToGprTimeSpec(timeout)) != 0;
138 bool WaitWithDeadline(Mutex* mu, absl::Time deadline) {
139 return gpr_cv_wait(&cv_, &mu->mu_, ToGprTimeSpec(deadline)) != 0;
146 #endif // GPR_ABSEIL_SYNC
148 template <typename Predicate>
149 static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) {
155 // Returns true iff we timed-out
156 template <typename Predicate>
157 static bool WaitUntilWithTimeout(CondVar* cv, Mutex* mu, Predicate pred,
158 absl::Duration timeout) {
160 if (cv->WaitWithTimeout(mu, timeout)) return true;
165 // Returns true iff we timed-out
166 template <typename Predicate>
167 static bool WaitUntilWithDeadline(CondVar* cv, Mutex* mu, Predicate pred,
168 absl::Time deadline) {
170 if (cv->WaitWithDeadline(mu, deadline)) return true;
175 // Deprecated. Prefer MutexLock
176 class MutexLockForGprMu {
178 explicit MutexLockForGprMu(gpr_mu* mu) : mu_(mu) { gpr_mu_lock(mu_); }
179 ~MutexLockForGprMu() { gpr_mu_unlock(mu_); }
181 MutexLockForGprMu(const MutexLock&) = delete;
182 MutexLockForGprMu& operator=(const MutexLock&) = delete;
188 // Deprecated. Prefer MutexLock or ReleasableMutexLock
189 class ABSL_SCOPED_LOCKABLE LockableAndReleasableMutexLock {
191 explicit LockableAndReleasableMutexLock(Mutex* mu)
192 ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
196 ~LockableAndReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
197 if (!released_) mu_->Unlock();
200 LockableAndReleasableMutexLock(const LockableAndReleasableMutexLock&) =
202 LockableAndReleasableMutexLock& operator=(
203 const LockableAndReleasableMutexLock&) = delete;
205 void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {
206 GPR_DEBUG_ASSERT(released_);
211 void Release() ABSL_UNLOCK_FUNCTION() {
212 GPR_DEBUG_ASSERT(!released_);
219 bool released_ = false;
222 } // namespace grpc_core
224 #endif /* GRPC_CORE_LIB_GPRPP_SYNC_H */