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_spinlock_t mtx2; |
| 7 | pthread_rwlock_t mtx3; |
| 8 | |
| 9 | void *Thread1(void *x) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 10 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 11 | pthread_mutex_lock(&mtx1); |
| 12 | Global++; |
| 13 | pthread_mutex_unlock(&mtx1); |
| 14 | return NULL; |
| 15 | } |
| 16 | |
| 17 | void *Thread2(void *x) { |
| 18 | pthread_mutex_lock(&mtx1); |
| 19 | pthread_mutex_unlock(&mtx1); |
| 20 | pthread_spin_lock(&mtx2); |
| 21 | pthread_rwlock_rdlock(&mtx3); |
| 22 | Global--; |
| 23 | pthread_spin_unlock(&mtx2); |
| 24 | pthread_rwlock_unlock(&mtx3); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 25 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 26 | return NULL; |
| 27 | } |
| 28 | |
| 29 | int main() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 30 | barrier_init(&barrier, 2); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 31 | // CHECK: WARNING: ThreadSanitizer: data race |
| 32 | // CHECK: Write of size 4 at {{.*}} by thread T1 |
| 33 | // CHECK: (mutexes: write [[M1:M[0-9]+]]): |
| 34 | // CHECK: Previous write of size 4 at {{.*}} by thread T2 |
| 35 | // CHECK: (mutexes: write [[M2:M[0-9]+]], read [[M3:M[0-9]+]]): |
| 36 | // CHECK: Mutex [[M1]] (0x{{.*}}) created at: |
| 37 | // CHECK: #1 main {{.*}}/mutexset6.cc:[[@LINE+5]] |
| 38 | // CHECK: Mutex [[M2]] (0x{{.*}}) created at: |
| 39 | // CHECK: #1 main {{.*}}/mutexset6.cc:[[@LINE+4]] |
| 40 | // CHECK: Mutex [[M3]] (0x{{.*}}) created at: |
| 41 | // CHECK: #1 main {{.*}}/mutexset6.cc:[[@LINE+3]] |
| 42 | pthread_mutex_init(&mtx1, 0); |
| 43 | pthread_spin_init(&mtx2, 0); |
| 44 | pthread_rwlock_init(&mtx3, 0); |
| 45 | pthread_t t[2]; |
| 46 | pthread_create(&t[0], NULL, Thread1, NULL); |
| 47 | pthread_create(&t[1], NULL, Thread2, NULL); |
| 48 | pthread_join(t[0], NULL); |
| 49 | pthread_join(t[1], NULL); |
| 50 | pthread_mutex_destroy(&mtx1); |
| 51 | pthread_spin_destroy(&mtx2); |
| 52 | pthread_rwlock_destroy(&mtx3); |
| 53 | } |