1// 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://github.com/google/sanitizers/issues/498
10
11int g_thread_run = 1;
12pthread_mutex_t mutex;
13pthread_cond_t cond;
14
15void sig_handler(int sig) {
16 (void)sig;
17 write(fd: 2, buf: "SIGNAL\n", n: sizeof("SIGNAL\n") - 1);
18 barrier_wait(barrier: &barrier);
19}
20
21void* my_thread(void* arg) {
22 pthread_mutex_lock(mutex: &mutex);
23 while (g_thread_run)
24 pthread_cond_wait(cond: &cond, mutex: &mutex);
25 pthread_mutex_unlock(mutex: &mutex);
26 return 0;
27}
28
29int main() {
30 barrier_init(barrier: &barrier, count: 2);
31
32 pthread_mutex_init(mutex: &mutex, mutexattr: 0);
33 pthread_cond_init(cond: &cond, cond_attr: 0);
34
35 signal(SIGUSR1, handler: &sig_handler);
36 pthread_t thr;
37 pthread_create(newthread: &thr, attr: 0, start_routine: &my_thread, arg: 0);
38 // wait for thread to get inside pthread_cond_wait
39 // (can't use barrier_wait for that)
40 sleep(seconds: 1);
41 pthread_kill(threadid: thr, SIGUSR1);
42 barrier_wait(barrier: &barrier);
43 pthread_mutex_lock(mutex: &mutex);
44 g_thread_run = 0;
45 pthread_cond_signal(cond: &cond);
46 pthread_mutex_unlock(mutex: &mutex);
47 pthread_join(th: thr, thread_return: 0);
48 fprintf(stderr, format: "DONE\n");
49 return 0;
50}
51
52// CHECK: SIGNAL
53// CHECK: DONE
54

source code of compiler-rt/test/tsan/signal_cond.cpp