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