• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 
11 struct sockaddr_in addr;
12 int X;
13 
ClientThread(void * x)14 void *ClientThread(void *x) {
15   X = 42;
16   int c = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
17   if (connect(c, (struct sockaddr*)&addr, sizeof(addr))) {
18     perror("connect");
19     exit(1);
20   }
21   if (send(c, "a", 1, 0) != 1) {
22     perror("send");
23     exit(1);
24   }
25   close(c);
26   return NULL;
27 }
28 
main()29 int main() {
30   int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
31   addr.sin_family = AF_INET;
32   inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
33   addr.sin_port = INADDR_ANY;
34   socklen_t len = sizeof(addr);
35   bind(s, (sockaddr*)&addr, len);
36   getsockname(s, (sockaddr*)&addr, &len);
37   listen(s, 10);
38   pthread_t t;
39   pthread_create(&t, 0, ClientThread, 0);
40   int c = accept(s, 0, 0);
41   char buf;
42   while (read(c, &buf, 1) != 1) {
43   }
44   X = 43;
45   close(c);
46   close(s);
47   pthread_join(t, 0);
48   fprintf(stderr, "OK\n");
49 }
50 
51 // CHECK-NOT: WARNING: ThreadSanitizer: data race
52 
53