diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index 23959da..95a5766 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -25,7 +25,7 @@ struct HasLockAndUnlock { template static char Test(...); - static constexpr bool value = + static constexpr bool value = std::is_same(0)), int>::value; }; }// namespace internal @@ -75,42 +75,46 @@ public: LockGuard(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete; - explicit LockGuard(TLock *lock) : lock_(lock) { lock_->Lock(); }; + explicit LockGuard(TLock *lock) : mutex_(lock) { mutex_->Lock(); }; - ~LockGuard() { lock_->Unlock(); }; + ~LockGuard() { mutex_->Unlock(); }; private: - TLock *lock_; + TLock *mutex_; + friend class ConditionVariable; }; -class MutexLock final { -public: - MutexLock(const MutexLock &) = delete; - MutexLock &operator=(const MutexLock &) = delete; +using MutexLock = LockGuard; +using RecursiveMutexLock = LockGuard; - explicit MutexLock(Mutex *mutex) : mutex_(mutex) { mutex->Lock(); } - - ~MutexLock() { mutex_->Unlock(); } - -private: - Mutex *mutex_; -}; - -class RecursiveMutexLock final { -public: - RecursiveMutexLock(const RecursiveMutexLock &) = delete; - RecursiveMutexLock &operator=(const RecursiveMutexLock &) = delete; - - explicit RecursiveMutexLock(RecursiveMutex *mutex) : mutex_(mutex) - { - mutex->Lock(); - } - - ~RecursiveMutexLock() { mutex_->Unlock(); } - -private: - RecursiveMutex *mutex_; -}; +// class MutexLock final { +// public: +// MutexLock(const MutexLock &) = delete; +// MutexLock &operator=(const MutexLock &) = delete; +// +// explicit MutexLock(Mutex *mutex) : mutex_(mutex) { mutex->Lock(); } +// +// ~MutexLock() { mutex_->Unlock(); } +// +// private: +// Mutex *mutex_; +// }; +// +// class RecursiveMutexLock final { +// public: +// RecursiveMutexLock(const RecursiveMutexLock &) = delete; +// RecursiveMutexLock &operator=(const RecursiveMutexLock &) = delete; +// +// explicit RecursiveMutexLock(RecursiveMutex *mutex) : mutex_(mutex) +// { +// mutex->Lock(); +// } +// +// ~RecursiveMutexLock() { mutex_->Unlock(); } +// +// private: +// RecursiveMutex *mutex_; +// }; class ConditionVariable final { public: @@ -120,17 +124,18 @@ public: ConditionVariable &operator=(const ConditionVariable &) = delete; template - inline bool Wait(Mutex *mutex, Predicate pred) + inline bool Wait(LockGuard &guard, Predicate pred) { - std::unique_lock lock(mutex->impl_, std::adopt_lock); + std::unique_lock lock(guard.mutex_->impl_, std::adopt_lock); cv_.wait(lock, pred); return true; } template - inline bool WaitFor(Mutex *mutex, TimeDelta timeout, Predicate pred) + inline bool + WaitFor(LockGuard &guard, TimeDelta timeout, Predicate pred) { - std::unique_lock lock(mutex->impl_, std::adopt_lock); + std::unique_lock lock(guard.mutex_->impl_, std::adopt_lock); if (timeout == kForever) { cv_.wait(lock, pred); return true; diff --git a/src/synchronization/event.cc b/src/synchronization/event.cc index 95744cd..28e09a6 100644 --- a/src/synchronization/event.cc +++ b/src/synchronization/event.cc @@ -32,7 +32,7 @@ Event::Wait(TimeDelta give_up_after, TimeDelta warn_after) { MutexLock lock(&mutex_); bool wait_success = - cv_.WaitFor(&mutex_, give_up_after, [&] { return event_status_; }); + cv_.WaitFor(lock, give_up_after, [&] { return event_status_; }); if (!wait_success) { return false; } if (!is_manual_reset_) { event_status_ = false; }