34 namespace synchronization_internal {
56 MutexImpl::MutexImpl() {}
58 MutexImpl::~MutexImpl() {
64 void MutexImpl::Lock() {
69 bool MutexImpl::TryLock() {
70 bool locked = std_mutex_.try_lock();
71 if (locked) locked_ =
true;
75 void MutexImpl::Unlock() {
77 released_.SignalAll();
81 CondVarImpl::CondVarImpl() {}
83 CondVarImpl::~CondVarImpl() {}
85 void CondVarImpl::Signal() { std_cv_.notify_one(); }
87 void CondVarImpl::SignalAll() { std_cv_.notify_all(); }
89 void CondVarImpl::Wait(MutexImpl* mu) {
90 mu->released_.SignalAll();
91 std_cv_.wait(mu->std_mutex_);
94 bool CondVarImpl::WaitWithDeadline(MutexImpl* mu,
absl::Time deadline) {
95 mu->released_.SignalAll();
96 time_t when =
ToTimeT(deadline);
99 std::chrono::system_clock::from_time_t(when) +
100 std::chrono::duration_cast<std::chrono::system_clock::duration>(
101 std::chrono::nanoseconds(nanos));
102 auto deadline_since_epoch =
103 std::chrono::duration_cast<std::chrono::duration<double>>(
104 deadline_tp - std::chrono::system_clock::from_time_t(0));
105 return std_cv_.wait_until(mu->std_mutex_, deadline_tp) ==
106 std::cv_status::timeout;
111 MutexImpl::MutexImpl() {
116 MutexImpl::~MutexImpl() {
118 ABSL_RAW_CHECK(pthread_mutex_unlock(&pthread_mutex_) == 0,
"pthread error");
120 ABSL_RAW_CHECK(pthread_mutex_destroy(&pthread_mutex_) == 0,
"pthread error");
123 void MutexImpl::Lock() {
124 ABSL_RAW_CHECK(pthread_mutex_lock(&pthread_mutex_) == 0,
"pthread error");
128 bool MutexImpl::TryLock() {
129 bool locked = (0 == pthread_mutex_trylock(&pthread_mutex_));
130 if (locked) locked_ =
true;
134 void MutexImpl::Unlock() {
136 released_.SignalAll();
137 ABSL_RAW_CHECK(pthread_mutex_unlock(&pthread_mutex_) == 0,
"pthread error");
140 CondVarImpl::CondVarImpl() {
145 CondVarImpl::~CondVarImpl() {
146 ABSL_RAW_CHECK(pthread_cond_destroy(&pthread_cv_) == 0,
"pthread error");
149 void CondVarImpl::Signal() {
150 ABSL_RAW_CHECK(pthread_cond_signal(&pthread_cv_) == 0,
"pthread error");
153 void CondVarImpl::SignalAll() {
154 ABSL_RAW_CHECK(pthread_cond_broadcast(&pthread_cv_) == 0,
"pthread error");
157 void CondVarImpl::Wait(MutexImpl* mu) {
158 mu->released_.SignalAll();
159 ABSL_RAW_CHECK(pthread_cond_wait(&pthread_cv_, &mu->pthread_mutex_) == 0,
163 bool CondVarImpl::WaitWithDeadline(MutexImpl* mu,
absl::Time deadline) {
164 mu->released_.SignalAll();
166 int rc = pthread_cond_timedwait(&pthread_cv_, &mu->pthread_mutex_, &ts);
167 if (rc == ETIMEDOUT)
return true;
174 void MutexImpl::Await(
const Condition& cond) {
175 if (cond.Eval())
return;
176 released_.SignalAll();
178 released_.Wait(
this);
179 }
while (!cond.Eval());
182 bool MutexImpl::AwaitWithDeadline(
const Condition& cond,
absl::Time deadline) {
183 if (cond.Eval())
return true;
184 released_.SignalAll();
186 if (released_.WaitWithDeadline(
this, deadline))
return false;
187 if (cond.Eval())
return true;
215 return impl()->AwaitWithDeadline(
216 cond, synchronization_internal::LimitedDeadline(deadline));
220 return AwaitWithDeadline(
226 return AwaitWithDeadline(cond, deadline);
230 return LockWhenWithDeadline(
241 return LockWhenWithTimeout(cond, timeout);
245 return LockWhenWithDeadline(cond, deadline);
266 return impl()->WaitWithDeadline(
267 mu->impl(), synchronization_internal::LimitedDeadline(deadline));
271 return WaitWithDeadline(mu,
absl::Now() + timeout);
276 #ifdef THREAD_SANITIZER 279 #define __tsan_read1(addr) // do nothing if TSan not enabled 288 return *(
static_cast<bool *
>(
arg));
295 : eval_(&CallVoidPtrFunction),
313 return (this->
eval_ ==
nullptr) || (*this->
eval_)(
this);
timespec ToTimespec(Duration d)
bool AwaitWithDeadline(const Condition &cond, absl::Time deadline)
static const Condition kTrue
void ForgetDeadlockInfo()
void ReaderLockWhen(const Condition &cond) SHARED_LOCK_FUNCTION()
bool WaitWithDeadline(Mutex *mu, absl::Time deadline)
bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true)
static bool Dereference(void *arg)
void LockWhen(const Condition &cond) EXCLUSIVE_LOCK_FUNCTION()
InternalMethodType method_
bool ReaderLockWhenWithDeadline(const Condition &cond, absl::Time deadline) SHARED_LOCK_FUNCTION()
InternalFunctionType function_
void RegisterSymbolizer(bool(*)(const void *, char *, int))
void EnableInvariantDebugging(void(*invariant)(void *), void *arg)
void EnableDebugLog(const char *name)
void EnableDebugLog(const char *name)
static absl::Time DeadlineFromTimeout(absl::Duration timeout)
bool ReaderLockWhenWithTimeout(const Condition &cond, absl::Duration timeout) SHARED_LOCK_FUNCTION()
bool(* eval_)(const Condition *)
int64_t ToInt64Nanoseconds(Duration d)
#define ABSL_RAW_CHECK(condition, message)
bool LockWhenWithDeadline(const Condition &cond, absl::Time deadline) EXCLUSIVE_LOCK_FUNCTION()
void ReaderUnlock() UNLOCK_FUNCTION()
void Unlock() UNLOCK_FUNCTION()
bool LockWhenWithTimeout(const Condition &cond, absl::Duration timeout) EXCLUSIVE_LOCK_FUNCTION()
void AssertHeld() const ASSERT_EXCLUSIVE_LOCK()
bool AwaitWithTimeout(const Condition &cond, absl::Duration timeout)
void Lock() EXCLUSIVE_LOCK_FUNCTION()
std::chrono::time_point< std::chrono::system_clock, D > time_point
#define __tsan_read1(addr)
void AssertNotHeld() const
void Await(const Condition &cond)
bool WaitWithTimeout(Mutex *mu, absl::Duration timeout)
void ReaderLock() SHARED_LOCK_FUNCTION()
static bool CallVoidPtrFunction(const Condition *)
void AssertReaderHeld() const ASSERT_SHARED_LOCK()
constexpr Time FromTimeT(time_t t)