Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 4 | // See https://github.com/google/sanitizers/issues/454 |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 5 | #ifdef __FreeBSD__ |
| 6 | #define _WITH_GETLINE // to declare getline() |
| 7 | #endif |
| 8 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 9 | #include <pthread.h> |
| 10 | #include <stdio.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 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 | |
| 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 |