1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot #include <stdlib.h>
3*7c3d14c8STreehugger Robot #include <stdio.h>
4*7c3d14c8STreehugger Robot #include <errno.h>
5*7c3d14c8STreehugger Robot #include <pthread.h>
6*7c3d14c8STreehugger Robot #include <unistd.h>
7*7c3d14c8STreehugger Robot #include <sys/types.h>
8*7c3d14c8STreehugger Robot #include <sys/wait.h>
9*7c3d14c8STreehugger Robot
racer(void * p)10*7c3d14c8STreehugger Robot static void *racer(void *p) {
11*7c3d14c8STreehugger Robot *(int*)p = 42;
12*7c3d14c8STreehugger Robot return 0;
13*7c3d14c8STreehugger Robot }
14*7c3d14c8STreehugger Robot
main()15*7c3d14c8STreehugger Robot int main() {
16*7c3d14c8STreehugger Robot switch (fork()) {
17*7c3d14c8STreehugger Robot default: // parent
18*7c3d14c8STreehugger Robot while (wait(0) < 0) {}
19*7c3d14c8STreehugger Robot break;
20*7c3d14c8STreehugger Robot case 0: // child
21*7c3d14c8STreehugger Robot {
22*7c3d14c8STreehugger Robot int x = 0;
23*7c3d14c8STreehugger Robot pthread_t th1, th2;
24*7c3d14c8STreehugger Robot pthread_create(&th1, 0, racer, &x);
25*7c3d14c8STreehugger Robot pthread_create(&th2, 0, racer, &x);
26*7c3d14c8STreehugger Robot pthread_join(th1, 0);
27*7c3d14c8STreehugger Robot pthread_join(th2, 0);
28*7c3d14c8STreehugger Robot exit(0);
29*7c3d14c8STreehugger Robot break;
30*7c3d14c8STreehugger Robot }
31*7c3d14c8STreehugger Robot case -1: // error
32*7c3d14c8STreehugger Robot fprintf(stderr, "failed to fork (%d)\n", errno);
33*7c3d14c8STreehugger Robot exit(1);
34*7c3d14c8STreehugger Robot }
35*7c3d14c8STreehugger Robot fprintf(stderr, "OK\n");
36*7c3d14c8STreehugger Robot }
37*7c3d14c8STreehugger Robot
38*7c3d14c8STreehugger Robot // CHECK: ThreadSanitizer: data race
39*7c3d14c8STreehugger Robot // CHECK: OK
40*7c3d14c8STreehugger Robot
41