123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //
- //
- // Copyright 2019 gRPC authors.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- //
- #ifndef GRPCPP_IMPL_SYNC_H
- #define GRPCPP_IMPL_SYNC_H
- #include <grpc/support/port_platform.h>
- #ifdef GPR_HAS_PTHREAD_H
- #include <pthread.h>
- #endif
- #include <mutex>
- #include "y_absl/synchronization/mutex.h"
- #include <grpc/support/log.h>
- #include <grpc/support/sync.h>
- #include <grpc/support/time.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
- // src/core/lib/gprpp/sync.h too.
- //
- // Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file,
- // since in core we do not rely on g_core_codegen_interface and hence do not
- // pay the costs of virtual function calls.
- namespace grpc {
- namespace internal {
- #ifdef GPR_ABSEIL_SYNC
- using Mutex = y_absl::Mutex;
- using MutexLock = y_absl::MutexLock;
- using ReleasableMutexLock = y_absl::ReleasableMutexLock;
- using CondVar = y_absl::CondVar;
- #else
- class Y_ABSL_LOCKABLE Mutex {
- public:
- Mutex() { gpr_mu_init(&mu_); }
- ~Mutex() { gpr_mu_destroy(&mu_); }
- Mutex(const Mutex&) = delete;
- Mutex& operator=(const Mutex&) = delete;
- void Lock() Y_ABSL_EXCLUSIVE_LOCK_FUNCTION() { gpr_mu_lock(&mu_); }
- void Unlock() Y_ABSL_UNLOCK_FUNCTION() { gpr_mu_unlock(&mu_); }
- private:
- union {
- gpr_mu mu_;
- std::mutex do_not_use_sth_;
- #ifdef GPR_HAS_PTHREAD_H
- pthread_mutex_t do_not_use_pth_;
- #endif
- };
- friend class CondVar;
- };
- class Y_ABSL_SCOPED_LOCKABLE MutexLock {
- public:
- explicit MutexLock(Mutex* mu) Y_ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
- mu_->Lock();
- }
- ~MutexLock() Y_ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
- MutexLock(const MutexLock&) = delete;
- MutexLock& operator=(const MutexLock&) = delete;
- private:
- Mutex* const mu_;
- };
- class Y_ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
- public:
- explicit ReleasableMutexLock(Mutex* mu) Y_ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
- : mu_(mu) {
- mu_->Lock();
- }
- ~ReleasableMutexLock() Y_ABSL_UNLOCK_FUNCTION() {
- if (!released_) mu_->Unlock();
- }
- ReleasableMutexLock(const ReleasableMutexLock&) = delete;
- ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
- void Release() Y_ABSL_UNLOCK_FUNCTION() {
- GPR_DEBUG_ASSERT(!released_);
- released_ = true;
- mu_->Unlock();
- }
- private:
- Mutex* const mu_;
- bool released_ = false;
- };
- class CondVar {
- public:
- CondVar() { gpr_cv_init(&cv_); }
- ~CondVar() { gpr_cv_destroy(&cv_); }
- CondVar(const CondVar&) = delete;
- CondVar& operator=(const CondVar&) = delete;
- void Signal() { gpr_cv_signal(&cv_); }
- void SignalAll() { gpr_cv_broadcast(&cv_); }
- void Wait(Mutex* mu) {
- gpr_cv_wait(&cv_, &mu->mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
- }
- private:
- gpr_cv cv_;
- };
- #endif // GPR_ABSEIL_SYNC
- template <typename Predicate>
- GRPC_DEPRECATED("incompatible with thread safety analysis")
- static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) {
- while (!pred()) {
- cv->Wait(mu);
- }
- }
- } // namespace internal
- } // namespace grpc
- #endif // GRPCPP_IMPL_SYNC_H
|