1// RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=atexit_sleep_ms=0 %run %t 2>&1 | FileCheck %s
2#include "../test.h"
3#include <errno.h>
4#include <sched.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7
8long counter;
9
10static void *incrementer(void *arg) {
11 for (;;)
12 __sync_fetch_and_add(&counter, 1);
13 return 0;
14}
15
16static int cloned(void *arg) {
17 for (int i = 0; i < 1000; i++)
18 __sync_fetch_and_add(&counter, 1);
19 exit(status: 0);
20 return 0;
21}
22
23int main() {
24 barrier_init(barrier: &barrier, count: 2);
25 pthread_t th;
26 pthread_create(newthread: &th, attr: 0, start_routine: incrementer, arg: 0);
27 for (int i = 0; i < 100; i++) {
28 char stack[64 << 10] __attribute__((aligned(64)));
29 int pid = clone(fn: cloned, child_stack: stack + sizeof(stack), SIGCHLD, arg: 0);
30 if (pid == -1) {
31 fprintf(stderr, format: "failed to clone: %d\n", errno);
32 exit(status: 1);
33 }
34 while (wait(stat_loc: 0) != pid) {
35 }
36 }
37 fprintf(stderr, format: "DONE\n");
38}
39
40// CHECK: DONE
41

source code of compiler-rt/test/tsan/Linux/clone_deadlock.cpp