tsan_sync.h revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1//===-- tsan_sync.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_SYNC_H 14#define TSAN_SYNC_H 15 16#include "sanitizer_common/sanitizer_atomic.h" 17#include "sanitizer_common/sanitizer_common.h" 18#include "sanitizer_common/sanitizer_deadlock_detector_interface.h" 19#include "tsan_clock.h" 20#include "tsan_defs.h" 21#include "tsan_mutex.h" 22 23namespace __tsan { 24 25class StackTrace { 26 public: 27 StackTrace(); 28 // Initialized the object in "static mode", 29 // in this mode it never calls malloc/free but uses the provided buffer. 30 StackTrace(uptr *buf, uptr cnt); 31 ~StackTrace(); 32 void Reset(); 33 34 void Init(const uptr *pcs, uptr cnt); 35 void ObtainCurrent(ThreadState *thr, uptr toppc); 36 bool IsEmpty() const; 37 uptr Size() const; 38 uptr Get(uptr i) const; 39 const uptr *Begin() const; 40 void CopyFrom(const StackTrace& other); 41 42 private: 43 uptr n_; 44 uptr *s_; 45 const uptr c_; 46 47 StackTrace(const StackTrace&); 48 void operator = (const StackTrace&); 49}; 50 51struct SyncVar { 52 explicit SyncVar(uptr addr, u64 uid); 53 54 static const int kInvalidTid = -1; 55 56 Mutex mtx; 57 uptr addr; 58 const u64 uid; // Globally unique id. 59 u32 creation_stack_id; 60 int owner_tid; // Set only by exclusive owners. 61 u64 last_lock; 62 int recursion; 63 bool is_rw; 64 bool is_recursive; 65 bool is_broken; 66 bool is_linker_init; 67 SyncVar *next; // In SyncTab hashtable. 68 DDMutex dd; 69 SyncClock read_clock; // Used for rw mutexes only. 70 // The clock is placed last, so that it is situated on a different cache line 71 // with the mtx. This reduces contention for hot sync objects. 72 SyncClock clock; 73 74 u64 GetId() const { 75 // 47 lsb is addr, then 14 bits is low part of uid, then 3 zero bits. 76 return GetLsb((u64)addr | (uid << 47), 61); 77 } 78 bool CheckId(u64 uid) const { 79 CHECK_EQ(uid, GetLsb(uid, 14)); 80 return GetLsb(this->uid, 14) == uid; 81 } 82 static uptr SplitId(u64 id, u64 *uid) { 83 *uid = id >> 47; 84 return (uptr)GetLsb(id, 47); 85 } 86}; 87 88class SyncTab { 89 public: 90 SyncTab(); 91 ~SyncTab(); 92 93 SyncVar* GetOrCreateAndLock(ThreadState *thr, uptr pc, 94 uptr addr, bool write_lock); 95 SyncVar* GetIfExistsAndLock(uptr addr, bool write_lock); 96 97 // If the SyncVar does not exist, returns 0. 98 SyncVar* GetAndRemove(ThreadState *thr, uptr pc, uptr addr); 99 100 SyncVar* Create(ThreadState *thr, uptr pc, uptr addr); 101 102 private: 103 struct Part { 104 Mutex mtx; 105 SyncVar *val; 106 char pad[kCacheLineSize - sizeof(Mutex) - sizeof(SyncVar*)]; // NOLINT 107 Part(); 108 }; 109 110 // FIXME: Implement something more sane. 111 static const int kPartCount = 1009; 112 Part tab_[kPartCount]; 113 atomic_uint64_t uid_gen_; 114 115 int PartIdx(uptr addr); 116 117 SyncVar* GetAndLock(ThreadState *thr, uptr pc, 118 uptr addr, bool write_lock, bool create); 119 120 SyncTab(const SyncTab&); // Not implemented. 121 void operator = (const SyncTab&); // Not implemented. 122}; 123 124} // namespace __tsan 125 126#endif // TSAN_SYNC_H 127