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