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