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 *mtx; |
| 6 | |
| 7 | void *Thread1(void *x) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 8 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 9 | pthread_mutex_lock(mtx); |
| 10 | Global++; |
| 11 | pthread_mutex_unlock(mtx); |
| 12 | return NULL; |
| 13 | } |
| 14 | |
| 15 | void *Thread2(void *x) { |
| 16 | Global--; |
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 | return NULL; |
| 19 | } |
| 20 | |
| 21 | int main() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 22 | barrier_init(&barrier, 2); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 23 | // CHECK: WARNING: ThreadSanitizer: data race |
| 24 | // CHECK: Write of size 4 at {{.*}} by thread T1 |
| 25 | // CHECK: (mutexes: write [[M1:M[0-9]+]]): |
| 26 | // CHECK: Previous write of size 4 at {{.*}} by thread T2: |
| 27 | // CHECK: Mutex [[M1]] (0x{{.*}}) created at: |
| 28 | // CHECK: #0 pthread_mutex_init |
| 29 | // CHECK: #1 main {{.*}}/mutexset8.cc |
| 30 | mtx = new pthread_mutex_t; |
| 31 | pthread_mutex_init(mtx, 0); |
| 32 | pthread_t t[2]; |
| 33 | pthread_create(&t[0], NULL, Thread1, NULL); |
| 34 | pthread_create(&t[1], NULL, Thread2, NULL); |
| 35 | pthread_join(t[0], NULL); |
| 36 | pthread_join(t[1], NULL); |
| 37 | pthread_mutex_destroy(mtx); |
| 38 | delete mtx; |
| 39 | } |