Home | History | Annotate | Download | only in tests
      1 /*
      2  * Test program with happens-before / happens-after annotations that triggers
      3  * a data race. The data race will only be reported if happens-after
      4  * annotations that occur in different threads are not totally ordered. Or:
      5  * this is a test for the implementation of ordering annotations.
      6  */
      7 
      8 
      9 #include <stdio.h>
     10 #include <pthread.h>
     11 #include "unified_annotations.h"
     12 
     13 
     14 static int s_i;
     15 
     16 
     17 static void* thread_func(void* arg)
     18 {
     19   int i;
     20 
     21   U_ANNOTATE_HAPPENS_AFTER(&s_i);
     22   i = s_i;
     23   U_ANNOTATE_HAPPENS_AFTER(&s_i);
     24   *(int*)arg = i;
     25   return NULL;
     26 }
     27 
     28 int main(int argc, char** argv)
     29 {
     30   const struct timespec delay = { 0, 100 * 1000 * 1000 };
     31   pthread_t tid[2];
     32   int result[2];
     33 
     34   U_ANNOTATE_HAPPENS_BEFORE(&s_i);
     35   pthread_create(&tid[0], 0, thread_func, &result[0]);
     36   pthread_create(&tid[1], 0, thread_func, &result[1]);
     37 
     38   nanosleep(&delay, 0);
     39 
     40   s_i = 1;
     41 
     42   pthread_join(tid[0], NULL);
     43   pthread_join(tid[1], NULL);
     44 
     45   fprintf(stderr, "Done.\n");
     46 
     47   return 0;
     48 }
     49