Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / getline_nohang.cpp
blobd1bb279a450f554b272bb478c1a2eddd9150d4ef
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t
3 // Data race randomly triggered.
4 // UNSUPPORTED: target={{.*netbsd.*}}
6 // Make sure TSan doesn't deadlock on a file stream lock at program shutdown.
7 // See https://github.com/google/sanitizers/issues/454
8 #ifdef __FreeBSD__
9 #define _WITH_GETLINE // to declare getline()
10 #endif
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <unistd.h>
16 void *thread(void *unused) {
17 char *line = NULL;
18 size_t size;
19 int fd[2];
20 pipe(fd);
21 // Forge a non-standard stream to make sure it's not closed.
22 FILE *stream = fdopen(fd[0], "r");
23 while (1) {
24 volatile int res = getline(&line, &size, stream);
25 (void)res;
27 return NULL;
30 int main() {
31 pthread_t t;
32 pthread_attr_t a;
33 pthread_attr_init(&a);
34 pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
35 pthread_create(&t, &a, thread, NULL);
36 pthread_attr_destroy(&a);
37 fprintf(stderr, "DONE\n");
38 return 0;
39 // ThreadSanitizer used to hang here because of a deadlock on a file stream.
42 // CHECK: DONE