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 <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 | |
| 11 | static void *racer(void *p) { |
| 12 | *(int*)p = 42; |
| 13 | return 0; |
| 14 | } |
| 15 | |
| 16 | int main() { |
| 17 | switch (fork()) { |
| 18 | default: // parent |
| 19 | while (wait(0) < 0) {} |
| 20 | break; |
| 21 | case 0: // child |
| 22 | { |
| 23 | int x = 0; |
| 24 | pthread_t th1, th2; |
| 25 | pthread_create(&th1, 0, racer, &x); |
| 26 | pthread_create(&th2, 0, racer, &x); |
| 27 | pthread_join(th1, 0); |
| 28 | pthread_join(th2, 0); |
| 29 | exit(0); |
| 30 | break; |
| 31 | } |
| 32 | case -1: // error |
| 33 | fprintf(stderr, "failed to fork (%d)\n", errno); |
| 34 | exit(1); |
| 35 | } |
| 36 | fprintf(stderr, "OK\n"); |
| 37 | } |
| 38 | |
| 39 | // CHECK: ThreadSanitizer: data race |
| 40 | // CHECK: OK |
| 41 | |