Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_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 | // This file is a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef TSAN_MUTEX_H |
| 14 | #define TSAN_MUTEX_H |
| 15 | |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_atomic.h" |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_mutex.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 18 | #include "tsan_defs.h" |
| 19 | |
| 20 | namespace __tsan { |
| 21 | |
| 22 | enum MutexType { |
| 23 | MutexTypeInvalid, |
| 24 | MutexTypeTrace, |
| 25 | MutexTypeThreads, |
| 26 | MutexTypeReport, |
| 27 | MutexTypeSyncVar, |
| 28 | MutexTypeSyncTab, |
| 29 | MutexTypeSlab, |
| 30 | MutexTypeAnnotations, |
| 31 | MutexTypeAtExit, |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 32 | MutexTypeMBlock, |
Dmitry Vyukov | f4e4f93 | 2012-12-21 11:30:14 +0000 | [diff] [blame] | 33 | MutexTypeJavaMBlock, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 34 | MutexTypeDDetector, |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 35 | |
| 36 | // This must be the last. |
Alexey Samsonov | 2135d8a | 2012-09-13 11:54:41 +0000 | [diff] [blame] | 37 | MutexTypeCount |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | class Mutex { |
| 41 | public: |
| 42 | explicit Mutex(MutexType type, StatType stat_type); |
| 43 | ~Mutex(); |
| 44 | |
| 45 | void Lock(); |
| 46 | void Unlock(); |
| 47 | |
| 48 | void ReadLock(); |
| 49 | void ReadUnlock(); |
| 50 | |
Dmitry Vyukov | ff35f1d | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 51 | void CheckLocked(); |
| 52 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 53 | private: |
| 54 | atomic_uintptr_t state_; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 55 | #if SANITIZER_DEBUG |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 56 | MutexType type_; |
| 57 | #endif |
| 58 | #if TSAN_COLLECT_STATS |
| 59 | StatType stat_type_; |
| 60 | #endif |
| 61 | |
| 62 | Mutex(const Mutex&); |
| 63 | void operator = (const Mutex&); |
| 64 | }; |
| 65 | |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 66 | typedef GenericScopedLock<Mutex> Lock; |
| 67 | typedef GenericScopedReadLock<Mutex> ReadLock; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 68 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 69 | class InternalDeadlockDetector { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 70 | public: |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 71 | InternalDeadlockDetector(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 72 | void Lock(MutexType t); |
| 73 | void Unlock(MutexType t); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 74 | void CheckNoLocks(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 75 | private: |
| 76 | u64 seq_; |
| 77 | u64 locked_[MutexTypeCount]; |
| 78 | }; |
| 79 | |
| 80 | void InitializeMutex(); |
| 81 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 82 | // Checks that the current thread does not hold any runtime locks |
| 83 | // (e.g. when returning from an interceptor). |
| 84 | void CheckNoLocks(ThreadState *thr); |
| 85 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 86 | } // namespace __tsan |
| 87 | |
| 88 | #endif // TSAN_MUTEX_H |