1*7c3d14c8STreehugger Robot // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot #include "test.h"
3*7c3d14c8STreehugger Robot #include <errno.h>
4*7c3d14c8STreehugger Robot #include <sys/mman.h>
5*7c3d14c8STreehugger Robot
SubWorker(void * arg)6*7c3d14c8STreehugger Robot void *SubWorker(void *arg) {
7*7c3d14c8STreehugger Robot (void)arg;
8*7c3d14c8STreehugger Robot const int kMmapSize = 65536;
9*7c3d14c8STreehugger Robot for (int i = 0; i < 500; i++) {
10*7c3d14c8STreehugger Robot int *ptr = (int*)mmap(0, kMmapSize, PROT_READ | PROT_WRITE,
11*7c3d14c8STreehugger Robot MAP_PRIVATE | MAP_ANON, -1, 0);
12*7c3d14c8STreehugger Robot if (ptr == MAP_FAILED)
13*7c3d14c8STreehugger Robot exit(printf("mmap failed: %d\n", errno));
14*7c3d14c8STreehugger Robot *ptr = 42;
15*7c3d14c8STreehugger Robot if (munmap(ptr, kMmapSize))
16*7c3d14c8STreehugger Robot exit(printf("munmap failed: %d\n", errno));
17*7c3d14c8STreehugger Robot }
18*7c3d14c8STreehugger Robot return 0;
19*7c3d14c8STreehugger Robot }
20*7c3d14c8STreehugger Robot
Worker1(void * arg)21*7c3d14c8STreehugger Robot void *Worker1(void *arg) {
22*7c3d14c8STreehugger Robot (void)arg;
23*7c3d14c8STreehugger Robot pthread_t th[4];
24*7c3d14c8STreehugger Robot for (int i = 0; i < 4; i++) {
25*7c3d14c8STreehugger Robot if (pthread_create(&th[i], 0, SubWorker, 0))
26*7c3d14c8STreehugger Robot exit(printf("pthread_create failed: %d\n", errno));
27*7c3d14c8STreehugger Robot }
28*7c3d14c8STreehugger Robot for (int i = 0; i < 4; i++) {
29*7c3d14c8STreehugger Robot if (pthread_join(th[i], 0))
30*7c3d14c8STreehugger Robot exit(printf("pthread_join failed: %d\n", errno));
31*7c3d14c8STreehugger Robot }
32*7c3d14c8STreehugger Robot return 0;
33*7c3d14c8STreehugger Robot }
34*7c3d14c8STreehugger Robot
Worker(void * arg)35*7c3d14c8STreehugger Robot void *Worker(void *arg) {
36*7c3d14c8STreehugger Robot (void)arg;
37*7c3d14c8STreehugger Robot pthread_t th[4];
38*7c3d14c8STreehugger Robot for (int i = 0; i < 4; i++) {
39*7c3d14c8STreehugger Robot if (pthread_create(&th[i], 0, Worker1, 0))
40*7c3d14c8STreehugger Robot exit(printf("pthread_create failed: %d\n", errno));
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot for (int i = 0; i < 4; i++) {
43*7c3d14c8STreehugger Robot if (pthread_join(th[i], 0))
44*7c3d14c8STreehugger Robot exit(printf("pthread_join failed: %d\n", errno));
45*7c3d14c8STreehugger Robot }
46*7c3d14c8STreehugger Robot return 0;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot
main()49*7c3d14c8STreehugger Robot int main() {
50*7c3d14c8STreehugger Robot // This test is flaky on several builders:
51*7c3d14c8STreehugger Robot // https://groups.google.com/d/msg/llvm-dev/KUFPdLhBN3Q/L75rwW9xBgAJ
52*7c3d14c8STreehugger Robot // The cause is unknown (lit hides test output on failures).
53*7c3d14c8STreehugger Robot #if 0
54*7c3d14c8STreehugger Robot pthread_t th[4];
55*7c3d14c8STreehugger Robot for (int i = 0; i < 4; i++) {
56*7c3d14c8STreehugger Robot if (pthread_create(&th[i], 0, Worker, 0))
57*7c3d14c8STreehugger Robot exit(printf("pthread_create failed: %d\n", errno));
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot for (int i = 0; i < 4; i++) {
60*7c3d14c8STreehugger Robot if (pthread_join(th[i], 0))
61*7c3d14c8STreehugger Robot exit(printf("pthread_join failed: %d\n", errno));
62*7c3d14c8STreehugger Robot }
63*7c3d14c8STreehugger Robot #endif
64*7c3d14c8STreehugger Robot fprintf(stderr, "DONE\n");
65*7c3d14c8STreehugger Robot }
66*7c3d14c8STreehugger Robot
67*7c3d14c8STreehugger Robot // CHECK: DONE
68