GRPC C++  1.62.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_SYNC_H
20 #define GRPCPP_IMPL_SYNC_H
21 
23 
24 #ifdef GPR_HAS_PTHREAD_H
25 #include <pthread.h>
26 #endif
27 
28 #include <mutex>
29 
30 #include "absl/synchronization/mutex.h"
31 
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34 #include <grpc/support/time.h>
35 
36 // The core library is not accessible in C++ codegen headers, and vice versa.
37 // Thus, we need to have duplicate headers with similar functionality.
38 // Make sure any change to this file is also reflected in
39 // src/core/lib/gprpp/sync.h too.
40 //
41 // Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file,
42 // since in core we do not rely on g_core_codegen_interface and hence do not
43 // pay the costs of virtual function calls.
44 
45 namespace grpc {
46 namespace internal {
47 
48 #ifdef GPR_ABSEIL_SYNC
49 
50 using Mutex = absl::Mutex;
51 using MutexLock = absl::MutexLock;
52 using ReleasableMutexLock = absl::ReleasableMutexLock;
53 using CondVar = absl::CondVar;
54 
55 #else
56 
57 class ABSL_LOCKABLE Mutex {
58  public:
59  Mutex() { gpr_mu_init(&mu_); }
60  ~Mutex() { gpr_mu_destroy(&mu_); }
61 
62  Mutex(const Mutex&) = delete;
63  Mutex& operator=(const Mutex&) = delete;
64 
65  void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { gpr_mu_lock(&mu_); }
66  void Unlock() ABSL_UNLOCK_FUNCTION() { gpr_mu_unlock(&mu_); }
67 
68  private:
69  union {
71  std::mutex do_not_use_sth_;
72 #ifdef GPR_HAS_PTHREAD_H
73  pthread_mutex_t do_not_use_pth_;
74 #endif
75  };
76 
77  friend class CondVar;
78 };
79 
80 class ABSL_SCOPED_LOCKABLE MutexLock {
81  public:
82  explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
83  mu_->Lock();
84  }
85  ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
86 
87  MutexLock(const MutexLock&) = delete;
88  MutexLock& operator=(const MutexLock&) = delete;
89 
90  private:
91  Mutex* const mu_;
92 };
93 
94 class ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
95  public:
96  explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
97  : mu_(mu) {
98  mu_->Lock();
99  }
100  ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
101  if (!released_) mu_->Unlock();
102  }
103 
104  ReleasableMutexLock(const ReleasableMutexLock&) = delete;
105  ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
106 
107  void Release() ABSL_UNLOCK_FUNCTION() {
108  GPR_DEBUG_ASSERT(!released_);
109  released_ = true;
110  mu_->Unlock();
111  }
112 
113  private:
114  Mutex* const mu_;
115  bool released_ = false;
116 };
117 
118 class CondVar {
119  public:
120  CondVar() { gpr_cv_init(&cv_); }
122 
123  CondVar(const CondVar&) = delete;
124  CondVar& operator=(const CondVar&) = delete;
125 
126  void Signal() { gpr_cv_signal(&cv_); }
127  void SignalAll() { gpr_cv_broadcast(&cv_); }
128 
129  void Wait(Mutex* mu) {
131  }
132 
133  private:
134  gpr_cv cv_;
135 };
136 
137 #endif // GPR_ABSEIL_SYNC
138 
139 template <typename Predicate>
140 GRPC_DEPRECATED("incompatible with thread safety analysis")
141 static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) {
142  while (!pred()) {
143  cv->Wait(mu);
144  }
145 }
146 
147 } // namespace internal
148 } // namespace grpc
149 
150 #endif // GRPCPP_IMPL_SYNC_H
grpc::internal::Mutex::Unlock
void Unlock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:66
grpc::internal::CondVar
Definition: sync.h:118
grpc::internal::CondVar::Signal
void Signal()
Definition: sync.h:126
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
gpr_mu
pthread_mutex_t gpr_mu
Definition: sync_posix.h:45
grpc::internal::CondVar::operator=
CondVar & operator=(const CondVar &)=delete
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
Cause *mu no longer to be initialized, freeing any memory in use.
grpc::internal::MutexLock::MutexLock
MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: sync.h:82
sync.h
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
Atomically release *mu and wait on *cv.
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The far future.
grpc::internal::CondVar::CondVar
CondVar()
Definition: sync.h:120
grpc::internal::CondVar::SignalAll
void SignalAll()
Definition: sync.h:127
grpc::internal::Mutex::~Mutex
~Mutex()
Definition: sync.h:60
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
— Condition variable interface —
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
Cause *cv no longer to be initialized, freeing any memory in use.
log.h
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
If any threads are waiting on *cv, wake at least one.
gpr_cv
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
grpc::internal::Mutex::mu_
gpr_mu mu_
Definition: sync.h:70
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
Wait until no thread has a lock on *mu, cause the calling thread to own an exclusive lock on *mu,...
gpr_cv_broadcast
GPRAPI void gpr_cv_broadcast(gpr_cv *cv)
Wake all threads waiting on *cv.
grpc::internal::MutexLock
Definition: sync.h:80
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
Release an exclusive lock on *mu held by the calling thread.
grpc::internal::ReleasableMutexLock::~ReleasableMutexLock
~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:100
GRPC_DEPRECATED
#define GRPC_DEPRECATED(reason)
Definition: port_platform.h:26
grpc::internal::CondVar::Wait
void Wait(Mutex *mu)
Definition: sync.h:129
grpc::internal::CondVar::~CondVar
~CondVar()
Definition: sync.h:121
grpc::internal::ReleasableMutexLock
Definition: sync.h:94
grpc::internal::ReleasableMutexLock::ReleasableMutexLock
ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: sync.h:96
grpc::internal::Mutex::Lock
void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:65
grpc::internal::Mutex
Definition: sync.h:57
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
Synchronization primitives for GPR.
grpc::internal::ReleasableMutexLock::Release
void Release() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:107
grpc::internal::Mutex::do_not_use_sth_
std::mutex do_not_use_sth_
Definition: sync.h:71
grpc::internal::Mutex::Mutex
Mutex()
Definition: sync.h:59
grpc::internal::MutexLock::~MutexLock
~MutexLock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:85
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Realtime clock.
Definition: time.h:37
time.h
port_platform.h