47 lines · cpp
1// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t2// RUN: %env_tsan_opts=report_destroy_locked=0 %run %t 2>&1 | FileCheck %s3#include "custom_mutex.h"4 5// Regression test for a bug.6// Thr1 destroys a locked mutex, previously such mutex was not removed from7// sync map and as the result subsequent uses of a mutex located at the same8// address caused false race reports.9 10Mutex mu(false, __tsan_mutex_write_reentrant);11long data;12 13void *thr1(void *arg) {14 mu.Lock();15 mu.~Mutex();16 new(&mu) Mutex(true, __tsan_mutex_write_reentrant);17 return 0;18}19 20void *thr2(void *arg) {21 barrier_wait(&barrier);22 mu.Lock();23 data++;24 mu.Unlock();25 return 0;26}27 28int main() {29 barrier_init(&barrier, 2);30 pthread_t th;31 pthread_create(&th, 0, thr1, 0);32 pthread_join(th, 0);33 34 barrier_init(&barrier, 2);35 pthread_create(&th, 0, thr2, 0);36 mu.Lock();37 data++;38 mu.Unlock();39 barrier_wait(&barrier);40 pthread_join(th, 0);41 fprintf(stderr, "DONE\n");42 return 0;43}44 45// CHECK-NOT: WARNING: ThreadSanitizer: data race46// CHECK: DONE47