blob: 89afbe1a66a8f6a0a89b83b2e056eeedb167ac59 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// 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
Stephen Hines6d186232014-11-26 17:56:19 -08005#ifdef __FreeBSD__
6#define _WITH_GETLINE // to declare getline()
7#endif
8
Stephen Hines2d1fdb22014-05-28 23:58:16 -07009#include <pthread.h>
10#include <stdio.h>
11#include <unistd.h>
12
13void *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
27int 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