• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "test.h"
3 
4 pthread_mutex_t Mtx;
5 int Global;
6 
Thread2(void * x)7 void *Thread2(void *x) {
8   barrier_wait(&barrier);
9 // CHECK:      WARNING: ThreadSanitizer: data race
10 // CHECK-NEXT:   Atomic read of size 1 at {{.*}} by thread T2:
11 // CHECK-NEXT:     #0 pthread_mutex_lock
12 // CHECK-NEXT:     #1 Thread2{{.*}} {{.*}}race_on_mutex.c:[[@LINE+1]]{{(:3)?}} ({{.*}})
13   pthread_mutex_lock(&Mtx);
14   Global = 43;
15   pthread_mutex_unlock(&Mtx);
16   return NULL;
17 }
18 
Thread1(void * x)19 void *Thread1(void *x) {
20 // CHECK:        Previous write of size {{[0-9]+}} at {{.*}} by thread T1:
21 // CHECK:          #{{[0-9]+}} {{.*}}pthread_mutex_init {{.*}} ({{.*}})
22 // CHECK-NEXT:     #{{[0-9]+}} Thread1{{.*}} {{.*}}race_on_mutex.c:[[@LINE+1]]{{(:3)?}} ({{.*}})
23   pthread_mutex_init(&Mtx, 0);
24   pthread_mutex_lock(&Mtx);
25   Global = 42;
26   pthread_mutex_unlock(&Mtx);
27   barrier_wait(&barrier);
28   return NULL;
29 }
30 
main()31 int main() {
32   barrier_init(&barrier, 2);
33   pthread_t t[2];
34   pthread_create(&t[0], NULL, Thread1, NULL);
35   pthread_create(&t[1], NULL, Thread2, NULL);
36   pthread_join(t[0], NULL);
37   pthread_join(t[1], NULL);
38   pthread_mutex_destroy(&Mtx);
39   return 0;
40 }
41