brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 03ac639 Raw
65 lines · cpp
1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s2 3// Test that a signal is not delivered when it is blocked.4 5// FIXME: Very flaky on PPC with COMPILER_RT_DEBUG.6// https://github.com/google/sanitizers/issues/17927// UNSUPPORTED: !compiler-rt-optimized && ppc8 9#include "test.h"10#include <semaphore.h>11#include <signal.h>12#include <errno.h>13 14int stop;15sig_atomic_t signal_blocked;16 17void handler(int signum) {18  if (signal_blocked) {19    fprintf(stderr, "signal arrived when blocked\n");20    exit(1);21  }22}23 24void *thread(void *arg) {25  sigset_t myset;26  sigemptyset(&myset);27  sigaddset(&myset, SIGUSR1);28  while (!__atomic_load_n(&stop, __ATOMIC_RELAXED)) {29    usleep(1);30    if (pthread_sigmask(SIG_BLOCK, &myset, 0)) {31      fprintf(stderr, "pthread_sigmask failed %d\n", errno);32      exit(1);33    }34    signal_blocked = 1;35    usleep(1);36    signal_blocked = 0;37    if (pthread_sigmask(SIG_UNBLOCK, &myset, 0)) {38      fprintf(stderr, "pthread_sigmask failed %d\n", errno);39      exit(1);40    }41  }42  return 0;43}44 45int main(int argc, char** argv) {46  struct sigaction act = {};47  act.sa_handler = &handler;48  if (sigaction(SIGUSR1, &act, 0)) {49    fprintf(stderr, "sigaction failed %d\n", errno);50    return 1;51  }52  pthread_t th;53  pthread_create(&th, 0, thread, 0);54  for (int i = 0; i < 100000; i++)55    pthread_kill(th, SIGUSR1);56  __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);57  pthread_join(th, 0);58  fprintf(stderr, "DONE\n");59  return 0;60}61 62// CHECK-NOT: ThreadSanitizer CHECK63// CHECK-NOT: WARNING: ThreadSanitizer:64// CHECK: DONE65