blob: 08f5f6534a3be153011b5749cbb9acc5c38d1f77 [file] [log] [blame]
Alexey Samsonov341e5bc2012-09-18 07:23:54 +00001// RUN: %clang_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
Kostya Serebryanyff15ef02012-05-10 14:18:22 +00002#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5
6int Global;
7
8void __attribute__((noinline)) foo1() {
9 Global = 42;
10}
11
12void __attribute__((noinline)) bar1() {
13 volatile int tmp = 42; (void)tmp;
14 foo1();
15}
16
17void __attribute__((noinline)) foo2() {
18 volatile int v = Global; (void)v;
19}
20
21void __attribute__((noinline)) bar2() {
22 volatile int tmp = 42; (void)tmp;
23 foo2();
24}
25
26void *Thread1(void *x) {
27 usleep(1000000);
28 bar1();
29 return NULL;
30}
31
32void *Thread2(void *x) {
33 bar2();
34 return NULL;
35}
36
37void StartThread(pthread_t *t, void *(*f)(void*)) {
38 pthread_create(t, NULL, f, NULL);
39}
40
41int main() {
42 pthread_t t[2];
43 StartThread(&t[0], Thread1);
44 StartThread(&t[1], Thread2);
45 pthread_join(t[0], NULL);
46 pthread_join(t[1], NULL);
47 return 0;
48}
49
50// CHECK: WARNING: ThreadSanitizer: data race
51// CHECK-NEXT: Write of size 4 at {{.*}} by thread 1:
Alexey Samsonov341e5bc2012-09-18 07:23:54 +000052// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack.c:9{{(:3)?}} ({{.*}})
53// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack.c:14{{(:3)?}} ({{.*}})
54// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack.c:28{{(:3)?}} ({{.*}})
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000055// CHECK: Previous read of size 4 at {{.*}} by thread 2:
Alexey Samsonov341e5bc2012-09-18 07:23:54 +000056// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack.c:18{{(:26)?}} ({{.*}})
57// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack.c:23{{(:3)?}} ({{.*}})
58// CHECK-NEXT: #2 Thread2{{.*}} {{.*}}simple_stack.c:33{{(:3)?}} ({{.*}})
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000059// CHECK: Thread 1 (running) created at:
Kostya Serebryanyff15ef02012-05-10 14:18:22 +000060// CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}})
Alexey Samsonov341e5bc2012-09-18 07:23:54 +000061// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}})
62// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}})
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000063// CHECK: Thread 2 ({{.*}}) created at:
Kostya Serebryanyff15ef02012-05-10 14:18:22 +000064// CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}})
Alexey Samsonov341e5bc2012-09-18 07:23:54 +000065// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}})
66// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:44{{(:3)?}} ({{.*}})