1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// Always enable asserts.
4#ifdef NDEBUG
5#undef NDEBUG
6#endif
7
8#include <assert.h>
9#include <stdlib.h>
10#include <signal.h>
11#include <unistd.h>
12#include <stdio.h>
13
14static bool signal_handler_ran = false;
15
16void do_nothing_signal_handler(int signum) {
17 write(fd: 1, buf: "HANDLER\n", n: 8);
18 signal_handler_ran = true;
19}
20
21int main() {
22 const int kSignalToTest = SIGSYS;
23 assert(SIG_ERR != signal(kSignalToTest, do_nothing_signal_handler));
24 sigset_t empty_set;
25 assert(0 == sigemptyset(&empty_set));
26 sigset_t one_signal = empty_set;
27 assert(0 == sigaddset(&one_signal, kSignalToTest));
28 sigset_t old_set;
29 assert(0 == sigprocmask(SIG_BLOCK, &one_signal, &old_set));
30 raise(sig: kSignalToTest);
31 assert(!signal_handler_ran);
32 sigset_t all_but_one;
33 assert(0 == sigfillset(&all_but_one));
34 assert(0 == sigdelset(&all_but_one, kSignalToTest));
35 sigsuspend(set: &all_but_one);
36 assert(signal_handler_ran);
37
38 // Restore the original set.
39 assert(0 == sigprocmask(SIG_SETMASK, &old_set, NULL));
40 printf(format: "DONE\n");
41}
42
43// CHECK: HANDLER
44// CHECK: DONE
45

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