blob: 9ccf952b00505b0d24ab39c99acf09d3c44e55c5 [file] [log] [blame]
Alexey Samsonov0172c8c2013-08-07 09:02:37 +00001// RUN: %clangxx_tsan -O1 %s -o %t && not %t 2>&1 | FileCheck %s
Dmitry Vyukovad9da372012-12-06 12:16:15 +00002#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5
6int Global;
7pthread_mutex_t mtx;
8
9void *Thread1(void *x) {
10 pthread_mutex_lock(&mtx);
11 Global++;
12 pthread_mutex_unlock(&mtx);
13 return NULL;
14}
15
16void *Thread2(void *x) {
Dmitry Vyukov63a22f02012-12-07 09:24:57 +000017 sleep(1);
Dmitry Vyukovad9da372012-12-06 12:16:15 +000018 Global--;
19 return NULL;
20}
21
22int main() {
Alexey Samsonov55425eb2012-12-28 08:38:09 +000023 // 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]] created at:
28 // CHECK: #0 pthread_mutex_init
29 // CHECK: #1 main {{.*}}/mutexset2.cc:[[@LINE+1]]
Dmitry Vyukovad9da372012-12-06 12:16:15 +000030 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}