llvm-project
53 строки · 1.2 Кб
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;
17write(2, "SIGNAL\n", sizeof("SIGNAL\n") - 1);
18barrier_wait(&barrier);
19}
20
21void* my_thread(void* arg) {
22pthread_mutex_lock(&mutex);
23while (g_thread_run)
24pthread_cond_wait(&cond, &mutex);
25pthread_mutex_unlock(&mutex);
26return 0;
27}
28
29int main() {
30barrier_init(&barrier, 2);
31
32pthread_mutex_init(&mutex, 0);
33pthread_cond_init(&cond, 0);
34
35signal(SIGUSR1, &sig_handler);
36pthread_t thr;
37pthread_create(&thr, 0, &my_thread, 0);
38// wait for thread to get inside pthread_cond_wait
39// (can't use barrier_wait for that)
40sleep(1);
41pthread_kill(thr, SIGUSR1);
42barrier_wait(&barrier);
43pthread_mutex_lock(&mutex);
44g_thread_run = 0;
45pthread_cond_signal(&cond);
46pthread_mutex_unlock(&mutex);
47pthread_join(thr, 0);
48fprintf(stderr, "DONE\n");
49return 0;
50}
51
52// CHECK: SIGNAL
53// CHECK: DONE
54