sync.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. *
  3. * Copyright 2019 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. #ifndef GRPCPP_IMPL_CODEGEN_SYNC_H
  19. #define GRPCPP_IMPL_CODEGEN_SYNC_H
  20. // IWYU pragma: private
  21. #include <grpc/impl/codegen/port_platform.h>
  22. #ifdef GPR_HAS_PTHREAD_H
  23. #include <pthread.h>
  24. #endif
  25. #include <mutex>
  26. #include "absl/synchronization/mutex.h"
  27. #include <grpc/impl/codegen/log.h>
  28. #include <grpc/impl/codegen/sync.h>
  29. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  30. // The core library is not accessible in C++ codegen headers, and vice versa.
  31. // Thus, we need to have duplicate headers with similar functionality.
  32. // Make sure any change to this file is also reflected in
  33. // src/core/lib/gprpp/sync.h too.
  34. //
  35. // Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file,
  36. // since in core we do not rely on g_core_codegen_interface and hence do not
  37. // pay the costs of virtual function calls.
  38. namespace grpc {
  39. namespace internal {
  40. #ifdef GPR_ABSEIL_SYNC
  41. using Mutex = absl::Mutex;
  42. using MutexLock = absl::MutexLock;
  43. using ReleasableMutexLock = absl::ReleasableMutexLock;
  44. using CondVar = absl::CondVar;
  45. #else
  46. class ABSL_LOCKABLE Mutex {
  47. public:
  48. Mutex() { g_core_codegen_interface->gpr_mu_init(&mu_); }
  49. ~Mutex() { g_core_codegen_interface->gpr_mu_destroy(&mu_); }
  50. Mutex(const Mutex&) = delete;
  51. Mutex& operator=(const Mutex&) = delete;
  52. void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {
  53. g_core_codegen_interface->gpr_mu_lock(&mu_);
  54. }
  55. void Unlock() ABSL_UNLOCK_FUNCTION() {
  56. g_core_codegen_interface->gpr_mu_unlock(&mu_);
  57. }
  58. private:
  59. union {
  60. gpr_mu mu_;
  61. std::mutex do_not_use_sth_;
  62. #ifdef GPR_HAS_PTHREAD_H
  63. pthread_mutex_t do_not_use_pth_;
  64. #endif
  65. };
  66. friend class CondVar;
  67. };
  68. class ABSL_SCOPED_LOCKABLE MutexLock {
  69. public:
  70. explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
  71. mu_->Lock();
  72. }
  73. ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
  74. MutexLock(const MutexLock&) = delete;
  75. MutexLock& operator=(const MutexLock&) = delete;
  76. private:
  77. Mutex* const mu_;
  78. };
  79. class ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
  80. public:
  81. explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
  82. : mu_(mu) {
  83. mu_->Lock();
  84. }
  85. ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
  86. if (!released_) mu_->Unlock();
  87. }
  88. ReleasableMutexLock(const ReleasableMutexLock&) = delete;
  89. ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
  90. void Release() ABSL_UNLOCK_FUNCTION() {
  91. GPR_DEBUG_ASSERT(!released_);
  92. released_ = true;
  93. mu_->Unlock();
  94. }
  95. private:
  96. Mutex* const mu_;
  97. bool released_ = false;
  98. };
  99. class CondVar {
  100. public:
  101. CondVar() { g_core_codegen_interface->gpr_cv_init(&cv_); }
  102. ~CondVar() { g_core_codegen_interface->gpr_cv_destroy(&cv_); }
  103. CondVar(const CondVar&) = delete;
  104. CondVar& operator=(const CondVar&) = delete;
  105. void Signal() { g_core_codegen_interface->gpr_cv_signal(&cv_); }
  106. void SignalAll() { g_core_codegen_interface->gpr_cv_broadcast(&cv_); }
  107. void Wait(Mutex* mu) {
  108. g_core_codegen_interface->gpr_cv_wait(
  109. &cv_, &mu->mu_,
  110. g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME));
  111. }
  112. private:
  113. gpr_cv cv_;
  114. };
  115. #endif // GPR_ABSEIL_SYNC
  116. template <typename Predicate>
  117. GRPC_DEPRECATED("incompatible with thread safety analysis")
  118. static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) {
  119. while (!pred()) {
  120. cv->Wait(mu);
  121. }
  122. }
  123. } // namespace internal
  124. } // namespace grpc
  125. #endif // GRPCPP_IMPL_CODEGEN_SYNC_H