Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / signal_block.cpp
blobdfd4259c43c9b2ae8e1a6f50ec3f240eafe784f0
1 // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
3 // Test that a signal is not delivered when it is blocked.
5 #include "test.h"
6 #include <semaphore.h>
7 #include <signal.h>
8 #include <errno.h>
10 int stop;
11 sig_atomic_t signal_blocked;
13 void handler(int signum) {
14 if (signal_blocked) {
15 fprintf(stderr, "signal arrived when blocked\n");
16 exit(1);
20 void *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);
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);
38 return 0;
41 int 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;
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;
58 // CHECK-NOT: ThreadSanitizer CHECK
59 // CHECK-NOT: WARNING: ThreadSanitizer:
60 // CHECK: DONE