1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t
2
3// Make sure TSan doesn't deadlock on a file stream lock at program shutdown.
4// See https://code.google.com/p/thread-sanitizer/issues/detail?id=47
5#include <pthread.h>
6#include <stdio.h>
7#include <unistd.h>
8
9void *thread(void *unused) {
10  char *line = NULL;
11  size_t size;
12  int fd[2];
13  pipe(fd);
14  // Forge a non-standard stream to make sure it's not closed.
15  FILE *stream = fdopen(fd[0], "r");
16  while (1) {
17    volatile int res = getline(&line, &size, stream);
18    (void)res;
19  }
20  return NULL;
21}
22
23int main() {
24  pthread_t t;
25  pthread_attr_t a;
26  pthread_attr_init(&a);
27  pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
28  pthread_create(&t, &a, thread, NULL);
29  pthread_attr_destroy(&a);
30  fprintf(stderr, "DONE\n");
31  return 0;
32  // ThreadSanitizer used to hang here because of a deadlock on a file stream.
33}
34
35// CHECK: DONE
36