1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// Test that a signal is not delivered when it is blocked.
4
5#include "test.h"
6#include <semaphore.h>
7#include <signal.h>
8#include <errno.h>
9
10int stop;
11sig_atomic_t signal_blocked;
12
13void handler(int signum) {
14 if (signal_blocked) {
15 fprintf(stderr, "signal arrived when blocked\n");
16 exit(1);
17 }
18}
19
20void *thread(void *arg) {
21 sigset_t myset;
22 sigemptyset(&myset);
23 sigaddset(&myset, SIGUSR1);
24 while (!__atomic_load_n(&stop, __ATOMIC_RELAXED)) {
25 usleep(1);
26 if (pthread_sigmask(SIG_BLOCK, &myset, 0)) {
27 fprintf(stderr, "pthread_sigmask failed %d\n", errno);
28 exit(1);
29 }
30 signal_blocked = 1;
31 usleep(1);
32 signal_blocked = 0;
33 if (pthread_sigmask(SIG_UNBLOCK, &myset, 0)) {
34 fprintf(stderr, "pthread_sigmask failed %d\n", errno);
35 exit(1);
36 }
37 }
38 return 0;
39}
40
41int main(int argc, char** argv) {
42 struct sigaction act = {};
43 act.sa_handler = &handler;
44 if (sigaction(SIGUSR1, &act, 0)) {
45 fprintf(stderr, "sigaction failed %d\n", errno);
46 return 1;
47 }
48 pthread_t th;
49 pthread_create(&th, 0, thread, 0);
50 for (int i = 0; i < 100000; i++)
51 pthread_kill(th, SIGUSR1);
52 __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);
53 pthread_join(th, 0);
54 fprintf(stderr, "DONE\n");
55 return 0;
56}
57
58// CHECK-NOT: ThreadSanitizer CHECK
59// CHECK-NOT: WARNING: ThreadSanitizer:
60// CHECK: DONE
61