d53585995cbf339a77fa84108f45163e7f427b43
[platform/upstream/grpc.git] / src / core / lib / gprpp / sync.h
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
22 #include <grpc/support/port_platform.h>
23
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 #include <grpc/support/time.h>
27
28 #include "absl/synchronization/mutex.h"
29 #include "src/core/lib/gprpp/time_util.h"
30
31 // The core library is not accessible in C++ codegen headers, and vice versa.
32 // Thus, we need to have duplicate headers with similar functionality.
33 // Make sure any change to this file is also reflected in
34 // include/grpcpp/impl/codegen/sync.h.
35 //
36 // Whenever possible, prefer using this file over <grpcpp/impl/codegen/sync.h>
37 // since this file doesn't rely on g_core_codegen_interface and hence does not
38 // pay the costs of virtual function calls.
39
40 namespace grpc_core {
41
42 #ifdef GPR_ABSEIL_SYNC
43
44 using Mutex = absl::Mutex;
45 using MutexLock = absl::MutexLock;
46 using ReleasableMutexLock = absl::ReleasableMutexLock;
47 using CondVar = absl::CondVar;
48
49 // Returns the underlying gpr_mu from Mutex. This should be used only when
50 // it has to like passing the C++ mutex to C-core API.
51 // TODO(veblush): Remove this after C-core no longer uses gpr_mu.
52 inline gpr_mu* GetUnderlyingGprMu(Mutex* mutex) {
53   return reinterpret_cast<gpr_mu*>(mutex);
54 }
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   bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
69     return gpr_mu_trylock(&mu_) != 0;
70   }
71
72  private:
73   gpr_mu mu_;
74
75   friend class CondVar;
76   friend gpr_mu* GetUnderlyingGprMu(Mutex* mutex);
77 };
78
79 // Returns the underlying gpr_mu from Mutex. This should be used only when
80 // it has to like passing the C++ mutex to C-core API.
81 // TODO(veblush): Remove this after C-core no longer uses gpr_mu.
82 inline gpr_mu* GetUnderlyingGprMu(Mutex* mutex) { return &mutex->mu_; }
83
84 class ABSL_SCOPED_LOCKABLE MutexLock {
85  public:
86   explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
87     mu_->Lock();
88   }
89   ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
90
91   MutexLock(const MutexLock&) = delete;
92   MutexLock& operator=(const MutexLock&) = delete;
93
94  private:
95   Mutex* const mu_;
96 };
97
98 class ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
99  public:
100   explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
101       : mu_(mu) {
102     mu_->Lock();
103   }
104   ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
105     if (!released_) mu_->Unlock();
106   }
107
108   ReleasableMutexLock(const ReleasableMutexLock&) = delete;
109   ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
110
111   void Release() ABSL_UNLOCK_FUNCTION() {
112     GPR_DEBUG_ASSERT(!released_);
113     released_ = true;
114     mu_->Unlock();
115   }
116
117  private:
118   Mutex* const mu_;
119   bool released_ = false;
120 };
121
122 class CondVar {
123  public:
124   CondVar() { gpr_cv_init(&cv_); }
125   ~CondVar() { gpr_cv_destroy(&cv_); }
126
127   CondVar(const CondVar&) = delete;
128   CondVar& operator=(const CondVar&) = delete;
129
130   void Signal() { gpr_cv_signal(&cv_); }
131   void SignalAll() { gpr_cv_broadcast(&cv_); }
132
133   void Wait(Mutex* mu) { WaitWithDeadline(mu, absl::InfiniteFuture()); }
134   bool WaitWithTimeout(Mutex* mu, absl::Duration timeout) {
135     return gpr_cv_wait(&cv_, &mu->mu_, ToGprTimeSpec(timeout)) != 0;
136   }
137   bool WaitWithDeadline(Mutex* mu, absl::Time deadline) {
138     return gpr_cv_wait(&cv_, &mu->mu_, ToGprTimeSpec(deadline)) != 0;
139   }
140
141  private:
142   gpr_cv cv_;
143 };
144
145 #endif  // GPR_ABSEIL_SYNC
146
147 // Deprecated. Prefer MutexLock
148 class MutexLockForGprMu {
149  public:
150   explicit MutexLockForGprMu(gpr_mu* mu) : mu_(mu) { gpr_mu_lock(mu_); }
151   ~MutexLockForGprMu() { gpr_mu_unlock(mu_); }
152
153   MutexLockForGprMu(const MutexLock&) = delete;
154   MutexLockForGprMu& operator=(const MutexLock&) = delete;
155
156  private:
157   gpr_mu* const mu_;
158 };
159
160 // Deprecated. Prefer MutexLock or ReleasableMutexLock
161 class ABSL_SCOPED_LOCKABLE LockableAndReleasableMutexLock {
162  public:
163   explicit LockableAndReleasableMutexLock(Mutex* mu)
164       ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
165       : mu_(mu) {
166     mu_->Lock();
167   }
168   ~LockableAndReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
169     if (!released_) mu_->Unlock();
170   }
171
172   LockableAndReleasableMutexLock(const LockableAndReleasableMutexLock&) =
173       delete;
174   LockableAndReleasableMutexLock& operator=(
175       const LockableAndReleasableMutexLock&) = delete;
176
177   void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {
178     GPR_DEBUG_ASSERT(released_);
179     mu_->Lock();
180     released_ = false;
181   }
182
183   void Release() ABSL_UNLOCK_FUNCTION() {
184     GPR_DEBUG_ASSERT(!released_);
185     released_ = true;
186     mu_->Unlock();
187   }
188
189  private:
190   Mutex* const mu_;
191   bool released_ = false;
192 };
193
194 }  // namespace grpc_core
195
196 #endif /* GRPC_CORE_LIB_GPRPP_SYNC_H */