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, |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 35 | MutexTypeFired, |
| 36 | MutexTypeRacy, |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 37 | |
| 38 | // This must be the last. |
Alexey Samsonov | 2135d8a | 2012-09-13 11:54:41 +0000 | [diff] [blame] | 39 | MutexTypeCount |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class Mutex { |
| 43 | public: |
| 44 | explicit Mutex(MutexType type, StatType stat_type); |
| 45 | ~Mutex(); |
| 46 | |
| 47 | void Lock(); |
| 48 | void Unlock(); |
| 49 | |
| 50 | void ReadLock(); |
| 51 | void ReadUnlock(); |
| 52 | |
Dmitry Vyukov | ff35f1d | 2012-08-30 13:02:30 +0000 | [diff] [blame] | 53 | void CheckLocked(); |
| 54 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 55 | private: |
| 56 | atomic_uintptr_t state_; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 57 | #if SANITIZER_DEBUG |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | MutexType type_; |
| 59 | #endif |
| 60 | #if TSAN_COLLECT_STATS |
| 61 | StatType stat_type_; |
| 62 | #endif |
| 63 | |
| 64 | Mutex(const Mutex&); |
| 65 | void operator = (const Mutex&); |
| 66 | }; |
| 67 | |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 68 | typedef GenericScopedLock<Mutex> Lock; |
| 69 | typedef GenericScopedReadLock<Mutex> ReadLock; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 70 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 71 | class InternalDeadlockDetector { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 72 | public: |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 73 | InternalDeadlockDetector(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 74 | void Lock(MutexType t); |
| 75 | void Unlock(MutexType t); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 76 | void CheckNoLocks(); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 77 | private: |
| 78 | u64 seq_; |
| 79 | u64 locked_[MutexTypeCount]; |
| 80 | }; |
| 81 | |
| 82 | void InitializeMutex(); |
| 83 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 84 | // Checks that the current thread does not hold any runtime locks |
| 85 | // (e.g. when returning from an interceptor). |
| 86 | void CheckNoLocks(ThreadState *thr); |
| 87 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 88 | } // namespace __tsan |
| 89 | |
| 90 | #endif // TSAN_MUTEX_H |