GRPC C++  1.30.0
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 
23 
24 #ifdef GPR_HAS_PTHREAD_H
25 #include <pthread.h>
26 #endif
27 
28 #include <mutex>
29 
30 #include <grpc/impl/codegen/log.h>
31 #include <grpc/impl/codegen/sync.h>
32 
34 
35 // The core library is not accessible in C++ codegen headers, and vice versa.
36 // Thus, we need to have duplicate headers with similar functionality.
37 // Make sure any change to this file is also reflected in
38 // src/core/lib/gprpp/sync.h too.
39 //
40 // Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file,
41 // since in core we do not rely on g_core_codegen_interface and hence do not
42 // pay the costs of virtual function calls.
43 
44 namespace grpc {
45 namespace internal {
46 
47 class Mutex {
48  public:
51 
52  Mutex(const Mutex&) = delete;
53  Mutex& operator=(const Mutex&) = delete;
54 
55  gpr_mu* get() { return &mu_; }
56  const gpr_mu* get() const { return &mu_; }
57 
58  private:
59  union {
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 };
67 
68 // MutexLock is a std::
69 class MutexLock {
70  public:
71  explicit MutexLock(Mutex* mu) : mu_(mu->get()) {
73  }
74  explicit MutexLock(gpr_mu* mu) : mu_(mu) {
76  }
78 
79  MutexLock(const MutexLock&) = delete;
80  MutexLock& operator=(const MutexLock&) = delete;
81 
82  private:
83  gpr_mu* const mu_;
84 };
85 
87  public:
88  explicit ReleasableMutexLock(Mutex* mu) : mu_(mu->get()) {
90  }
91  explicit ReleasableMutexLock(gpr_mu* mu) : mu_(mu) {
93  }
95  if (!released_) g_core_codegen_interface->gpr_mu_unlock(mu_);
96  }
97 
100 
101  void Lock() {
102  GPR_DEBUG_ASSERT(released_);
104  released_ = false;
105  }
106 
107  void Unlock() {
108  GPR_DEBUG_ASSERT(!released_);
109  released_ = true;
111  }
112 
113  private:
114  gpr_mu* const mu_;
115  bool released_ = false;
116 };
117 
118 class CondVar {
119  public:
122 
123  CondVar(const CondVar&) = delete;
124  CondVar& operator=(const CondVar&) = delete;
125 
128 
129  int Wait(Mutex* mu) {
130  return Wait(mu,
132  }
133  int Wait(Mutex* mu, const gpr_timespec& deadline) {
134  return g_core_codegen_interface->gpr_cv_wait(&cv_, mu->get(), deadline);
135  }
136 
137  template <typename Predicate>
138  void WaitUntil(Mutex* mu, Predicate pred) {
139  while (!pred()) {
141  }
142  }
143 
144  private:
145  gpr_cv cv_;
146 };
147 
148 } // namespace internal
149 } // namespace grpc
150 
151 #endif // GRPCPP_IMPL_CODEGEN_SYNC_H
grpc::CoreCodegenInterface::gpr_mu_unlock
virtual void gpr_mu_unlock(gpr_mu *mu)=0
log.h
grpc::internal::CondVar
Definition: sync.h:118
grpc::internal::CondVar::Signal
void Signal()
Definition: sync.h:126
grpc::internal::ReleasableMutexLock::ReleasableMutexLock
ReleasableMutexLock(Mutex *mu)
Definition: sync.h:88
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc::internal::CondVar::operator=
CondVar & operator=(const CondVar &)=delete
grpc::CoreCodegenInterface::gpr_mu_destroy
virtual void gpr_mu_destroy(gpr_mu *mu)=0
grpc::internal::CondVar::Wait
int Wait(Mutex *mu)
Definition: sync.h:129
gpr_cv
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
grpc::CoreCodegenInterface::gpr_cv_signal
virtual void gpr_cv_signal(gpr_cv *cv)=0
grpc::internal::MutexLock::MutexLock
MutexLock(gpr_mu *mu)
Definition: sync.h:74
grpc::internal::CondVar::Wait
int Wait(Mutex *mu, const gpr_timespec &deadline)
Definition: sync.h:133
core_codegen_interface.h
grpc::internal::CondVar::CondVar
CondVar()
Definition: sync.h:120
grpc::CoreCodegenInterface::gpr_inf_future
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
grpc::internal::ReleasableMutexLock::Unlock
void Unlock()
Definition: sync.h:107
grpc::internal::MutexLock::operator=
MutexLock & operator=(const MutexLock &)=delete
grpc::internal::Mutex::~Mutex
~Mutex()
Definition: sync.h:50
grpc::CoreCodegenInterface::gpr_mu_init
virtual void gpr_mu_init(gpr_mu *mu)=0
grpc::internal::Mutex::get
const gpr_mu * get() const
Definition: sync.h:56
grpc::CoreCodegenInterface::gpr_cv_init
virtual void gpr_cv_init(gpr_cv *cv)=0
grpc::internal::Mutex::operator=
Mutex & operator=(const Mutex &)=delete
sync.h
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
grpc::internal::Mutex::mu_
gpr_mu mu_
Definition: sync.h:60
grpc::CoreCodegenInterface::gpr_cv_wait
virtual int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)=0
grpc::internal::CondVar::Broadcast
void Broadcast()
Definition: sync.h:127
grpc::internal::MutexLock::~MutexLock
~MutexLock()
Definition: sync.h:77
grpc::internal::MutexLock
Definition: sync.h:69
port_platform.h
grpc::internal::ReleasableMutexLock::~ReleasableMutexLock
~ReleasableMutexLock()
Definition: sync.h:94
grpc::internal::CondVar::WaitUntil
void WaitUntil(Mutex *mu, Predicate pred)
Definition: sync.h:138
grpc::internal::CondVar::~CondVar
~CondVar()
Definition: sync.h:121
grpc::internal::ReleasableMutexLock
Definition: sync.h:86
grpc::CoreCodegenInterface::gpr_mu_lock
virtual void gpr_mu_lock(gpr_mu *mu)=0
grpc::CoreCodegenInterface::gpr_cv_destroy
virtual void gpr_cv_destroy(gpr_cv *cv)=0
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:93
grpc::internal::Mutex
Definition: sync.h:47
gpr_mu
Definition: sync_windows.h:26
gpr_timespec
Analogous to struct timespec.
Definition: gpr_types.h:47
grpc::internal::ReleasableMutexLock::ReleasableMutexLock
ReleasableMutexLock(gpr_mu *mu)
Definition: sync.h:91
grpc::internal::Mutex::do_not_use_sth_
std::mutex do_not_use_sth_
Definition: sync.h:61
grpc::internal::ReleasableMutexLock::operator=
ReleasableMutexLock & operator=(const ReleasableMutexLock &)=delete
grpc::internal::Mutex::Mutex
Mutex()
Definition: sync.h:49
GPR_CLOCK_REALTIME
Realtime clock.
Definition: gpr_types.h:36
grpc::CoreCodegenInterface::gpr_cv_broadcast
virtual void gpr_cv_broadcast(gpr_cv *cv)=0
grpc::internal::MutexLock::MutexLock
MutexLock(Mutex *mu)
Definition: sync.h:71
grpc::internal::Mutex::get
gpr_mu * get()
Definition: sync.h:55
grpc::internal::ReleasableMutexLock::Lock
void Lock()
Definition: sync.h:101