• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 int g_thread_run = 1;
12 pthread_mutex_t mutex;
13 pthread_cond_t cond;
14 
sig_handler(int sig)15 void sig_handler(int sig) {
16   (void)sig;
17   write(1, "SIGNAL\n", sizeof("SIGNAL\n") - 1);
18   barrier_wait(&barrier);
19 }
20 
my_thread(void * arg)21 void* my_thread(void* arg) {
22   pthread_mutex_lock(&mutex);
23   while (g_thread_run)
24     pthread_cond_wait(&cond, &mutex);
25   pthread_mutex_unlock(&mutex);
26   return 0;
27 }
28 
main()29 int main() {
30   barrier_init(&barrier, 2);
31 
32   pthread_mutex_init(&mutex, 0);
33   pthread_cond_init(&cond, 0);
34 
35   signal(SIGUSR1, &sig_handler);
36   pthread_t thr;
37   pthread_create(&thr, 0, &my_thread, 0);
38   // wait for thread to get inside pthread_cond_wait
39   // (can't use barrier_wait for that)
40   sleep(1);
41   pthread_kill(thr, SIGUSR1);
42   barrier_wait(&barrier);
43   pthread_mutex_lock(&mutex);
44   g_thread_run = 0;
45   pthread_cond_signal(&cond);
46   pthread_mutex_unlock(&mutex);
47   pthread_join(thr, 0);
48   fprintf(stderr, "DONE\n");
49   return 0;
50 }
51 
52 // CHECK: SIGNAL
53 // CHECK: DONE
54