blob: 34d3e0b2132f9e270a03123a164917c9c8d600e9 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- 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
Dmitry Vyukov6fa46f72012-06-29 16:58:33 +000016#include "sanitizer_common/sanitizer_atomic.h"
17#include "sanitizer_common/sanitizer_common.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000018#include "tsan_clock.h"
19#include "tsan_defs.h"
20#include "tsan_mutex.h"
21
22namespace __tsan {
23
24class SlabCache;
25
26class StackTrace {
27 public:
28 StackTrace();
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +000029 // Initialized the object in "static mode",
30 // in this mode it never calls malloc/free but uses the provided buffer.
31 StackTrace(uptr *buf, uptr cnt);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000032 ~StackTrace();
33 void Reset();
34
35 void Init(const uptr *pcs, uptr cnt);
36 void ObtainCurrent(ThreadState *thr, uptr toppc);
37 bool IsEmpty() const;
38 uptr Size() const;
39 uptr Get(uptr i) const;
40 const uptr *Begin() const;
41 void CopyFrom(const StackTrace& other);
42
43 private:
44 uptr n_;
45 uptr *s_;
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +000046 const uptr c_;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000047
48 StackTrace(const StackTrace&);
49 void operator = (const StackTrace&);
50};
51
52struct SyncVar {
53 explicit SyncVar(uptr addr);
54
55 static const int kInvalidTid = -1;
56
57 Mutex mtx;
58 const uptr addr;
59 SyncClock clock;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000060 SyncClock read_clock; // Used for rw mutexes only.
Dmitry Vyukov15710c92012-05-22 11:33:03 +000061 StackTrace creation_stack;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000062 int owner_tid; // Set only by exclusive owners.
63 int recursion;
64 bool is_rw;
65 bool is_recursive;
66 bool is_broken;
67 SyncVar *next; // In SyncTab hashtable.
Dmitry Vyukov15710c92012-05-22 11:33:03 +000068
69 uptr GetMemoryConsumption();
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000070};
71
72class SyncTab {
73 public:
74 SyncTab();
75 ~SyncTab();
76
77 // If the SyncVar does not exist yet, it is created.
78 SyncVar* GetAndLock(ThreadState *thr, uptr pc,
79 uptr addr, bool write_lock);
80
81 // If the SyncVar does not exist, returns 0.
82 SyncVar* GetAndRemove(ThreadState *thr, uptr pc, uptr addr);
83
Dmitry Vyukov15710c92012-05-22 11:33:03 +000084 uptr GetMemoryConsumption(uptr *nsync);
85
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000086 private:
87 struct Part {
88 Mutex mtx;
89 SyncVar *val;
90 char pad[kCacheLineSize - sizeof(Mutex) - sizeof(SyncVar*)]; // NOLINT
91 Part();
92 };
93
94 // FIXME: Implement something more sane.
95 static const int kPartCount = 1009;
96 Part tab_[kPartCount];
97
98 int PartIdx(uptr addr);
99
100 SyncTab(const SyncTab&); // Not implemented.
101 void operator = (const SyncTab&); // Not implemented.
102};
103
104} // namespace __tsan
105
106#endif // TSAN_SYNC_H