aboutsummaryrefslogtreecommitdiff
path: root/libsanitizer/sanitizer_common/sanitizer_mutex.h
diff options
context:
space:
mode:
authorWei Mi <wmi@google.com>2012-11-12 15:53:47 +0000
committerDodji Seketeli <dodji@gcc.gnu.org>2012-11-12 16:53:47 +0100
commitf35db108b96cac4fd3f2b62024ed93ac006ff932 (patch)
tree78af479c74cf29780202765e911b12f4bf192001 /libsanitizer/sanitizer_common/sanitizer_mutex.h
parent25ae50273ad2801ecc262ba75fa8bac0c4e0001b (diff)
downloadgcc-f35db108b96cac4fd3f2b62024ed93ac006ff932.zip
gcc-f35db108b96cac4fd3f2b62024ed93ac006ff932.tar.gz
gcc-f35db108b96cac4fd3f2b62024ed93ac006ff932.tar.bz2
Import the asan runtime library into GCC tree
This patch imports the runtime library in the GCC tree, ensures that -lasan is passed to the linker when -faddress-sanitizer is used and sets up the build system accordingly. ChangeLog: * configure.ac: Add libsanitizer to target_libraries. * Makefile.def: Ditto. * configure: Regenerate. * Makefile.in: Regenerate. * libsanitizer: New directory for asan runtime. Contains an empty tsan directory. gcc/ChangeLog: * gcc.c (LINK_COMMAND_SPEC): Add -laddress-sanitizer to link command if -faddress-sanitizer is on. libsanitizer: Initial checkin: migrate asan runtime from llvm. From-SVN: r193441
Diffstat (limited to 'libsanitizer/sanitizer_common/sanitizer_mutex.h')
-rw-r--r--libsanitizer/sanitizer_common/sanitizer_mutex.h106
1 files changed, 106 insertions, 0 deletions
diff --git a/libsanitizer/sanitizer_common/sanitizer_mutex.h b/libsanitizer/sanitizer_common/sanitizer_mutex.h
new file mode 100644
index 0000000..a38a49a
--- /dev/null
+++ b/libsanitizer/sanitizer_common/sanitizer_mutex.h
@@ -0,0 +1,106 @@
+//===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SANITIZER_MUTEX_H
+#define SANITIZER_MUTEX_H
+
+#include "sanitizer_atomic.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_libc.h"
+
+namespace __sanitizer {
+
+class StaticSpinMutex {
+ public:
+ void Init() {
+ atomic_store(&state_, 0, memory_order_relaxed);
+ }
+
+ void Lock() {
+ if (atomic_exchange(&state_, 1, memory_order_acquire) == 0)
+ return;
+ LockSlow();
+ }
+
+ void Unlock() {
+ atomic_store(&state_, 0, memory_order_release);
+ }
+
+ private:
+ atomic_uint8_t state_;
+
+ void NOINLINE LockSlow() {
+ for (int i = 0;; i++) {
+ if (i < 10)
+ proc_yield(10);
+ else
+ internal_sched_yield();
+ if (atomic_load(&state_, memory_order_relaxed) == 0
+ && atomic_exchange(&state_, 1, memory_order_acquire) == 0)
+ return;
+ }
+ }
+};
+
+class SpinMutex : public StaticSpinMutex {
+ public:
+ SpinMutex() {
+ Init();
+ }
+
+ private:
+ SpinMutex(const SpinMutex&);
+ void operator=(const SpinMutex&);
+};
+
+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&);
+};
+
+typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
+
+} // namespace __sanitizer
+
+#endif // SANITIZER_MUTEX_H