1 // RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2 #include "custom_mutex.h"
3 
4 #include <type_traits>
5 
6 // Test that we detect the destruction of an in-use mutex when the
7 // thread annotations don't otherwise disable the check.
8 
main()9 int main() {
10   std::aligned_storage<sizeof(Mutex), alignof(Mutex)>::type mu1_store;
11   Mutex* mu1 = reinterpret_cast<Mutex*>(&mu1_store);
12   new(&mu1_store) Mutex(false, 0);
13   mu1->Lock();
14   mu1->~Mutex();
15   mu1->Unlock();
16 
17   std::aligned_storage<sizeof(Mutex), alignof(Mutex)>::type mu2_store;
18   Mutex* mu2 = reinterpret_cast<Mutex*>(&mu2_store);
19   new(&mu2_store)
20       Mutex(false, __tsan_mutex_not_static, __tsan_mutex_not_static);
21   mu2->Lock();
22   mu2->~Mutex();
23   mu2->Unlock();
24 
25   fprintf(stderr, "DONE\n");
26   return 0;
27 }
28 
29 // CHECK: WARNING: ThreadSanitizer: destroy of a locked mutex
30 // CHECK:   main {{.*}}custom_mutex5.cpp:14
31 // CHECK: WARNING: ThreadSanitizer: destroy of a locked mutex
32 // CHECK:   main {{.*}}custom_mutex5.cpp:22
33 // CHECK: DONE
34