tsan/asan: unify ScopedLock


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@159438 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_lock.h b/lib/asan/asan_lock.h
index 15b2180..edee49a 100644
--- a/lib/asan/asan_lock.h
+++ b/lib/asan/asan_lock.h
@@ -14,6 +14,7 @@
 #ifndef ASAN_LOCK_H
 #define ASAN_LOCK_H
 
+#include "sanitizer_common/sanitizer_mutex.h"
 #include "asan_internal.h"
 
 // The locks in ASan are global objects and they are never destroyed to avoid
@@ -34,17 +35,7 @@
   uptr owner_;  // for debugging and for malloc_introspection_t interface
 };
 
-class ScopedLock {
- public:
-  explicit ScopedLock(AsanLock *mu) : mu_(mu) {
-    mu_->Lock();
-  }
-  ~ScopedLock() {
-    mu_->Unlock();
-  }
- private:
-  AsanLock *mu_;
-};
+typedef GenericScopedLock<AsanLock> ScopedLock;
 
 }  // namespace __asan
 
diff --git a/lib/sanitizer_common/sanitizer_mutex.h b/lib/sanitizer_common/sanitizer_mutex.h
new file mode 100644
index 0000000..c4e4f7f
--- /dev/null
+++ b/lib/sanitizer_common/sanitizer_mutex.h
@@ -0,0 +1,58 @@
+//===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SANITIZER_MUTEX_H
+#define SANITIZER_MUTEX_H
+
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_atomic.h"
+
+namespace __sanitizer {
+
+template<typename MutexType>
+class GenericScopedLock {
+ public:
+  explicit GenericScopedLock(MutexType *mu)
+      : mu_(mu) {
+    mu_->Lock();
+  }
+
+  ~GenericScopedLock() {
+    mu_->Unlock();
+  }
+
+ private:
+  MutexType *mu_;
+
+  GenericScopedLock(const GenericScopedLock&);
+  void operator=(const GenericScopedLock&);
+};
+
+template<typename MutexType>
+class GenericScopedReadLock {
+ public:
+  explicit GenericScopedReadLock(MutexType *mu)
+      : mu_(mu) {
+    mu_->ReadLock();
+  }
+
+  ~GenericScopedReadLock() {
+    mu_->ReadUnlock();
+  }
+
+ private:
+  MutexType *mu_;
+
+  GenericScopedReadLock(const GenericScopedReadLock&);
+  void operator=(const GenericScopedReadLock&);
+};
+
+}  // namespace __sanitizer
+
+#endif  // SANITIZER_MUTEX_H
diff --git a/lib/tsan/rtl/tsan_mutex.cc b/lib/tsan/rtl/tsan_mutex.cc
index 1ef48a0..68eab5d 100644
--- a/lib/tsan/rtl/tsan_mutex.cc
+++ b/lib/tsan/rtl/tsan_mutex.cc
@@ -256,22 +256,4 @@
 #endif
 }
 
-Lock::Lock(Mutex *m)
-  : m_(m) {
-  m_->Lock();
-}
-
-Lock::~Lock() {
-  m_->Unlock();
-}
-
-ReadLock::ReadLock(Mutex *m)
-  : m_(m) {
-  m_->ReadLock();
-}
-
-ReadLock::~ReadLock() {
-  m_->ReadUnlock();
-}
-
 }  // namespace __tsan
diff --git a/lib/tsan/rtl/tsan_mutex.h b/lib/tsan/rtl/tsan_mutex.h
index 6fbf822..5b22a41 100644
--- a/lib/tsan/rtl/tsan_mutex.h
+++ b/lib/tsan/rtl/tsan_mutex.h
@@ -14,6 +14,7 @@
 #define TSAN_MUTEX_H
 
 #include "sanitizer_common/sanitizer_atomic.h"
+#include "sanitizer_common/sanitizer_mutex.h"
 #include "tsan_defs.h"
 
 namespace __tsan {
@@ -57,29 +58,8 @@
   void operator = (const Mutex&);
 };
 
-class Lock {
- public:
-  explicit Lock(Mutex *m);
-  ~Lock();
-
- private:
-  Mutex *m_;
-
-  Lock(const Lock&);
-  void operator = (const Lock&);
-};
-
-class ReadLock {
- public:
-  explicit ReadLock(Mutex *m);
-  ~ReadLock();
-
- private:
-  Mutex *m_;
-
-  ReadLock(const ReadLock&);
-  void operator = (const ReadLock&);
-};
+typedef GenericScopedLock<Mutex> Lock;
+typedef GenericScopedReadLock<Mutex> ReadLock;
 
 class DeadlockDetector {
  public: