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