blob: db860e005d6718097f834fac9896bb266b2f252e [file] [log] [blame]
Stephen Hines6a211c52014-07-21 00:49:56 -07001// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
Stephen Hines2d1fdb22014-05-28 23:58:16 -07002#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5
6int Global;
7pthread_mutex_t mtx1;
8pthread_mutex_t mtx2;
9
10void *Thread1(void *x) {
11 pthread_mutex_lock(&mtx1);
12 pthread_mutex_lock(&mtx2);
13 Global++;
14 pthread_mutex_unlock(&mtx2);
15 pthread_mutex_unlock(&mtx1);
16 return NULL;
17}
18
19void *Thread2(void *x) {
20 sleep(1);
21 Global--;
22 return NULL;
23}
24
25int main() {
26 // CHECK: WARNING: ThreadSanitizer: data race
27 // CHECK: Write of size 4 at {{.*}} by thread T2:
28 // CHECK: Previous write of size 4 at {{.*}} by thread T1
29 // CHECK: (mutexes: write [[M1:M[0-9]+]], write [[M2:M[0-9]+]]):
30 // CHECK: Mutex [[M1]] (0x{{.*}}) created at:
31 // CHECK: #0 pthread_mutex_init
32 // CHECK: #1 main {{.*}}/mutexset4.cc:[[@LINE+4]]
33 // CHECK: Mutex [[M2]] (0x{{.*}}) created at:
34 // CHECK: #0 pthread_mutex_init
35 // CHECK: #1 main {{.*}}/mutexset4.cc:[[@LINE+2]]
36 pthread_mutex_init(&mtx1, 0);
37 pthread_mutex_init(&mtx2, 0);
38 pthread_t t[2];
39 pthread_create(&t[0], NULL, Thread1, NULL);
40 pthread_create(&t[1], NULL, Thread2, NULL);
41 pthread_join(t[0], NULL);
42 pthread_join(t[1], NULL);
43 pthread_mutex_destroy(&mtx1);
44 pthread_mutex_destroy(&mtx2);
45}