Home | History | Annotate | Download | only in rtl
      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 
     16 #include "sanitizer_common/sanitizer_atomic.h"
     17 #include "sanitizer_common/sanitizer_mutex.h"
     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,
     32 
     33   // This must be the last.
     34   MutexTypeCount,
     35 };
     36 
     37 class Mutex {
     38  public:
     39   explicit Mutex(MutexType type, StatType stat_type);
     40   ~Mutex();
     41 
     42   void Lock();
     43   void Unlock();
     44 
     45   void ReadLock();
     46   void ReadUnlock();
     47 
     48   void CheckLocked();
     49 
     50  private:
     51   atomic_uintptr_t state_;
     52 #if TSAN_DEBUG
     53   MutexType type_;
     54 #endif
     55 #if TSAN_COLLECT_STATS
     56   StatType stat_type_;
     57 #endif
     58 
     59   Mutex(const Mutex&);
     60   void operator = (const Mutex&);
     61 };
     62 
     63 typedef GenericScopedLock<Mutex> Lock;
     64 typedef GenericScopedReadLock<Mutex> ReadLock;
     65 
     66 class DeadlockDetector {
     67  public:
     68   DeadlockDetector();
     69   void Lock(MutexType t);
     70   void Unlock(MutexType t);
     71  private:
     72   u64 seq_;
     73   u64 locked_[MutexTypeCount];
     74 };
     75 
     76 void InitializeMutex();
     77 
     78 }  // namespace __tsan
     79 
     80 #endif  // TSAN_MUTEX_H
     81