• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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://github.com/google/sanitizers/issues/454
5 #ifdef __FreeBSD__
6 #define _WITH_GETLINE  // to declare getline()
7 #endif
8 
9 #include <pthread.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 
thread(void * unused)13 void *thread(void *unused) {
14   char *line = NULL;
15   size_t size;
16   int fd[2];
17   pipe(fd);
18   // Forge a non-standard stream to make sure it's not closed.
19   FILE *stream = fdopen(fd[0], "r");
20   while (1) {
21     volatile int res = getline(&line, &size, stream);
22     (void)res;
23   }
24   return NULL;
25 }
26 
main()27 int main() {
28   pthread_t t;
29   pthread_attr_t a;
30   pthread_attr_init(&a);
31   pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
32   pthread_create(&t, &a, thread, NULL);
33   pthread_attr_destroy(&a);
34   fprintf(stderr, "DONE\n");
35   return 0;
36   // ThreadSanitizer used to hang here because of a deadlock on a file stream.
37 }
38 
39 // CHECK: DONE
40