1 //===-- tsan_mutexset.h -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //
11 // MutexSet holds the set of mutexes currently held by a thread.
12 //===----------------------------------------------------------------------===//
13 #ifndef TSAN_MUTEXSET_H
14 #define TSAN_MUTEXSET_H
15 
16 #include "tsan_defs.h"
17 
18 namespace __tsan {
19 
20 class MutexSet {
21  public:
22   // Holds limited number of mutexes.
23   // The oldest mutexes are discarded on overflow.
24   static const uptr kMaxSize = 16;
25   struct Desc {
26     u64 id;
27     u64 epoch;
28     int count;
29     bool write;
30   };
31 
32   MutexSet();
33   // The 'id' is obtained from SyncVar::GetId().
34   void Add(u64 id, bool write, u64 epoch);
35   void Del(u64 id, bool write);
36   void Remove(u64 id);  // Removes the mutex completely (if it's destroyed).
37   uptr Size() const;
38   Desc Get(uptr i) const;
39 
40   void operator=(const MutexSet &other) {
41     internal_memcpy(this, &other, sizeof(*this));
42   }
43 
44  private:
45 #if !SANITIZER_GO
46   uptr size_;
47   Desc descs_[kMaxSize];
48 #endif
49 
50   void RemovePos(uptr i);
51   MutexSet(const MutexSet&);
52 };
53 
54 // Go does not have mutexes, so do not spend memory and time.
55 // (Go sync.Mutex is actually a semaphore -- can be unlocked
56 // in different goroutine).
57 #if SANITIZER_GO
58 MutexSet::MutexSet() {}
59 void MutexSet::Add(u64 id, bool write, u64 epoch) {}
60 void MutexSet::Del(u64 id, bool write) {}
61 void MutexSet::Remove(u64 id) {}
62 void MutexSet::RemovePos(uptr i) {}
63 uptr MutexSet::Size() const { return 0; }
64 MutexSet::Desc MutexSet::Get(uptr i) const { return Desc(); }
65 #endif
66 
67 }  // namespace __tsan
68 
69 #endif  // TSAN_MUTEXSET_H
70