2014-05-30 22:08:51 +08:00
|
|
|
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
|
2015-01-21 21:50:02 +08:00
|
|
|
#include "test.h"
|
2012-05-10 22:18:22 +08:00
|
|
|
|
|
|
|
pthread_mutex_t Mtx;
|
|
|
|
int Global;
|
|
|
|
|
2016-02-26 19:09:32 +08:00
|
|
|
void *Thread2(void *x) {
|
|
|
|
barrier_wait(&barrier);
|
|
|
|
// CHECK: WARNING: ThreadSanitizer: data race
|
|
|
|
// CHECK-NEXT: Atomic read of size 1 at {{.*}} by thread T2:
|
|
|
|
// CHECK-NEXT: #0 pthread_mutex_lock
|
|
|
|
// CHECK-NEXT: #1 Thread2{{.*}} {{.*}}race_on_mutex.c:[[@LINE+1]]{{(:3)?}} ({{.*}})
|
2012-05-10 22:18:22 +08:00
|
|
|
pthread_mutex_lock(&Mtx);
|
2016-02-26 19:09:32 +08:00
|
|
|
Global = 43;
|
2012-05-10 22:18:22 +08:00
|
|
|
pthread_mutex_unlock(&Mtx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:09:32 +08:00
|
|
|
void *Thread1(void *x) {
|
2016-03-18 13:41:20 +08:00
|
|
|
// CHECK: Previous write of size {{[0-9]+}} at {{.*}} by thread T1:
|
|
|
|
// CHECK: #{{[0-9]+}} {{.*}}pthread_mutex_init {{.*}} ({{.*}})
|
|
|
|
// CHECK-NEXT: #{{[0-9]+}} Thread1{{.*}} {{.*}}race_on_mutex.c:[[@LINE+1]]{{(:3)?}} ({{.*}})
|
2016-02-26 19:09:32 +08:00
|
|
|
pthread_mutex_init(&Mtx, 0);
|
2012-05-10 22:18:22 +08:00
|
|
|
pthread_mutex_lock(&Mtx);
|
2016-02-26 19:09:32 +08:00
|
|
|
Global = 42;
|
2012-05-10 22:18:22 +08:00
|
|
|
pthread_mutex_unlock(&Mtx);
|
2016-02-26 19:09:32 +08:00
|
|
|
barrier_wait(&barrier);
|
2012-05-10 22:18:22 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2015-01-21 21:50:02 +08:00
|
|
|
barrier_init(&barrier, 2);
|
2012-05-10 22:18:22 +08:00
|
|
|
pthread_t t[2];
|
|
|
|
pthread_create(&t[0], NULL, Thread1, NULL);
|
|
|
|
pthread_create(&t[1], NULL, Thread2, NULL);
|
|
|
|
pthread_join(t[0], NULL);
|
|
|
|
pthread_join(t[1], NULL);
|
|
|
|
pthread_mutex_destroy(&Mtx);
|
|
|
|
return 0;
|
|
|
|
}
|