]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/tsan/tsan_mutexset.cpp
1 //===-- tsan_mutexset.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
12 #include "tsan_mutexset.h"
17 const uptr
MutexSet::kMaxSize
;
19 MutexSet::MutexSet() {
21 internal_memset(&descs_
, 0, sizeof(descs_
));
24 void MutexSet::Add(u64 id
, bool write
, u64 epoch
) {
25 // Look up existing mutex with the same id.
26 for (uptr i
= 0; i
< size_
; i
++) {
27 if (descs_
[i
].id
== id
) {
29 descs_
[i
].epoch
= epoch
;
33 // On overflow, find the oldest mutex and drop it.
34 if (size_
== kMaxSize
) {
35 u64 minepoch
= (u64
)-1;
37 for (uptr i
= 0; i
< size_
; i
++) {
38 if (descs_
[i
].epoch
< minepoch
) {
39 minepoch
= descs_
[i
].epoch
;
44 CHECK_EQ(size_
, kMaxSize
- 1);
46 // Add new mutex descriptor.
47 descs_
[size_
].id
= id
;
48 descs_
[size_
].write
= write
;
49 descs_
[size_
].epoch
= epoch
;
50 descs_
[size_
].count
= 1;
54 void MutexSet::Del(u64 id
, bool write
) {
55 for (uptr i
= 0; i
< size_
; i
++) {
56 if (descs_
[i
].id
== id
) {
57 if (--descs_
[i
].count
== 0)
64 void MutexSet::Remove(u64 id
) {
65 for (uptr i
= 0; i
< size_
; i
++) {
66 if (descs_
[i
].id
== id
) {
73 void MutexSet::RemovePos(uptr i
) {
75 descs_
[i
] = descs_
[size_
- 1];
79 uptr
MutexSet::Size() const {
83 MutexSet::Desc
MutexSet::Get(uptr i
) const {