• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-DIE
2 // RUN: %clangxx_tsan -O1 %s -o %t && TSAN_OPTIONS="die_after_fork=0" %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-NODIE
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <pthread.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 
sleeper(void * p)11 static void *sleeper(void *p) {
12   sleep(10);
13   return 0;
14 }
15 
main()16 int main() {
17   pthread_t th;
18   pthread_create(&th, 0, sleeper, 0);
19   switch (fork()) {
20   default:  // parent
21     while (wait(0) < 0) {}
22     break;
23   case 0:  // child
24     {
25       pthread_t th2;
26       pthread_create(&th2, 0, sleeper, 0);
27       exit(0);
28       break;
29     }
30   case -1:  // error
31     fprintf(stderr, "failed to fork (%d)\n", errno);
32     exit(1);
33   }
34   fprintf(stderr, "OK\n");
35 }
36 
37 // CHECK-DIE: ThreadSanitizer: starting new threads after multi-threaded fork is not supported
38 // CHECK-DIE: OK
39 
40 // CHECK-NODIE-NOT: ThreadSanitizer: starting new threads after multi-threaded fork is not supported
41 // CHECK-NODIE: OK
42 
43