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
7int counter;
8
9static void *incrementer(void *p) {
10 for (;;)
11 __sync_fetch_and_add(&counter, 1);
12 return 0;
13}
14
15int main() {
16 barrier_init(barrier: &barrier, count: 2);
17 pthread_t th1;
18 pthread_create(newthread: &th1, attr: 0, start_routine: incrementer, arg: 0);
19 for (int i = 0; i < 10; i++) {
20 switch (fork()) {
21 default: // parent
22 while (wait(stat_loc: 0) < 0) {}
23 fprintf(stderr, format: ".");
24 break;
25 case 0: // child
26 __sync_fetch_and_add(&counter, 1);
27 exit(status: 0);
28 break;
29 case -1: // error
30 fprintf(stderr, format: "failed to fork (%d)\n", errno);
31 exit(status: 1);
32 }
33 }
34 fprintf(stderr, format: "OK\n");
35}
36
37// CHECK: OK
38
39

source code of compiler-rt/test/tsan/fork_deadlock.cpp