1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7
8int fds[2];
9int X;
10
11void *Thread1(void *x) {
12 X = 42;
13 write(fd: fds[1], buf: "a", n: 1);
14 close(fd: fds[1]);
15 return NULL;
16}
17
18void *Thread2(void *x) {
19 char buf;
20 while (read(fd: fds[0], buf: &buf, nbytes: 1) != 1) {
21 }
22 X = 43;
23 close(fd: fds[0]);
24 return NULL;
25}
26
27int main() {
28 socketpair(AF_UNIX, SOCK_STREAM, protocol: 0, fds: fds);
29 pthread_t t[2];
30 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
31 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
32 pthread_join(th: t[0], NULL);
33 pthread_join(th: t[1], NULL);
34 fprintf(stderr, format: "OK\n");
35}
36
37// CHECK-NOT: WARNING: ThreadSanitizer: data race
38

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