rtc::CriticalSection: Add function IsLocked

R=tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/50759004

Cr-Commit-Position: refs/heads/master@{#9065}
This commit is contained in:
Magnus Jedvert 2015-04-23 11:37:55 +02:00
parent bd67f66ebd
commit 6bf10843bf
2 changed files with 25 additions and 2 deletions

View File

@ -27,9 +27,9 @@
#include <pthread.h>
#endif
#ifdef _DEBUG
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
#define CS_TRACK_OWNER 1
#endif // _DEBUG
#endif
#if CS_TRACK_OWNER
#define TRACK_OWNER(x) x
@ -58,6 +58,8 @@ class LOCKABLE CriticalSection {
bool CurrentThreadIsOwner() const {
return crit_.OwningThread == reinterpret_cast<HANDLE>(GetCurrentThreadId());
}
// Use only for DCHECKing.
bool IsLocked() { return crit_.LockCount != -1; }
private:
CRITICAL_SECTION crit_;
@ -102,6 +104,10 @@ class LOCKABLE CriticalSection {
return true;
#endif // CS_TRACK_OWNER
}
// Use only for DCHECKing.
#if CS_TRACK_OWNER
bool IsLocked() { return thread_ != 0; }
#endif
private:
pthread_mutex_t mutex_;

View File

@ -281,4 +281,21 @@ TEST(CriticalSectionTest, Basic) {
EXPECT_EQ(0, runner.shared_value());
}
#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
TEST(CriticalSectionTest, IsLocked) {
// Simple single-threaded test of IsLocked.
CriticalSection cs;
EXPECT_FALSE(cs.IsLocked());
cs.Enter();
EXPECT_TRUE(cs.IsLocked());
cs.Leave();
EXPECT_FALSE(cs.IsLocked());
if (!cs.TryEnter())
FAIL();
EXPECT_TRUE(cs.IsLocked());
cs.Leave();
EXPECT_FALSE(cs.IsLocked());
}
#endif
} // namespace rtc