diff options
Diffstat (limited to 'grpc/include/grpcpp/impl/codegen/sync.h')
-rw-r--r-- | grpc/include/grpcpp/impl/codegen/sync.h | 91 |
1 files changed, 48 insertions, 43 deletions
diff --git a/grpc/include/grpcpp/impl/codegen/sync.h b/grpc/include/grpcpp/impl/codegen/sync.h index 146f182e..0c4effe4 100644 --- a/grpc/include/grpcpp/impl/codegen/sync.h +++ b/grpc/include/grpcpp/impl/codegen/sync.h @@ -32,6 +32,8 @@ #include <grpcpp/impl/codegen/core_codegen_interface.h> +#include "absl/synchronization/mutex.h" + // The core library is not accessible in C++ codegen headers, and vice versa. // Thus, we need to have duplicate headers with similar functionality. // Make sure any change to this file is also reflected in @@ -44,7 +46,16 @@ namespace grpc { namespace internal { -class Mutex { +#ifdef GRPCPP_ABSEIL_SYNC + +using Mutex = absl::Mutex; +using MutexLock = absl::MutexLock; +using ReleasableMutexLock = absl::ReleasableMutexLock; +using CondVar = absl::CondVar; + +#else + +class ABSL_LOCKABLE Mutex { public: Mutex() { g_core_codegen_interface->gpr_mu_init(&mu_); } ~Mutex() { g_core_codegen_interface->gpr_mu_destroy(&mu_); } @@ -52,8 +63,12 @@ class Mutex { Mutex(const Mutex&) = delete; Mutex& operator=(const Mutex&) = delete; - gpr_mu* get() { return &mu_; } - const gpr_mu* get() const { return &mu_; } + void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { + g_core_codegen_interface->gpr_mu_lock(&mu_); + } + void Unlock() ABSL_UNLOCK_FUNCTION() { + g_core_codegen_interface->gpr_mu_unlock(&mu_); + } private: union { @@ -63,55 +78,45 @@ class Mutex { pthread_mutex_t do_not_use_pth_; #endif }; + + friend class CondVar; }; -// MutexLock is a std:: -class MutexLock { +class ABSL_SCOPED_LOCKABLE MutexLock { public: - explicit MutexLock(Mutex* mu) : mu_(mu->get()) { - g_core_codegen_interface->gpr_mu_lock(mu_); + explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { + mu_->Lock(); } - explicit MutexLock(gpr_mu* mu) : mu_(mu) { - g_core_codegen_interface->gpr_mu_lock(mu_); - } - ~MutexLock() { g_core_codegen_interface->gpr_mu_unlock(mu_); } + ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); } MutexLock(const MutexLock&) = delete; MutexLock& operator=(const MutexLock&) = delete; private: - gpr_mu* const mu_; + Mutex* const mu_; }; -class ReleasableMutexLock { +class ABSL_SCOPED_LOCKABLE ReleasableMutexLock { public: - explicit ReleasableMutexLock(Mutex* mu) : mu_(mu->get()) { - g_core_codegen_interface->gpr_mu_lock(mu_); - } - explicit ReleasableMutexLock(gpr_mu* mu) : mu_(mu) { - g_core_codegen_interface->gpr_mu_lock(mu_); + explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) + : mu_(mu) { + mu_->Lock(); } - ~ReleasableMutexLock() { - if (!released_) g_core_codegen_interface->gpr_mu_unlock(mu_); + ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() { + if (!released_) mu_->Unlock(); } ReleasableMutexLock(const ReleasableMutexLock&) = delete; ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete; - void Lock() { - GPR_DEBUG_ASSERT(released_); - g_core_codegen_interface->gpr_mu_lock(mu_); - released_ = false; - } - - void Unlock() { + void Release() ABSL_UNLOCK_FUNCTION() { GPR_DEBUG_ASSERT(!released_); released_ = true; - g_core_codegen_interface->gpr_mu_unlock(mu_); + mu_->Unlock(); } private: - gpr_mu* const mu_; + Mutex* const mu_; bool released_ = false; }; @@ -124,27 +129,27 @@ class CondVar { CondVar& operator=(const CondVar&) = delete; void Signal() { g_core_codegen_interface->gpr_cv_signal(&cv_); } - void Broadcast() { g_core_codegen_interface->gpr_cv_broadcast(&cv_); } - - int Wait(Mutex* mu) { - return Wait(mu, - g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)); - } - int Wait(Mutex* mu, const gpr_timespec& deadline) { - return g_core_codegen_interface->gpr_cv_wait(&cv_, mu->get(), deadline); - } + void SignalAll() { g_core_codegen_interface->gpr_cv_broadcast(&cv_); } - template <typename Predicate> - void WaitUntil(Mutex* mu, Predicate pred) { - while (!pred()) { - Wait(mu, g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)); - } + void Wait(Mutex* mu) { + g_core_codegen_interface->gpr_cv_wait( + &cv_, &mu->mu_, + g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)); } private: gpr_cv cv_; }; +#endif // GRPCPP_ABSEIL_SYNC + +template <typename Predicate> +static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) { + while (!pred()) { + cv->Wait(mu); + } +} + } // namespace internal } // namespace grpc |