1 //===-- tsan_mutexset.cc --------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
11 #include "tsan_mutexset.h"
16 const uptr
MutexSet::kMaxSize
;
18 MutexSet::MutexSet() {
20 internal_memset(&descs_
, 0, sizeof(descs_
));
23 void MutexSet::Add(u64 id
, bool write
, u64 epoch
) {
24 // Look up existing mutex with the same id.
25 for (uptr i
= 0; i
< size_
; i
++) {
26 if (descs_
[i
].id
== id
) {
28 descs_
[i
].epoch
= epoch
;
32 // On overflow, find the oldest mutex and drop it.
33 if (size_
== kMaxSize
) {
34 u64 minepoch
= (u64
)-1;
36 for (uptr i
= 0; i
< size_
; i
++) {
37 if (descs_
[i
].epoch
< minepoch
) {
38 minepoch
= descs_
[i
].epoch
;
43 CHECK_EQ(size_
, kMaxSize
- 1);
45 // Add new mutex descriptor.
46 descs_
[size_
].id
= id
;
47 descs_
[size_
].write
= write
;
48 descs_
[size_
].epoch
= epoch
;
49 descs_
[size_
].count
= 1;
53 void MutexSet::Del(u64 id
, bool write
) {
54 for (uptr i
= 0; i
< size_
; i
++) {
55 if (descs_
[i
].id
== id
) {
56 if (--descs_
[i
].count
== 0)
63 void MutexSet::Remove(u64 id
) {
64 for (uptr i
= 0; i
< size_
; i
++) {
65 if (descs_
[i
].id
== id
) {
72 void MutexSet::RemovePos(uptr i
) {
74 descs_
[i
] = descs_
[size_
- 1];
78 uptr
MutexSet::Size() const {
82 MutexSet::Desc
MutexSet::Get(uptr i
) const {