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