1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// UNSUPPORTED: darwin
3#include "test.h"
4#include <signal.h>
5#include <sys/types.h>
6#include <sys/time.h>
7#include <errno.h>
8
9volatile int X;
10
11static void handler(int sig) {
12 (void)sig;
13 if (X != 42)
14 printf(format: "bad");
15}
16
17static void* thr(void *p) {
18 for (int i = 0; i != 1000; i++)
19 usleep(useconds: 1000); // process signals
20 return 0;
21}
22
23int main() {
24 const int kThreads = 10;
25 pthread_t th[kThreads];
26 for (int i = 0; i < kThreads; i++)
27 pthread_create(newthread: &th[i], attr: 0, start_routine: thr, arg: 0);
28
29 X = 42;
30
31 struct sigaction act = {};
32 act.sa_handler = &handler;
33 if (sigaction(SIGPROF, act: &act, oact: 0)) {
34 perror(s: "sigaction");
35 exit(status: 1);
36 }
37
38 itimerval t;
39 t.it_value.tv_sec = 0;
40 t.it_value.tv_usec = 10;
41 t.it_interval = t.it_value;
42 if (setitimer(ITIMER_PROF, new: &t, old: 0)) {
43 perror(s: "setitimer");
44 exit(status: 1);
45 }
46
47 for (int i = 0; i < kThreads; i++)
48 pthread_join(th: th[i], thread_return: 0);
49
50 fprintf(stderr, format: "DONE\n");
51 return 0;
52}
53
54// CHECK-NOT: WARNING: ThreadSanitizer:
55// CHECK: DONE
56// CHECK-NOT: WARNING: ThreadSanitizer:
57

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