Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 2 | // UNSUPPORTED: darwin |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 3 | #include <pthread.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <sys/types.h> |
| 8 | |
| 9 | int fds[2]; |
| 10 | int X; |
| 11 | |
| 12 | void *Thread1(void *x) { |
| 13 | X = 42; |
| 14 | write(fds[1], "a", 1); |
| 15 | return NULL; |
| 16 | } |
| 17 | |
| 18 | void *Thread2(void *x) { |
| 19 | char buf; |
| 20 | while (read(fds[0], &buf, 1) != 1) { |
| 21 | } |
| 22 | X = 43; |
| 23 | return NULL; |
| 24 | } |
| 25 | |
| 26 | int 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 |