[Sanitizer] Implement BlockingMutex::CheckLocked()
authorAlexey Samsonov <samsonov@google.com>
Mon, 11 Mar 2013 15:45:20 +0000 (15:45 +0000)
committerAlexey Samsonov <samsonov@google.com>
Mon, 11 Mar 2013 15:45:20 +0000 (15:45 +0000)
llvm-svn: 176805

compiler-rt/lib/sanitizer_common/sanitizer_linux.cc
compiler-rt/lib/sanitizer_common/sanitizer_mac.cc
compiler-rt/lib/sanitizer_common/sanitizer_mutex.h
compiler-rt/lib/sanitizer_common/sanitizer_win.cc
compiler-rt/lib/sanitizer_common/tests/sanitizer_mutex_test.cc

index 7d5b9a9..9b51d2c 100644 (file)
@@ -545,6 +545,11 @@ void BlockingMutex::Unlock() {
     syscall(__NR_futex, m, FUTEX_WAKE, 1, 0, 0, 0);
 }
 
+void BlockingMutex::CheckLocked() {
+  atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
+  CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
+}
+
 // ----------------- sanitizer_linux.h
 // The actual size of this structure is specified by d_reclen.
 // Note that getdents64 uses a different structure format. We only provide the
index 17bc223..0f11220 100644 (file)
@@ -317,6 +317,10 @@ void BlockingMutex::Unlock() {
   OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
 }
 
+void BlockingMutex::CheckLocked() {
+  CHECK_EQ((uptr)pthread_self(), owner_);
+}
+
 }  // namespace __sanitizer
 
 #endif  // __APPLE__
index 56438fc..be3d559 100644 (file)
@@ -72,6 +72,7 @@ class BlockingMutex {
   explicit BlockingMutex(LinkerInitialized);
   void Lock();
   void Unlock();
+  void CheckLocked();
  private:
   uptr opaque_storage_[10];
   uptr owner_;  // for debugging
index 40af4e3..7b540a2 100644 (file)
@@ -291,6 +291,10 @@ void BlockingMutex::Unlock() {
   LeaveCriticalSection((LPCRITICAL_SECTION)opaque_storage_);
 }
 
+void BlockingMutex::CheckLocked() {
+  CHECK_EQ(owner_, GetThreadSelf());
+}
+
 }  // namespace __sanitizer
 
 #endif  // _WIN32
index 6bb2ae2..1dc9bef 100644 (file)
@@ -92,6 +92,12 @@ static void *try_thread(void *param) {
   return 0;
 }
 
+template<typename MutexType>
+static void check_locked(MutexType *mtx) {
+  GenericScopedLock<MutexType> l(mtx);
+  mtx->CheckLocked();
+}
+
 TEST(SanitizerCommon, SpinMutex) {
   SpinMutex mtx;
   mtx.Init();
@@ -123,6 +129,7 @@ TEST(SanitizerCommon, BlockingMutex) {
     pthread_create(&threads[i], 0, lock_thread<BlockingMutex>, &data);
   for (int i = 0; i < kThreads; i++)
     pthread_join(threads[i], 0);
+  check_locked(mtx);
 }
 
 }  // namespace __sanitizer