| 1 | // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
| 2 | // UNSUPPORTED: darwin |
| 3 | |
| 4 | // FIXME: Very flaky on PPC with COMPILER_RT_DEBUG. |
| 5 | // https://github.com/google/sanitizers/issues/1792 |
| 6 | // UNSUPPORTED: !compiler-rt-optimized && ppc |
| 7 | |
| 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 | |
| 17 | volatile int X; |
| 18 | int stop; |
| 19 | |
| 20 | static void handler(int sig) { |
| 21 | (void)sig; |
| 22 | if (X != 0) |
| 23 | printf(format: "bad" ); |
| 24 | } |
| 25 | |
| 26 | static void* busy(void *p) { |
| 27 | while (__atomic_load_n(&stop, __ATOMIC_RELAXED) == 0) { |
| 28 | } |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static 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: &act, oact: 0)) { |
| 37 | perror(s: "sigaction" ); |
| 38 | exit(status: 1); |
| 39 | } |
| 40 | act.sa_handler = SIG_IGN; |
| 41 | if (sigaction(SIGPROF, act: &act, oact: 0)) { |
| 42 | perror(s: "sigaction" ); |
| 43 | exit(status: 1); |
| 44 | } |
| 45 | } |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int main() { |
| 50 | struct sigaction act = {}; |
| 51 | act.sa_handler = SIG_IGN; |
| 52 | if (sigaction(SIGPROF, act: &act, oact: 0)) { |
| 53 | perror(s: "sigaction" ); |
| 54 | exit(status: 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, new: &t, old: 0)) { |
| 62 | perror(s: "setitimer" ); |
| 63 | exit(status: 1); |
| 64 | } |
| 65 | |
| 66 | pthread_t th[2]; |
| 67 | pthread_create(newthread: &th[0], attr: 0, start_routine: busy, arg: 0); |
| 68 | pthread_create(newthread: &th[1], attr: 0, start_routine: reset, arg: 0); |
| 69 | |
| 70 | pthread_join(th: th[1], thread_return: 0); |
| 71 | __atomic_store_n(&stop, 1, __ATOMIC_RELAXED); |
| 72 | pthread_join(th: th[0], thread_return: 0); |
| 73 | |
| 74 | fprintf(stderr, format: "DONE\n" ); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | // CHECK-NOT: WARNING: ThreadSanitizer: |
| 79 | // CHECK: DONE |
| 80 | // CHECK-NOT: WARNING: ThreadSanitizer: |
| 81 | |