1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "test.h"
3 
4 // Ensure that we can restore a stack of a finished thread.
5 
6 int g_data;
7 
foobar(int * p)8 void __attribute__((noinline)) foobar(int *p) {
9   *p = 42;
10 }
11 
Thread1(void * x)12 void *Thread1(void *x) {
13   foobar(&g_data);
14   barrier_wait(&barrier);
15   return NULL;
16 }
17 
Thread2(void * x)18 void *Thread2(void *x) {
19   barrier_wait(&barrier);
20   sleep(1); // let the thread finish and exit
21   g_data = 43;
22   return NULL;
23 }
24 
main()25 int main() {
26   barrier_init(&barrier, 2);
27   pthread_t t[2];
28   pthread_create(&t[0], NULL, Thread1, NULL);
29   pthread_create(&t[1], NULL, Thread2, NULL);
30   pthread_join(t[0], NULL);
31   pthread_join(t[1], NULL);
32   return 0;
33 }
34 
35 // CHECK: WARNING: ThreadSanitizer: data race
36 // CHECK:   Write of size 4 at {{.*}} by thread T2:
37 // CHECK:   Previous write of size 4 at {{.*}} by thread T1:
38 // CHECK:     #0 foobar
39 // CHECK:     #1 Thread1
40 // CHECK:   Thread T1 (tid={{.*}}, finished) created by main thread at:
41 // CHECK:     #0 pthread_create
42 // CHECK:     #1 main
43