1// RUN: %clangxx_tsan %s -o %t && %run %t 2>&1 | FileCheck %s
2// UNSUPPORTED: darwin
3
4// It's very flaky on PPC with COMPILER_RT_DEBUG.
5// UNSUPPORTED: !compiler-rt-optimized && ppc
6
7// Test case for https://github.com/google/sanitizers/issues/1540
8
9#include <errno.h>
10#include <pthread.h>
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <sys/time.h>
15#include <sys/types.h>
16#include <unistd.h>
17
18volatile int X;
19
20static void handler(int sig) {
21 (void)sig;
22 if (X != 0)
23 printf(format: "bad");
24}
25
26static void *thr1(void *p) {
27 sleep(seconds: 1);
28 return 0;
29}
30
31static void *thr(void *p) {
32 pthread_t th[10];
33 for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
34 pthread_create(newthread: &th[i], attr: 0, start_routine: thr1, arg: 0);
35 for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
36 pthread_join(th: th[i], thread_return: 0);
37 return 0;
38}
39
40int main() {
41 struct sigaction act = {};
42 act.sa_handler = &handler;
43 if (sigaction(SIGPROF, act: &act, oact: 0)) {
44 perror(s: "sigaction");
45 exit(status: 1);
46 }
47
48 itimerval t;
49 t.it_value.tv_sec = 0;
50 t.it_value.tv_usec = 10;
51 t.it_interval = t.it_value;
52 if (setitimer(ITIMER_PROF, new: &t, old: 0)) {
53 perror(s: "setitimer");
54 exit(status: 1);
55 }
56
57 pthread_t th[100];
58 for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
59 pthread_create(newthread: &th[i], attr: 0, start_routine: thr, arg: 0);
60 for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
61 pthread_join(th: th[i], thread_return: 0);
62
63 fprintf(stderr, format: "DONE\n");
64 return 0;
65}
66
67// CHECK-NOT: WARNING: ThreadSanitizer:
68// CHECK: DONE
69// CHECK-NOT: WARNING: ThreadSanitizer:
70

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