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 "test.h"
4 
5 volatile int X;
6 volatile int N;
7 void (*volatile F)();
8 
foo()9 static void foo() {
10   if (--N == 0)
11     X = 42;
12   else
13     F();
14 }
15 
Thread(void * p)16 void *Thread(void *p) {
17 #ifdef ORDER1
18   barrier_wait(&barrier);
19 #endif
20   F();
21 #ifdef ORDER2
22   barrier_wait(&barrier);
23 #endif
24   return 0;
25 }
26 
RoundUp(size_t n,size_t to)27 static size_t RoundUp(size_t n, size_t to) {
28   return ((n + to - 1) / to) * to;
29 }
30 
main()31 int main() {
32   barrier_init(&barrier, 2);
33   N = 50000;
34   F = foo;
35   pthread_t t;
36   pthread_attr_t a;
37   pthread_attr_init(&a);
38   size_t stack_size = N * 256 + (1 << 20);
39   stack_size = RoundUp(stack_size, 0x10000);  // round the stack size to 64k
40   int ret = pthread_attr_setstacksize(&a, stack_size);
41   if (ret) abort();
42   pthread_create(&t, &a, Thread, 0);
43 #ifdef ORDER2
44   barrier_wait(&barrier);
45 #endif
46   X = 43;
47 #ifdef ORDER1
48   barrier_wait(&barrier);
49 #endif
50 
51   pthread_join(t, 0);
52 }
53 
54 // CHECK: WARNING: ThreadSanitizer: data race
55 // CHECK:    #100 foo
56 // We must output suffucuently large stack (at least 100 frames)
57 
58