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
11void *SubWorker(void *arg) {
12 (void)arg;
13 const int kMmapSize = 65536;
14 for (int i = 0; i < 500; i++) {
15 int *ptr = (int*)mmap(addr: 0, len: kMmapSize, PROT_READ | PROT_WRITE,
16 MAP_PRIVATE | MAP_ANON, fd: -1, offset: 0);
17 if (ptr == MAP_FAILED)
18 exit(status: printf(format: "mmap failed: %d\n", errno));
19 *ptr = 42;
20 if (munmap(addr: ptr, len: kMmapSize))
21 exit(status: printf(format: "munmap failed: %d\n", errno));
22 }
23 return 0;
24}
25
26void *Worker1(void *arg) {
27 (void)arg;
28 pthread_t th[4];
29 for (int i = 0; i < 4; i++) {
30 if (pthread_create(newthread: &th[i], attr: 0, start_routine: SubWorker, arg: 0))
31 exit(status: printf(format: "pthread_create failed: %d\n", errno));
32 }
33 for (int i = 0; i < 4; i++) {
34 if (pthread_join(th: th[i], thread_return: 0))
35 exit(status: printf(format: "pthread_join failed: %d\n", errno));
36 }
37 return 0;
38}
39
40void *Worker(void *arg) {
41 (void)arg;
42 pthread_t th[4];
43 for (int i = 0; i < 4; i++) {
44 if (pthread_create(newthread: &th[i], attr: 0, start_routine: Worker1, arg: 0))
45 exit(status: printf(format: "pthread_create failed: %d\n", errno));
46 }
47 for (int i = 0; i < 4; i++) {
48 if (pthread_join(th: th[i], thread_return: 0))
49 exit(status: printf(format: "pthread_join failed: %d\n", errno));
50 }
51 return 0;
52}
53
54int main() {
55 pthread_t th[4];
56 for (int i = 0; i < 4; i++) {
57 if (pthread_create(newthread: &th[i], attr: 0, start_routine: Worker, arg: 0))
58 exit(status: printf(format: "pthread_create failed: %d\n", errno));
59 }
60 for (int i = 0; i < 4; i++) {
61 if (pthread_join(th: th[i], thread_return: 0))
62 exit(status: printf(format: "pthread_join failed: %d\n", errno));
63 }
64 fprintf(stderr, format: "DONE\n");
65}
66
67// CHECK: DONE
68

source code of compiler-rt/test/tsan/mmap_stress.cpp