Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 1 | // RUN: %clangxx_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 | int Global; |
| 5 | pthread_mutex_t mtx1; |
| 6 | pthread_mutex_t mtx2; |
| 7 | |
| 8 | void *Thread1(void *x) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 9 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 10 | pthread_mutex_lock(&mtx1); |
| 11 | Global++; |
| 12 | pthread_mutex_unlock(&mtx1); |
| 13 | return NULL; |
| 14 | } |
| 15 | |
| 16 | void *Thread2(void *x) { |
| 17 | pthread_mutex_lock(&mtx2); |
| 18 | Global--; |
| 19 | pthread_mutex_unlock(&mtx2); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 20 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | // CHECK: WARNING: ThreadSanitizer: data race |
| 27 | // CHECK: Write of size 4 at {{.*}} by thread T1 |
| 28 | // CHECK: (mutexes: write [[M1:M[0-9]+]]): |
| 29 | // CHECK: Previous write of size 4 at {{.*}} by thread T2 |
| 30 | // CHECK: (mutexes: write [[M2:M[0-9]+]]): |
| 31 | // CHECK: Mutex [[M1]] (0x{{.*}}) created at: |
| 32 | // CHECK: #0 pthread_mutex_init |
| 33 | // CHECK: #1 main {{.*}}/mutexset5.cc:[[@LINE+4]] |
| 34 | // CHECK: Mutex [[M2]] (0x{{.*}}) created at: |
| 35 | // CHECK: #0 pthread_mutex_init |
| 36 | // CHECK: #1 main {{.*}}/mutexset5.cc:[[@LINE+5]] |
| 37 | pthread_mutex_init(&mtx1, 0); |
| 38 | pthread_mutex_init(&mtx2, 0); |
| 39 | pthread_t t[2]; |
| 40 | pthread_create(&t[0], NULL, Thread1, NULL); |
| 41 | pthread_create(&t[1], NULL, Thread2, NULL); |
| 42 | pthread_join(t[0], NULL); |
| 43 | pthread_join(t[1], NULL); |
| 44 | pthread_mutex_destroy(&mtx1); |
| 45 | pthread_mutex_destroy(&mtx2); |
| 46 | } |