include/grpcpp/impl/codegen/sync.h
Go to the documentation of this file.
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 
19 #ifndef GRPCPP_IMPL_CODEGEN_SYNC_H
20 #define GRPCPP_IMPL_CODEGEN_SYNC_H
21 
22 // IWYU pragma: private
23 
25 
26 #ifdef GPR_HAS_PTHREAD_H
27 #include <pthread.h>
28 #endif
29 
30 #include <mutex>
31 
32 #include "absl/synchronization/mutex.h"
33 
34 #include <grpc/impl/codegen/log.h>
35 #include <grpc/impl/codegen/sync.h>
37 
38 // The core library is not accessible in C++ codegen headers, and vice versa.
39 // Thus, we need to have duplicate headers with similar functionality.
40 // Make sure any change to this file is also reflected in
41 // src/core/lib/gprpp/sync.h too.
42 //
43 // Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file,
44 // since in core we do not rely on g_core_codegen_interface and hence do not
45 // pay the costs of virtual function calls.
46 
47 namespace grpc {
48 namespace internal {
49 
50 #ifdef GPR_ABSEIL_SYNC
51 
52 using Mutex = absl::Mutex;
54 using ReleasableMutexLock = absl::ReleasableMutexLock;
55 using CondVar = absl::CondVar;
56 
57 #else
58 
60  public:
63 
64  Mutex(const Mutex&) = delete;
65  Mutex& operator=(const Mutex&) = delete;
66 
69  }
72  }
73 
74  private:
75  union {
78 #ifdef GPR_HAS_PTHREAD_H
79  pthread_mutex_t do_not_use_pth_;
80 #endif
81  };
82 
83  friend class CondVar;
84 };
85 
87  public:
89  mu_->Lock();
90  }
91  ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
92 
93  MutexLock(const MutexLock&) = delete;
94  MutexLock& operator=(const MutexLock&) = delete;
95 
96  private:
97  Mutex* const mu_;
98 };
99 
101  public:
103  : mu_(mu) {
104  mu_->Lock();
105  }
107  if (!released_) mu_->Unlock();
108  }
109 
110  ReleasableMutexLock(const ReleasableMutexLock&) = delete;
111  ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
112 
114  GPR_DEBUG_ASSERT(!released_);
115  released_ = true;
116  mu_->Unlock();
117  }
118 
119  private:
120  Mutex* const mu_;
121  bool released_ = false;
122 };
123 
124 class CondVar {
125  public:
128 
129  CondVar(const CondVar&) = delete;
130  CondVar& operator=(const CondVar&) = delete;
131 
134 
135  void Wait(Mutex* mu) {
137  &cv_, &mu->mu_,
139  }
140 
141  private:
143 };
144 
145 #endif // GPR_ABSEIL_SYNC
146 
147 template <typename Predicate>
148 GRPC_DEPRECATED("incompatible with thread safety analysis")
149 static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) {
150  while (!pred()) {
151  cv->Wait(mu);
152  }
153 }
154 
155 } // namespace internal
156 } // namespace grpc
157 
158 #endif // GRPCPP_IMPL_CODEGEN_SYNC_H
grpc::internal::CondVar::~CondVar
~CondVar()
Definition: include/grpcpp/impl/codegen/sync.h:127
grpc::internal::Mutex::Unlock
void Unlock() ABSL_UNLOCK_FUNCTION()
Definition: include/grpcpp/impl/codegen/sync.h:70
grpc::CoreCodegenInterface::gpr_mu_unlock
virtual void gpr_mu_unlock(gpr_mu *mu)=0
grpc::internal::CondVar::CondVar
CondVar()
Definition: include/grpcpp/impl/codegen/sync.h:126
MutexLock
#define MutexLock(x)
Definition: bloaty/third_party/re2/util/mutex.h:125
grpc::internal::Mutex
Definition: include/grpcpp/impl/codegen/sync.h:59
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
grpc
Definition: grpcpp/alarm.h:33
grpc::CoreCodegenInterface::gpr_cv_destroy
virtual void gpr_cv_destroy(gpr_cv *cv)=0
grpc::internal::Mutex::Mutex
Mutex()
Definition: include/grpcpp/impl/codegen/sync.h:61
grpc::internal::Mutex::mu_
gpr_mu mu_
Definition: include/grpcpp/impl/codegen/sync.h:76
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
grpc::CoreCodegenInterface::gpr_cv_init
virtual void gpr_cv_init(gpr_cv *cv)=0
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
grpc::CoreCodegenInterface::gpr_inf_future
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
grpc::internal::WaitUntil
static void WaitUntil(CondVar *cv, Mutex *mu, Predicate pred)
Definition: include/grpcpp/impl/codegen/sync.h:149
core_codegen_interface.h
log.h
grpc::internal::MutexLock
Definition: include/grpcpp/impl/codegen/sync.h:86
mu_
Mutex mu_
Definition: oob_backend_metric.cc:115
grpc::internal::Mutex::do_not_use_sth_
std::mutex do_not_use_sth_
Definition: include/grpcpp/impl/codegen/sync.h:77
grpc::internal::ReleasableMutexLock::ReleasableMutexLock
ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: include/grpcpp/impl/codegen/sync.h:102
GRPC_DEPRECATED
#define GRPC_DEPRECATED(reason)
Definition: impl/codegen/port_platform.h:36
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::internal::CondVar::Wait
void Wait(Mutex *mu)
Definition: include/grpcpp/impl/codegen/sync.h:135
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
grpc::internal::CondVar::SignalAll
void SignalAll()
Definition: include/grpcpp/impl/codegen/sync.h:133
grpc::internal::MutexLock::~MutexLock
~MutexLock() ABSL_UNLOCK_FUNCTION()
Definition: include/grpcpp/impl/codegen/sync.h:91
grpc::CoreCodegenInterface::gpr_mu_lock
virtual void gpr_mu_lock(gpr_mu *mu)=0
grpc::internal::ReleasableMutexLock::Release
void Release() ABSL_UNLOCK_FUNCTION()
Definition: include/grpcpp/impl/codegen/sync.h:113
grpc::CoreCodegenInterface::gpr_cv_signal
virtual void gpr_cv_signal(gpr_cv *cv)=0
grpc::CoreCodegenInterface::gpr_cv_wait
virtual int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)=0
grpc::internal::Mutex::Lock
void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: include/grpcpp/impl/codegen/sync.h:67
grpc::internal::MutexLock::MutexLock
MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: include/grpcpp/impl/codegen/sync.h:88
grpc::CoreCodegenInterface::gpr_mu_destroy
virtual void gpr_mu_destroy(gpr_mu *mu)=0
grpc::internal::Mutex::~Mutex
~Mutex()
Definition: include/grpcpp/impl/codegen/sync.h:62
ABSL_EXCLUSIVE_LOCK_FUNCTION
#define ABSL_EXCLUSIVE_LOCK_FUNCTION(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:207
cv
unsigned cv
Definition: cxa_demangle.cpp:4908
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
absl::ReleasableMutexLock
Definition: abseil-cpp/absl/synchronization/mutex.h:914
absl::CondVar
Definition: abseil-cpp/absl/synchronization/mutex.h:798
port_platform.h
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
grpc::internal::ReleasableMutexLock
Definition: include/grpcpp/impl/codegen/sync.h:100
grpc::CoreCodegenInterface::gpr_mu_init
virtual void gpr_mu_init(gpr_mu *mu)=0
grpc::internal::CondVar
Definition: include/grpcpp/impl/codegen/sync.h:124
grpc::internal::ReleasableMutexLock::~ReleasableMutexLock
~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION()
Definition: include/grpcpp/impl/codegen/sync.h:106
grpc::internal::ReleasableMutexLock::mu_
Mutex *const mu_
Definition: include/grpcpp/impl/codegen/sync.h:120
grpc::internal::MutexLock::mu_
Mutex *const mu_
Definition: include/grpcpp/impl/codegen/sync.h:97
internal
Definition: benchmark/test/output_test_helper.cc:20
grpc::internal::CondVar::operator=
CondVar & operator=(const CondVar &)=delete
grpc::internal::CondVar::Signal
void Signal()
Definition: include/grpcpp/impl/codegen/sync.h:132
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
ABSL_UNLOCK_FUNCTION
#define ABSL_UNLOCK_FUNCTION(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:228
ABSL_LOCKABLE
#define ABSL_LOCKABLE
Definition: abseil-cpp/absl/base/thread_annotations.h:183
sync.h
grpc::internal::CondVar::cv_
gpr_cv cv_
Definition: include/grpcpp/impl/codegen/sync.h:142
ABSL_SCOPED_LOCKABLE
#define ABSL_SCOPED_LOCKABLE
Definition: abseil-cpp/absl/base/thread_annotations.h:196
grpc::CoreCodegenInterface::gpr_cv_broadcast
virtual void gpr_cv_broadcast(gpr_cv *cv)=0


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:28