Dmitry Vyukov | 7a9fa7d | 2012-06-29 17:10:08 +0000 | [diff] [blame^] | 1 | //===-- sanitizer_mutex.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 | #ifndef SANITIZER_MUTEX_H |
| 11 | #define SANITIZER_MUTEX_H |
| 12 | |
| 13 | #include "sanitizer_internal_defs.h" |
| 14 | #include "sanitizer_atomic.h" |
| 15 | |
| 16 | namespace __sanitizer { |
| 17 | |
| 18 | template<typename MutexType> |
| 19 | class GenericScopedLock { |
| 20 | public: |
| 21 | explicit GenericScopedLock(MutexType *mu) |
| 22 | : mu_(mu) { |
| 23 | mu_->Lock(); |
| 24 | } |
| 25 | |
| 26 | ~GenericScopedLock() { |
| 27 | mu_->Unlock(); |
| 28 | } |
| 29 | |
| 30 | private: |
| 31 | MutexType *mu_; |
| 32 | |
| 33 | GenericScopedLock(const GenericScopedLock&); |
| 34 | void operator=(const GenericScopedLock&); |
| 35 | }; |
| 36 | |
| 37 | template<typename MutexType> |
| 38 | class GenericScopedReadLock { |
| 39 | public: |
| 40 | explicit GenericScopedReadLock(MutexType *mu) |
| 41 | : mu_(mu) { |
| 42 | mu_->ReadLock(); |
| 43 | } |
| 44 | |
| 45 | ~GenericScopedReadLock() { |
| 46 | mu_->ReadUnlock(); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | MutexType *mu_; |
| 51 | |
| 52 | GenericScopedReadLock(const GenericScopedReadLock&); |
| 53 | void operator=(const GenericScopedReadLock&); |
| 54 | }; |
| 55 | |
| 56 | } // namespace __sanitizer |
| 57 | |
| 58 | #endif // SANITIZER_MUTEX_H |