Home | History | Annotate | Download | only in tsan
      1 // RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=atexit_sleep_ms=50 %run %t 2>&1 | FileCheck %s
      2 #include "test.h"
      3 #include <errno.h>
      4 #include <sys/types.h>
      5 #include <sys/wait.h>
      6 
      7 int counter;
      8 
      9 static void *incrementer(void *p) {
     10   for (;;)
     11     __sync_fetch_and_add(&counter, 1);
     12   return 0;
     13 }
     14 
     15 int main() {
     16   barrier_init(&barrier, 2);
     17   pthread_t th1;
     18   pthread_create(&th1, 0, incrementer, 0);
     19   for (int i = 0; i < 10; i++) {
     20     switch (fork()) {
     21     default:  // parent
     22       while (wait(0) < 0) {}
     23       fprintf(stderr, ".");
     24       break;
     25     case 0:  // child
     26       __sync_fetch_and_add(&counter, 1);
     27       exit(0);
     28       break;
     29     case -1:  // error
     30       fprintf(stderr, "failed to fork (%d)\n", errno);
     31       exit(1);
     32     }
     33   }
     34   fprintf(stderr, "OK\n");
     35 }
     36 
     37 // CHECK: OK
     38 
     39