blob: 2a3e5bb95e875074f9991ba39494c661f90fc1e2 [file] [log] [blame]
Stephen Hines6a211c52014-07-21 00:49:56 -07001// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
Stephen Hines86277eb2015-03-23 12:06:32 -07002#include "test.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -07003
4int Global;
5pthread_mutex_t mtx;
6
7void *Thread1(void *x) {
8 pthread_mutex_lock(&mtx);
9 Global++;
10 pthread_mutex_unlock(&mtx);
Stephen Hines86277eb2015-03-23 12:06:32 -070011 barrier_wait(&barrier);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070012 return NULL;
13}
14
15void *Thread2(void *x) {
Stephen Hines86277eb2015-03-23 12:06:32 -070016 barrier_wait(&barrier);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070017 Global--;
18 return NULL;
19}
20
21int main() {
Stephen Hines86277eb2015-03-23 12:06:32 -070022 barrier_init(&barrier, 2);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070023 // CHECK: WARNING: ThreadSanitizer: data race
24 // CHECK: Write of size 4 at {{.*}} by thread T2:
25 // CHECK: Previous write of size 4 at {{.*}} by thread T1
26 // CHECK: (mutexes: write [[M1:M[0-9]+]]):
27 // CHECK: Mutex [[M1]] (0x{{.*}}) created at:
28 // CHECK: #0 pthread_mutex_init
29 // CHECK: #1 main {{.*}}/mutexset2.cc:[[@LINE+1]]
30 pthread_mutex_init(&mtx, 0);
31 pthread_t t[2];
32 pthread_create(&t[0], NULL, Thread1, NULL);
33 pthread_create(&t[1], NULL, Thread2, NULL);
34 pthread_join(t[0], NULL);
35 pthread_join(t[1], NULL);
36 pthread_mutex_destroy(&mtx);
37}