GRPC C++  1.26.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
gpr_mu mu_
Definition: sync.h:60
std::mutex do_not_use_sth_
Definition: sync.h:61
CoreCodegenInterface * g_core_codegen_interface
Null-initializes the global gRPC variables for the codegen library.
Definition: completion_queue_impl.h:90
virtual void gpr_mu_unlock(gpr_mu *mu)=0
ReleasableMutexLock(Mutex *mu)
Definition: sync.h:88
gpr_mu * get()
Definition: sync.h:55
virtual gpr_timespec gpr_inf_future(gpr_clock_type type)=0
virtual void gpr_cv_init(gpr_cv *cv)=0
void WaitUntil(Mutex *mu, Predicate pred)
Definition: sync.h:138
void Unlock()
Definition: sync.h:107
void Broadcast()
Definition: sync.h:127
virtual void gpr_cv_signal(gpr_cv *cv)=0
virtual void gpr_cv_broadcast(gpr_cv *cv)=0
~MutexLock()
Definition: sync.h:77
virtual void gpr_mu_destroy(gpr_mu *mu)=0
Definition: sync.h:69
~ReleasableMutexLock()
Definition: sync.h:94
Mutex()
Definition: sync.h:49
virtual int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)=0
MutexLock(gpr_mu *mu)
Definition: sync.h:74
ReleasableMutexLock & operator=(const ReleasableMutexLock &)=delete
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
MutexLock(Mutex *mu)
Definition: sync.h:71
CondVar()
Definition: sync.h:120
Definition: sync.h:47
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Definition: sync_windows.h:26
virtual void gpr_mu_lock(gpr_mu *mu)=0
~Mutex()
Definition: sync.h:50
~CondVar()
Definition: sync.h:121
virtual void gpr_cv_destroy(gpr_cv *cv)=0
Realtime clock.
Definition: gpr_types.h:36
void Signal()
Definition: sync.h:126
virtual void gpr_mu_init(gpr_mu *mu)=0
MutexLock & operator=(const MutexLock &)=delete
void Lock()
Definition: sync.h:101
int Wait(Mutex *mu, const gpr_timespec &deadline)
Definition: sync.h:133
Analogous to struct timespec.
Definition: gpr_types.h:47
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
int Wait(Mutex *mu)
Definition: sync.h:129
CondVar & operator=(const CondVar &)=delete
Mutex & operator=(const Mutex &)=delete
ReleasableMutexLock(gpr_mu *mu)
Definition: sync.h:91
Definition: sync.h:118