tsan_mutex.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===-- tsan_mutex.h --------------------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of ThreadSanitizer (TSan), a race detector.
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #ifndef TSAN_MUTEX_H
  12. #define TSAN_MUTEX_H
  13. #include "sanitizer_common/sanitizer_atomic.h"
  14. #include "sanitizer_common/sanitizer_mutex.h"
  15. #include "tsan_defs.h"
  16. namespace __tsan {
  17. enum MutexType {
  18. MutexTypeInvalid,
  19. MutexTypeTrace,
  20. MutexTypeThreads,
  21. MutexTypeReport,
  22. MutexTypeSyncVar,
  23. MutexTypeSyncTab,
  24. MutexTypeSlab,
  25. MutexTypeAnnotations,
  26. MutexTypeAtExit,
  27. MutexTypeMBlock,
  28. MutexTypeJavaMBlock,
  29. MutexTypeDDetector,
  30. // This must be the last.
  31. MutexTypeCount
  32. };
  33. class Mutex {
  34. public:
  35. explicit Mutex(MutexType type, StatType stat_type);
  36. ~Mutex();
  37. void Lock();
  38. void Unlock();
  39. void ReadLock();
  40. void ReadUnlock();
  41. void CheckLocked();
  42. private:
  43. atomic_uintptr_t state_;
  44. #if TSAN_DEBUG
  45. MutexType type_;
  46. #endif
  47. #if TSAN_COLLECT_STATS
  48. StatType stat_type_;
  49. #endif
  50. Mutex(const Mutex&);
  51. void operator = (const Mutex&);
  52. };
  53. typedef GenericScopedLock<Mutex> Lock;
  54. typedef GenericScopedReadLock<Mutex> ReadLock;
  55. class InternalDeadlockDetector {
  56. public:
  57. InternalDeadlockDetector();
  58. void Lock(MutexType t);
  59. void Unlock(MutexType t);
  60. void CheckNoLocks();
  61. private:
  62. u64 seq_;
  63. u64 locked_[MutexTypeCount];
  64. };
  65. void InitializeMutex();
  66. // Checks that the current thread does not hold any runtime locks
  67. // (e.g. when returning from an interceptor).
  68. void CheckNoLocks(ThreadState *thr);
  69. } // namespace __tsan
  70. #endif // TSAN_MUTEX_H