You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sync.h 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. {
  40. namespace internal
  41. {
  42. #ifdef GPR_ABSEIL_SYNC
  43. using Mutex = absl::Mutex;
  44. using MutexLock = absl::MutexLock;
  45. using ReleasableMutexLock = absl::ReleasableMutexLock;
  46. using CondVar = absl::CondVar;
  47. #else
  48. class ABSL_LOCKABLE Mutex
  49. {
  50. public:
  51. Mutex()
  52. {
  53. g_core_codegen_interface->gpr_mu_init(&mu_);
  54. }
  55. ~Mutex()
  56. {
  57. g_core_codegen_interface->gpr_mu_destroy(&mu_);
  58. }
  59. Mutex(const Mutex&) = delete;
  60. Mutex& operator=(const Mutex&) = delete;
  61. void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION()
  62. {
  63. g_core_codegen_interface->gpr_mu_lock(&mu_);
  64. }
  65. void Unlock() ABSL_UNLOCK_FUNCTION()
  66. {
  67. g_core_codegen_interface->gpr_mu_unlock(&mu_);
  68. }
  69. private:
  70. union
  71. {
  72. gpr_mu mu_;
  73. std::mutex do_not_use_sth_;
  74. #ifdef GPR_HAS_PTHREAD_H
  75. pthread_mutex_t do_not_use_pth_;
  76. #endif
  77. };
  78. friend class CondVar;
  79. };
  80. class ABSL_SCOPED_LOCKABLE MutexLock
  81. {
  82. public:
  83. explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) :
  84. mu_(mu)
  85. {
  86. mu_->Lock();
  87. }
  88. ~MutexLock() ABSL_UNLOCK_FUNCTION()
  89. {
  90. mu_->Unlock();
  91. }
  92. MutexLock(const MutexLock&) = delete;
  93. MutexLock& operator=(const MutexLock&) = delete;
  94. private:
  95. Mutex* const mu_;
  96. };
  97. class ABSL_SCOPED_LOCKABLE ReleasableMutexLock
  98. {
  99. public:
  100. explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) :
  101. mu_(mu)
  102. {
  103. mu_->Lock();
  104. }
  105. ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION()
  106. {
  107. if (!released_)
  108. mu_->Unlock();
  109. }
  110. ReleasableMutexLock(const ReleasableMutexLock&) = delete;
  111. ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
  112. void Release() ABSL_UNLOCK_FUNCTION()
  113. {
  114. GPR_DEBUG_ASSERT(!released_);
  115. released_ = true;
  116. mu_->Unlock();
  117. }
  118. private:
  119. Mutex* const mu_;
  120. bool released_ = false;
  121. };
  122. class CondVar
  123. {
  124. public:
  125. CondVar()
  126. {
  127. g_core_codegen_interface->gpr_cv_init(&cv_);
  128. }
  129. ~CondVar()
  130. {
  131. g_core_codegen_interface->gpr_cv_destroy(&cv_);
  132. }
  133. CondVar(const CondVar&) = delete;
  134. CondVar& operator=(const CondVar&) = delete;
  135. void Signal()
  136. {
  137. g_core_codegen_interface->gpr_cv_signal(&cv_);
  138. }
  139. void SignalAll()
  140. {
  141. g_core_codegen_interface->gpr_cv_broadcast(&cv_);
  142. }
  143. void Wait(Mutex* mu)
  144. {
  145. g_core_codegen_interface->gpr_cv_wait(
  146. &cv_, &mu->mu_, g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)
  147. );
  148. }
  149. private:
  150. gpr_cv cv_;
  151. };
  152. #endif // GPR_ABSEIL_SYNC
  153. template<typename Predicate>
  154. GRPC_DEPRECATED("incompatible with thread safety analysis")
  155. static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred)
  156. {
  157. while (!pred())
  158. {
  159. cv->Wait(mu);
  160. }
  161. }
  162. } // namespace internal
  163. } // namespace grpc
  164. #endif // GRPCPP_IMPL_CODEGEN_SYNC_H