1// RUN: %clang_tsan -O1 %s -lpthread -o %t && %deflake %run %t | FileCheck %s
2// Regression test for
3// https://github.com/google/sanitizers/issues/468
4// When the data race was reported, pthread_atfork() handler used to be
5// executed which caused another race report in the same thread, which resulted
6// in a deadlock.
7#include "test.h"
8
9int glob = 0;
10
11void *worker(void *unused) {
12 barrier_wait(barrier: &barrier);
13 glob++;
14 return NULL;
15}
16
17void atfork() {
18 fprintf(stderr, format: "ATFORK\n");
19 glob++;
20}
21
22int main() {
23 barrier_init(barrier: &barrier, count: 2);
24 pthread_atfork(prepare: atfork, parent: atfork, child: atfork);
25 pthread_t t;
26 pthread_create(newthread: &t, NULL, start_routine: worker, NULL);
27 glob++;
28 barrier_wait(barrier: &barrier);
29 pthread_join(th: t, NULL);
30 // CHECK: ThreadSanitizer: data race
31 // CHECK-NOT: ATFORK
32 return 0;
33}
34

source code of compiler-rt/test/tsan/pthread_atfork_deadlock.c