summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2012-06-29 17:10:08 +0000
committerDmitry Vyukov <dvyukov@google.com>2012-06-29 17:10:08 +0000
commitc04d8b39795b8faab2c172da59462f4e54823533 (patch)
treed5c989c569b4665fd4c124d13cba747f9402b7c6
parentfce5bd4cc29fddb5e8f0cb9c12df7c10187a991d (diff)
tsan/asan: unify ScopedLock
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@159438 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/asan/asan_lock.h13
-rw-r--r--lib/sanitizer_common/sanitizer_mutex.h58
-rw-r--r--lib/tsan/rtl/tsan_mutex.cc18
-rw-r--r--lib/tsan/rtl/tsan_mutex.h26
4 files changed, 63 insertions, 52 deletions
diff --git a/lib/asan/asan_lock.h b/lib/asan/asan_lock.h
index 15b2180ce..edee49adf 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 @@ class AsanLock {
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 000000000..c4e4f7f71
--- /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 1ef48a07c..68eab5d6c 100644
--- a/lib/tsan/rtl/tsan_mutex.cc
+++ b/lib/tsan/rtl/tsan_mutex.cc
@@ -256,22 +256,4 @@ void Mutex::ReadUnlock() {
#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 6fbf8220e..5b22a4145 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 @@ class Mutex {
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: