40 lines · cpp
1// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s2#include "custom_mutex.h"3 4// Test that failed TryLock does not induce parasitic synchronization.5 6Mutex mu(true, 0);7long data;8 9void *thr(void *arg) {10 mu.Lock();11 data++;12 mu.Unlock();13 mu.Lock();14 barrier_wait(&barrier);15 barrier_wait(&barrier);16 mu.Unlock();17 return 0;18}19 20int main() {21 barrier_init(&barrier, 2);22 pthread_t th;23 pthread_create(&th, 0, thr, 0);24 barrier_wait(&barrier);25 if (mu.TryLock()) {26 fprintf(stderr, "TryLock succeeded, should not\n");27 exit(0);28 }29 data++;30 barrier_wait(&barrier);31 pthread_join(th, 0);32 fprintf(stderr, "DONE\n");33 return 0;34}35 36// CHECK: ThreadSanitizer: data race37// CHECK-NEXT: Write of size 8 at {{.*}} by main thread:38// CHECK-NEXT: #0 main {{.*}}custom_mutex1.cpp:2939// CHECK: DONE40