2012-05-10 21:48:04 +08:00
|
|
|
//===-- tsan_mutex.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef TSAN_MUTEX_H
|
|
|
|
#define TSAN_MUTEX_H
|
|
|
|
|
2012-06-30 00:58:33 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
2012-06-30 01:10:08 +08:00
|
|
|
#include "sanitizer_common/sanitizer_mutex.h"
|
2012-05-10 21:48:04 +08:00
|
|
|
#include "tsan_defs.h"
|
|
|
|
|
|
|
|
namespace __tsan {
|
|
|
|
|
|
|
|
enum MutexType {
|
|
|
|
MutexTypeInvalid,
|
|
|
|
MutexTypeTrace,
|
|
|
|
MutexTypeThreads,
|
|
|
|
MutexTypeReport,
|
|
|
|
MutexTypeSyncVar,
|
|
|
|
MutexTypeSyncTab,
|
|
|
|
MutexTypeSlab,
|
|
|
|
MutexTypeAnnotations,
|
|
|
|
MutexTypeAtExit,
|
2012-12-06 20:16:15 +08:00
|
|
|
MutexTypeMBlock,
|
2012-12-21 19:30:14 +08:00
|
|
|
MutexTypeJavaMBlock,
|
2014-02-27 17:02:58 +08:00
|
|
|
MutexTypeDDetector,
|
2015-09-03 19:20:46 +08:00
|
|
|
MutexTypeFired,
|
|
|
|
MutexTypeRacy,
|
2016-05-07 03:35:22 +08:00
|
|
|
MutexTypeGlobalProc,
|
2012-05-10 21:48:04 +08:00
|
|
|
|
|
|
|
// This must be the last.
|
2012-09-13 19:54:41 +08:00
|
|
|
MutexTypeCount
|
2012-05-10 21:48:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Mutex {
|
|
|
|
public:
|
|
|
|
explicit Mutex(MutexType type, StatType stat_type);
|
|
|
|
~Mutex();
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
|
|
|
|
|
|
|
void ReadLock();
|
|
|
|
void ReadUnlock();
|
|
|
|
|
2012-08-30 21:02:30 +08:00
|
|
|
void CheckLocked();
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
private:
|
|
|
|
atomic_uintptr_t state_;
|
2015-01-03 12:29:12 +08:00
|
|
|
#if SANITIZER_DEBUG
|
2012-05-10 21:48:04 +08:00
|
|
|
MutexType type_;
|
|
|
|
#endif
|
|
|
|
#if TSAN_COLLECT_STATS
|
|
|
|
StatType stat_type_;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Mutex(const Mutex&);
|
|
|
|
void operator = (const Mutex&);
|
|
|
|
};
|
|
|
|
|
2012-06-30 01:10:08 +08:00
|
|
|
typedef GenericScopedLock<Mutex> Lock;
|
|
|
|
typedef GenericScopedReadLock<Mutex> ReadLock;
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2014-02-14 20:20:42 +08:00
|
|
|
class InternalDeadlockDetector {
|
2012-05-10 21:48:04 +08:00
|
|
|
public:
|
2014-02-14 20:20:42 +08:00
|
|
|
InternalDeadlockDetector();
|
2012-05-10 21:48:04 +08:00
|
|
|
void Lock(MutexType t);
|
|
|
|
void Unlock(MutexType t);
|
2014-05-29 21:50:54 +08:00
|
|
|
void CheckNoLocks();
|
2012-05-10 21:48:04 +08:00
|
|
|
private:
|
|
|
|
u64 seq_;
|
|
|
|
u64 locked_[MutexTypeCount];
|
|
|
|
};
|
|
|
|
|
|
|
|
void InitializeMutex();
|
|
|
|
|
2014-05-29 21:50:54 +08:00
|
|
|
// Checks that the current thread does not hold any runtime locks
|
|
|
|
// (e.g. when returning from an interceptor).
|
|
|
|
void CheckNoLocks(ThreadState *thr);
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
} // namespace __tsan
|
|
|
|
|
|
|
|
#endif // TSAN_MUTEX_H
|