fix replace MutexLock by LockGuard<Mutex>
This commit is contained in:
parent
b1a9a8a17f
commit
9cc2ec86b7
@ -25,7 +25,7 @@ struct HasLockAndUnlock {
|
|||||||
template<typename>
|
template<typename>
|
||||||
static char Test(...);
|
static char Test(...);
|
||||||
|
|
||||||
static constexpr bool value =
|
static constexpr bool value =
|
||||||
std::is_same<decltype(Test<T>(0)), int>::value;
|
std::is_same<decltype(Test<T>(0)), int>::value;
|
||||||
};
|
};
|
||||||
}// namespace internal
|
}// namespace internal
|
||||||
@ -75,42 +75,46 @@ public:
|
|||||||
LockGuard(const LockGuard &) = delete;
|
LockGuard(const LockGuard &) = delete;
|
||||||
LockGuard &operator=(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:
|
private:
|
||||||
TLock *lock_;
|
TLock *mutex_;
|
||||||
|
friend class ConditionVariable;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MutexLock final {
|
using MutexLock = LockGuard<Mutex>;
|
||||||
public:
|
using RecursiveMutexLock = LockGuard<RecursiveMutex>;
|
||||||
MutexLock(const MutexLock &) = delete;
|
|
||||||
MutexLock &operator=(const MutexLock &) = delete;
|
|
||||||
|
|
||||||
explicit MutexLock(Mutex *mutex) : mutex_(mutex) { mutex->Lock(); }
|
// class MutexLock final {
|
||||||
|
// public:
|
||||||
~MutexLock() { mutex_->Unlock(); }
|
// MutexLock(const MutexLock &) = delete;
|
||||||
|
// MutexLock &operator=(const MutexLock &) = delete;
|
||||||
private:
|
//
|
||||||
Mutex *mutex_;
|
// explicit MutexLock(Mutex *mutex) : mutex_(mutex) { mutex->Lock(); }
|
||||||
};
|
//
|
||||||
|
// ~MutexLock() { mutex_->Unlock(); }
|
||||||
class RecursiveMutexLock final {
|
//
|
||||||
public:
|
// private:
|
||||||
RecursiveMutexLock(const RecursiveMutexLock &) = delete;
|
// Mutex *mutex_;
|
||||||
RecursiveMutexLock &operator=(const RecursiveMutexLock &) = delete;
|
// };
|
||||||
|
//
|
||||||
explicit RecursiveMutexLock(RecursiveMutex *mutex) : mutex_(mutex)
|
// class RecursiveMutexLock final {
|
||||||
{
|
// public:
|
||||||
mutex->Lock();
|
// RecursiveMutexLock(const RecursiveMutexLock &) = delete;
|
||||||
}
|
// RecursiveMutexLock &operator=(const RecursiveMutexLock &) = delete;
|
||||||
|
//
|
||||||
~RecursiveMutexLock() { mutex_->Unlock(); }
|
// explicit RecursiveMutexLock(RecursiveMutex *mutex) : mutex_(mutex)
|
||||||
|
// {
|
||||||
private:
|
// mutex->Lock();
|
||||||
RecursiveMutex *mutex_;
|
// }
|
||||||
};
|
//
|
||||||
|
// ~RecursiveMutexLock() { mutex_->Unlock(); }
|
||||||
|
//
|
||||||
|
// private:
|
||||||
|
// RecursiveMutex *mutex_;
|
||||||
|
// };
|
||||||
|
|
||||||
class ConditionVariable final {
|
class ConditionVariable final {
|
||||||
public:
|
public:
|
||||||
@ -120,17 +124,18 @@ public:
|
|||||||
ConditionVariable &operator=(const ConditionVariable &) = delete;
|
ConditionVariable &operator=(const ConditionVariable &) = delete;
|
||||||
|
|
||||||
template<typename Predicate>
|
template<typename Predicate>
|
||||||
inline bool Wait(Mutex *mutex, Predicate pred)
|
inline bool Wait(LockGuard<Mutex> &guard, Predicate pred)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex->impl_, std::adopt_lock);
|
std::unique_lock<std::mutex> lock(guard.mutex_->impl_, std::adopt_lock);
|
||||||
cv_.wait(lock, pred);
|
cv_.wait(lock, pred);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Predicate>
|
template<typename Predicate>
|
||||||
inline bool WaitFor(Mutex *mutex, TimeDelta timeout, Predicate pred)
|
inline bool
|
||||||
|
WaitFor(LockGuard<Mutex> &guard, TimeDelta timeout, Predicate pred)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex->impl_, std::adopt_lock);
|
std::unique_lock<std::mutex> lock(guard.mutex_->impl_, std::adopt_lock);
|
||||||
if (timeout == kForever) {
|
if (timeout == kForever) {
|
||||||
cv_.wait(lock, pred);
|
cv_.wait(lock, pred);
|
||||||
return true;
|
return true;
|
||||||
|
@ -32,7 +32,7 @@ Event::Wait(TimeDelta give_up_after, TimeDelta warn_after)
|
|||||||
{
|
{
|
||||||
MutexLock lock(&mutex_);
|
MutexLock lock(&mutex_);
|
||||||
bool wait_success =
|
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 (!wait_success) { return false; }
|
||||||
|
|
||||||
if (!is_manual_reset_) { event_status_ = false; }
|
if (!is_manual_reset_) { event_status_ = false; }
|
||||||
|
Loading…
Reference in New Issue
Block a user