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 <sanitizer/linux_syscall_hooks.h>
5 #include <sys/syscall.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 
9 int counter;
10 
11 static void *incrementer(void *p) {
12   for (;;)
13     __sync_fetch_and_add(&counter, 1);
14   return 0;
15 }
16 
17 int myfork() {
18   __sanitizer_syscall_pre_fork();
19   int res = syscall(SYS_fork);
20   __sanitizer_syscall_post_fork(res);
21   return res;
22 }
23 
24 int main() {
25   barrier_init(&barrier, 2);
26   pthread_t th1;
27   pthread_create(&th1, 0, incrementer, 0);
28   for (int i = 0; i < 10; i++) {
29     switch (myfork()) {
30     default: // parent
31       while (wait(0) < 0) {
32       }
33       fprintf(stderr, ".");
34       break;
35     case 0: // child
36       __sync_fetch_and_add(&counter, 1);
37       exit(0);
38       break;
39     case -1: // error
40       fprintf(stderr, "failed to fork (%d)\n", errno);
41       exit(1);
42     }
43   }
44   fprintf(stderr, "OK\n");
45 }
46 
47 // CHECK: OK
48