]>
git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/tsan/tests/unit/tsan_mutex_test.cc
1 //===-- tsan_mutex_test.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_internal_defs.h"
14 #include "sanitizer_common/sanitizer_atomic.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_mutex.h"
17 #include "tsan_mutex.h"
18 #include "gtest/gtest.h"
22 template<typename MutexType
>
25 explicit TestData(MutexType
*mtx
)
27 for (int i
= 0; i
< kSize
; i
++)
34 for (int i
= 0; i
< kSize
; i
++) {
35 CHECK_EQ(data_
[i
], v0
);
43 for (int i
= 0; i
< kSize
; i
++) {
44 CHECK_EQ(data_
[i
], v0
);
49 volatile T data
[kSize
] = {};
50 for (int i
= 0; i
< kSize
; i
++) {
57 typedef GenericScopedLock
<MutexType
> Lock
;
58 static const int kSize
= 64;
61 char pad_
[kCacheLineSize
];
65 const int kThreads
= 8;
66 const int kWriteRate
= 1024;
68 const int kIters
= 16*1024;
70 const int kIters
= 64*1024;
73 template<typename MutexType
>
74 static void *write_mutex_thread(void *param
) {
75 TestData
<MutexType
> *data
= (TestData
<MutexType
>*)param
;
76 for (int i
= 0; i
< kIters
; i
++) {
83 template<typename MutexType
>
84 static void *read_mutex_thread(void *param
) {
85 TestData
<MutexType
> *data
= (TestData
<MutexType
>*)param
;
86 for (int i
= 0; i
< kIters
; i
++) {
87 if ((i
% kWriteRate
) == 0)
97 Mutex
mtx(MutexTypeAnnotations
, StatMtxAnnotations
);
98 TestData
<Mutex
> data(&mtx
);
99 pthread_t threads
[kThreads
];
100 for (int i
= 0; i
< kThreads
; i
++)
101 pthread_create(&threads
[i
], 0, write_mutex_thread
<Mutex
>, &data
);
102 for (int i
= 0; i
< kThreads
; i
++)
103 pthread_join(threads
[i
], 0);
106 TEST(Mutex
, ReadWrite
) {
107 Mutex
mtx(MutexTypeAnnotations
, StatMtxAnnotations
);
108 TestData
<Mutex
> data(&mtx
);
109 pthread_t threads
[kThreads
];
110 for (int i
= 0; i
< kThreads
; i
++)
111 pthread_create(&threads
[i
], 0, read_mutex_thread
<Mutex
>, &data
);
112 for (int i
= 0; i
< kThreads
; i
++)
113 pthread_join(threads
[i
], 0);
116 TEST(Mutex
, SpinWrite
) {
118 TestData
<SpinMutex
> data(&mtx
);
119 pthread_t threads
[kThreads
];
120 for (int i
= 0; i
< kThreads
; i
++)
121 pthread_create(&threads
[i
], 0, write_mutex_thread
<SpinMutex
>, &data
);
122 for (int i
= 0; i
< kThreads
; i
++)
123 pthread_join(threads
[i
], 0);
126 } // namespace __tsan