Home | History | Annotate | Download | only in tsan
      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 
      9 volatile int X;
     10 
     11 static void handler(int sig) {
     12   (void)sig;
     13   if (X != 42)
     14     printf("bad");
     15 }
     16 
     17 static void* thr(void *p) {
     18   for (int i = 0; i != 1000; i++)
     19     usleep(1000);  // process signals
     20   return 0;
     21 }
     22 
     23 int main() {
     24   const int kThreads = 10;
     25   pthread_t th[kThreads];
     26   for (int i = 0; i < kThreads; i++)
     27     pthread_create(&th[i], 0, thr, 0);
     28 
     29   X = 42;
     30 
     31   struct sigaction act = {};
     32   act.sa_handler = &handler;
     33   if (sigaction(SIGPROF, &act, 0)) {
     34     perror("sigaction");
     35     exit(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, &t, 0)) {
     43     perror("setitimer");
     44     exit(1);
     45   }
     46 
     47   for (int i = 0; i < kThreads; i++)
     48     pthread_join(th[i], 0);
     49 
     50   fprintf(stderr, "DONE\n");
     51   return 0;
     52 }
     53 
     54 // CHECK-NOT: WARNING: ThreadSanitizer:
     55 // CHECK: DONE
     56 // CHECK-NOT: WARNING: ThreadSanitizer:
     57