blob: f5eae745d4072a7f6dc55cb485147467686ece85 [file] [log] [blame]
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -07001// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "test.h"
3#include <signal.h>
4#include <unistd.h>
5#include <errno.h>
6#include <semaphore.h>
7
8// Test that signals can be delivered to blocked pthread_cond_wait.
9// https://code.google.com/p/thread-sanitizer/issues/detail?id=91
10
11int g_thread_run = 1;
12pthread_mutex_t mutex;
13pthread_cond_t cond;
14sem_t sem;
15
16void sig_handler(int sig) {
17 (void)sig;
18 write(1, "SIGNAL\n", sizeof("SIGNAL\n") - 1);
19 sem_post(&sem);
20}
21
22void* my_thread(void* arg) {
23 pthread_mutex_lock(&mutex);
24 while (g_thread_run)
25 pthread_cond_wait(&cond, &mutex);
26 pthread_mutex_unlock(&mutex);
27 return 0;
28}
29
30int main() {
31 sem_init(&sem, 0, 0);
32 signal(SIGUSR1, &sig_handler);
33 pthread_t thr;
34 pthread_create(&thr, 0, &my_thread, 0);
35 // wait for thread to get inside pthread_cond_wait
36 // (can't use barrier_wait for that)
37 sleep(1);
38 pthread_kill(thr, SIGUSR1);
39 while (sem_wait(&sem) == -1 && errno == EINTR) {
40 }
41 pthread_mutex_lock(&mutex);
42 g_thread_run = 0;
43 pthread_cond_signal(&cond);
44 pthread_mutex_unlock(&mutex);
45 pthread_join(thr, 0);
46 fprintf(stderr, "DONE\n");
47 return 0;
48}
49
50// CHECK: SIGNAL
51// CHECK: DONE