1 // RUN: %clangxx_tsan -O1 %s -o %t -DORDER1 && %deflake %run %t | FileCheck %s 2 // RUN: %clangxx_tsan -O1 %s -o %t -DORDER2 && %deflake %run %t | FileCheck %s 3 #include <pthread.h> 4 #include <stdio.h> 5 #include <unistd.h> 6 7 volatile int X; 8 volatile int N; 9 void (*volatile F)(); 10 11 static void foo() { 12 if (--N == 0) 13 X = 42; 14 else 15 F(); 16 } 17 18 void *Thread(void *p) { 19 #ifdef ORDER1 20 sleep(1); 21 #endif 22 F(); 23 return 0; 24 } 25 26 int main() { 27 N = 50000; 28 F = foo; 29 pthread_t t; 30 pthread_attr_t a; 31 pthread_attr_init(&a); 32 pthread_attr_setstacksize(&a, N * 256 + (1 << 20)); 33 pthread_create(&t, &a, Thread, 0); 34 #ifdef ORDER2 35 sleep(1); 36 #endif 37 X = 43; 38 pthread_join(t, 0); 39 } 40 41 // CHECK: WARNING: ThreadSanitizer: data race 42 // CHECK: #100 foo 43 // We must output suffucuently large stack (at least 100 frames) 44 45