19 #ifndef GRPC_CORE_LIB_GPRPP_SYNC_H
20 #define GRPC_CORE_LIB_GPRPP_SYNC_H
29 #include "absl/synchronization/mutex.h"
43 #ifdef GPR_ABSEIL_SYNC
45 using Mutex = absl::Mutex;
46 using MutexLock = absl::MutexLock;
47 using ReleasableMutexLock = absl::ReleasableMutexLock;
48 using CondVar = absl::CondVar;
54 return reinterpret_cast<gpr_mu*
>(mutex);
69 bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
106 if (!released_) mu_->Unlock();
120 bool released_ =
false;
148 template <
typename Predicate>
149 static void WaitUntil(CondVar* cv, Mutex*
mu, Predicate pred) {
156 template <
typename Predicate>
157 static bool WaitUntilWithTimeout(CondVar* cv, Mutex*
mu, Predicate pred,
158 absl::Duration timeout) {
160 if (cv->WaitWithTimeout(
mu, timeout))
return true;
166 template <
typename Predicate>
167 static bool WaitUntilWithDeadline(CondVar* cv, Mutex*
mu, Predicate pred,
168 absl::Time deadline) {
170 if (cv->WaitWithDeadline(
mu, deadline))
return true;
192 ABSL_EXCLUSIVE_LOCK_FUNCTION(
mu)
197 if (!released_) mu_->Unlock();
205 void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {
219 bool released_ =
false;
bool WaitWithDeadline(Mutex *mu, absl::Time deadline)
Definition: sync.h:138
CondVar(const CondVar &)=delete
void Signal()
Definition: sync.h:131
CondVar & operator=(const CondVar &)=delete
bool WaitWithTimeout(Mutex *mu, absl::Duration timeout)
Definition: sync.h:135
void Wait(Mutex *mu)
Definition: sync.h:134
CondVar()
Definition: sync.h:125
void SignalAll()
Definition: sync.h:132
~CondVar()
Definition: sync.h:126
LockableAndReleasableMutexLock & operator=(const LockableAndReleasableMutexLock &)=delete
void Release() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:211
void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:205
~LockableAndReleasableMutexLock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:196
LockableAndReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: sync.h:191
LockableAndReleasableMutexLock(const LockableAndReleasableMutexLock &)=delete
bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true)
Definition: sync.h:69
void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: sync.h:67
~Mutex()
Definition: sync.h:62
Mutex()
Definition: sync.h:61
void Unlock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:68
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
MutexLockForGprMu & operator=(const MutexLock &)=delete
MutexLockForGprMu(const MutexLock &)=delete
~MutexLockForGprMu()
Definition: sync.h:179
MutexLockForGprMu(gpr_mu *mu)
Definition: sync.h:178
MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: sync.h:87
MutexLock & operator=(const MutexLock &)=delete
~MutexLock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:90
MutexLock(const MutexLock &)=delete
~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:105
ReleasableMutexLock & operator=(const ReleasableMutexLock &)=delete
void Release() ABSL_UNLOCK_FUNCTION()
Definition: sync.h:112
ReleasableMutexLock(const ReleasableMutexLock &)=delete
ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition: sync.h:101
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:101
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
pthread_mutex_t gpr_mu
Definition: sync_posix.h:45
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 int gpr_mu_trylock(gpr_mu *mu)
Without blocking, attempt to acquire an exclusive lock on *mu for the calling thread,...
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:26
gpr_mu * GetUnderlyingGprMu(Mutex *mutex)
Definition: sync.h:83
gpr_timespec ToGprTimeSpec(absl::Duration duration)
Definition: time_util.cc:25
gpr_mu mu
Definition: timer_generic.cc:6