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