Home | History | Annotate | Download | only in output_tests
      1 #include <pthread.h>
      2 #include <stddef.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <unistd.h>
      6 
      7 // Ensure that we can restore a stack of a finished thread.
      8 
      9 int g_data;
     10 
     11 void __attribute__((noinline)) foobar(int *p) {
     12   *p = 42;
     13 }
     14 
     15 void *Thread1(void *x) {
     16   foobar(&g_data);
     17   return NULL;
     18 }
     19 
     20 void *Thread2(void *x) {
     21   usleep(1000*1000);
     22   g_data = 43;
     23   return NULL;
     24 }
     25 
     26 int main() {
     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 2:
     37 // CHECK:   Previous write of size 4 at {{.*}} by thread 1:
     38 // CHECK:     #0 foobar
     39 // CHECK:     #1 Thread1
     40 // CHECK:   Thread 1 (finished) created at:
     41 // CHECK:     #0 pthread_create
     42 // CHECK:     #1 main
     43 
     44