1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// UNSUPPORTED: darwin
3#include <pthread.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <signal.h>
7#include <sys/types.h>
8#include <sys/time.h>
9#include <unistd.h>
10#include <errno.h>
11
12volatile int X;
13int stop;
14
15static void handler(int sig) {
16 (void)sig;
17 if (X != 0)
18 printf(format: "bad");
19}
20
21static void* busy(void *p) {
22 while (__atomic_load_n(&stop, __ATOMIC_RELAXED) == 0) {
23 }
24 return 0;
25}
26
27static void* reset(void *p) {
28 struct sigaction act = {};
29 for (int i = 0; i < 1000000; i++) {
30 act.sa_handler = &handler;
31 if (sigaction(SIGPROF, act: &act, oact: 0)) {
32 perror(s: "sigaction");
33 exit(status: 1);
34 }
35 act.sa_handler = SIG_IGN;
36 if (sigaction(SIGPROF, act: &act, oact: 0)) {
37 perror(s: "sigaction");
38 exit(status: 1);
39 }
40 }
41 return 0;
42}
43
44int main() {
45 struct sigaction act = {};
46 act.sa_handler = SIG_IGN;
47 if (sigaction(SIGPROF, act: &act, oact: 0)) {
48 perror(s: "sigaction");
49 exit(status: 1);
50 }
51
52 itimerval t;
53 t.it_value.tv_sec = 0;
54 t.it_value.tv_usec = 10;
55 t.it_interval = t.it_value;
56 if (setitimer(ITIMER_PROF, new: &t, old: 0)) {
57 perror(s: "setitimer");
58 exit(status: 1);
59 }
60
61 pthread_t th[2];
62 pthread_create(newthread: &th[0], attr: 0, start_routine: busy, arg: 0);
63 pthread_create(newthread: &th[1], attr: 0, start_routine: reset, arg: 0);
64
65 pthread_join(th: th[1], thread_return: 0);
66 __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);
67 pthread_join(th: th[0], thread_return: 0);
68
69 fprintf(stderr, format: "DONE\n");
70 return 0;
71}
72
73// CHECK-NOT: WARNING: ThreadSanitizer:
74// CHECK: DONE
75// CHECK-NOT: WARNING: ThreadSanitizer:
76

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