GRPC Core  9.0.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 GRPC_CORE_LIB_GPRPP_SYNC_H
20 #define GRPC_CORE_LIB_GPRPP_SYNC_H
21 
23 
24 #include <grpc/impl/codegen/log.h>
25 #include <grpc/impl/codegen/sync.h>
26 #include <grpc/support/sync.h>
27 #include <grpc/support/time.h>
28 
29 // The core library is not accessible in C++ codegen headers, and vice versa.
30 // Thus, we need to have duplicate headers with similar functionality.
31 // Make sure any change to this file is also reflected in
32 // include/grpcpp/impl/codegen/sync.h.
33 //
34 // Whenever possible, prefer using this file over <grpcpp/impl/codegen/sync.h>
35 // since this file doesn't rely on g_core_codegen_interface and hence does not
36 // pay the costs of virtual function calls.
37 
38 namespace grpc_core {
39 
40 class Mutex {
41  public:
42  Mutex() { gpr_mu_init(&mu_); }
43  ~Mutex() { gpr_mu_destroy(&mu_); }
44 
45  Mutex(const Mutex&) = delete;
46  Mutex& operator=(const Mutex&) = delete;
47 
48  gpr_mu* get() { return &mu_; }
49  const gpr_mu* get() const { return &mu_; }
50 
51  private:
52  gpr_mu mu_;
53 };
54 
55 // MutexLock is a std::
56 class MutexLock {
57  public:
58  explicit MutexLock(Mutex* mu) : mu_(mu->get()) { gpr_mu_lock(mu_); }
59  explicit MutexLock(gpr_mu* mu) : mu_(mu) { gpr_mu_lock(mu_); }
61 
62  MutexLock(const MutexLock&) = delete;
63  MutexLock& operator=(const MutexLock&) = delete;
64 
65  private:
66  gpr_mu* const mu_;
67 };
68 
70  public:
71  explicit ReleasableMutexLock(Mutex* mu) : mu_(mu->get()) { gpr_mu_lock(mu_); }
72  explicit ReleasableMutexLock(gpr_mu* mu) : mu_(mu) { gpr_mu_lock(mu_); }
74  if (!released_) gpr_mu_unlock(mu_);
75  }
76 
79 
80  void Lock() {
81  GPR_DEBUG_ASSERT(released_);
82  gpr_mu_lock(mu_);
83  released_ = false;
84  }
85 
86  void Unlock() {
87  GPR_DEBUG_ASSERT(!released_);
88  released_ = true;
89  gpr_mu_unlock(mu_);
90  }
91 
92  private:
93  gpr_mu* const mu_;
94  bool released_ = false;
95 };
96 
97 class CondVar {
98  public:
99  CondVar() { gpr_cv_init(&cv_); }
101 
102  CondVar(const CondVar&) = delete;
103  CondVar& operator=(const CondVar&) = delete;
104 
105  void Signal() { gpr_cv_signal(&cv_); }
106  void Broadcast() { gpr_cv_broadcast(&cv_); }
107 
109  int Wait(Mutex* mu, const gpr_timespec& deadline) {
110  return gpr_cv_wait(&cv_, mu->get(), deadline);
111  }
112 
113  template <typename Predicate>
114  void WaitUntil(Mutex* mu, Predicate pred) {
115  while (!pred()) {
117  }
118  }
119 
120  private:
121  gpr_cv cv_;
122 };
123 
124 } // namespace grpc_core
125 
126 #endif /* GRPC_CORE_LIB_GPRPP_SYNC_H */
Definition: sync.h:97
void WaitUntil(Mutex *mu, Predicate pred)
Definition: sync.h:114
CondVar(const CondVar &)=delete
void Signal()
Definition: sync.h:105
CondVar & operator=(const CondVar &)=delete
int Wait(Mutex *mu)
Definition: sync.h:108
CondVar()
Definition: sync.h:99
int Wait(Mutex *mu, const gpr_timespec &deadline)
Definition: sync.h:109
void Broadcast()
Definition: sync.h:106
~CondVar()
Definition: sync.h:100
Definition: sync.h:40
const gpr_mu * get() const
Definition: sync.h:49
~Mutex()
Definition: sync.h:43
Mutex()
Definition: sync.h:42
gpr_mu * get()
Definition: sync.h:48
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
Definition: sync.h:56
MutexLock(Mutex *mu)
Definition: sync.h:58
MutexLock & operator=(const MutexLock &)=delete
MutexLock(const MutexLock &)=delete
~MutexLock()
Definition: sync.h:60
MutexLock(gpr_mu *mu)
Definition: sync.h:59
Definition: sync.h:69
ReleasableMutexLock & operator=(const ReleasableMutexLock &)=delete
void Lock()
Definition: sync.h:80
ReleasableMutexLock(Mutex *mu)
Definition: sync.h:71
ReleasableMutexLock(gpr_mu *mu)
Definition: sync.h:72
~ReleasableMutexLock()
Definition: sync.h:73
void Unlock()
Definition: sync.h:86
ReleasableMutexLock(const ReleasableMutexLock &)=delete
@ GPR_CLOCK_REALTIME
Realtime clock.
Definition: gpr_types.h:36
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
Cause *cv no longer to be initialized, freeing any memory in use.
GPRAPI void gpr_cv_init(gpr_cv *cv)
— Condition variable interface —
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
Cause *mu no longer to be initialized, freeing any memory in use.
GPRAPI void gpr_cv_broadcast(gpr_cv *cv)
Wake all threads waiting on *cv.
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,...
GPRAPI void gpr_mu_init(gpr_mu *mu)
— Mutex interface —
GPRAPI void gpr_cv_signal(gpr_cv *cv)
If any threads are waiting on *cv, wake at least one.
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
Atomically release *mu and wait on *cv.
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
Release an exclusive lock on *mu held by the calling thread.
Round Robin Policy.
Definition: backend_metric.cc:24
Definition: sync_windows.h:26
Analogous to struct timespec.
Definition: gpr_types.h:47
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
The zero time interval.
Definition: time.cc:54
gpr_mu mu
Definition: timer_generic.cc:6