34 lines · cpp
1// RUN: %clangxx_tsan -O1 --std=c++17 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s2#include "custom_mutex.h"3 4#include <cstddef>5 6// Test that we detect the destruction of an in-use mutex when the7// thread annotations don't otherwise disable the check.8 9int main() {10 alignas(Mutex) std::byte mu1_store[sizeof(Mutex)];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 alignas(Mutex) std::byte mu2_store[sizeof(Mutex)];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 mutex30// CHECK: main {{.*}}custom_mutex5.cpp:1431// CHECK: WARNING: ThreadSanitizer: destroy of a locked mutex32// CHECK: main {{.*}}custom_mutex5.cpp:2233// CHECK: DONE34