blob: 98a82623ee6513b22d533581f380c8b1ac688f78 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08002// UNSUPPORTED: darwin
Stephen Hines2d1fdb22014-05-28 23:58:16 -07003#include <pthread.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <sys/types.h>
8
9int fds[2];
10int X;
11
12void *Thread1(void *x) {
13 X = 42;
14 write(fds[1], "a", 1);
15 return NULL;
16}
17
18void *Thread2(void *x) {
19 char buf;
20 while (read(fds[0], &buf, 1) != 1) {
21 }
22 X = 43;
23 return NULL;
24}
25
26int main() {
27 pipe(fds);
28 int pid = vfork();
29 if (pid < 0) {
30 printf("FAIL to vfork\n");
31 exit(1);
32 }
33 if (pid == 0) { // child
34 // Closing of fds must not affect parent process.
35 // Strictly saying this is undefined behavior, because vfork child is not
36 // allowed to call any functions other than exec/exit. But this is what
37 // openjdk does.
38 close(fds[0]);
39 close(fds[1]);
40 _exit(0);
41 }
42 pthread_t t[2];
43 pthread_create(&t[0], NULL, Thread1, NULL);
44 pthread_create(&t[1], NULL, Thread2, NULL);
45 pthread_join(t[0], NULL);
46 pthread_join(t[1], NULL);
47 printf("DONE\n");
48}
49
50// CHECK-NOT: WARNING: ThreadSanitizer: data race
51// CHECK-NOT: FAIL to vfork
52// CHECK: DONE