sync.h 3.6 KB

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