Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 1 | //===-- asan_lock.h ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // A wrapper for a simple lock. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifndef ASAN_LOCK_H |
| 15 | #define ASAN_LOCK_H |
| 16 | |
| 17 | #include "asan_internal.h" |
| 18 | |
| 19 | // The locks in ASan are global objects and they are never destroyed to avoid |
| 20 | // at-exit races (that is, a lock is being used by other threads while the main |
| 21 | // thread is doing atexit destructors). |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 | [diff] [blame] | 22 | // We define the class using opaque storage to avoid including system headers. |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 23 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 24 | namespace __asan { |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 | [diff] [blame] | 25 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 26 | class AsanLock { |
| 27 | public: |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 | [diff] [blame] | 28 | explicit AsanLock(LinkerInitialized); |
| 29 | void Lock(); |
| 30 | void Unlock(); |
| 31 | bool IsLocked() { return owner_ != 0; } |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 32 | private: |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 | [diff] [blame] | 33 | uintptr_t opaque_storage_[10]; |
| 34 | uintptr_t owner_; // for debugging and for malloc_introspection_t interface |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 35 | }; |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 36 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 37 | class ScopedLock { |
| 38 | public: |
| 39 | explicit ScopedLock(AsanLock *mu) : mu_(mu) { |
| 40 | mu_->Lock(); |
| 41 | } |
| 42 | ~ScopedLock() { |
| 43 | mu_->Unlock(); |
| 44 | } |
| 45 | private: |
| 46 | AsanLock *mu_; |
| 47 | }; |
| 48 | |
| 49 | } // namespace __asan |
| 50 | |
| 51 | #endif // ASAN_LOCK_H |