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