1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot // UNSUPPORTED: darwin
3*7c3d14c8STreehugger Robot #include <pthread.h>
4*7c3d14c8STreehugger Robot #include <stdio.h>
5*7c3d14c8STreehugger Robot #include <stdlib.h>
6*7c3d14c8STreehugger Robot #include <signal.h>
7*7c3d14c8STreehugger Robot #include <sys/types.h>
8*7c3d14c8STreehugger Robot #include <sys/time.h>
9*7c3d14c8STreehugger Robot #include <unistd.h>
10*7c3d14c8STreehugger Robot #include <errno.h>
11*7c3d14c8STreehugger Robot
12*7c3d14c8STreehugger Robot // Test synchronization in signal handled within IgnoreSync region.
13*7c3d14c8STreehugger Robot
14*7c3d14c8STreehugger Robot extern "C" void AnnotateIgnoreSyncBegin(const char *f, int l);
15*7c3d14c8STreehugger Robot extern "C" void AnnotateIgnoreSyncEnd(const char *f, int l);
16*7c3d14c8STreehugger Robot
17*7c3d14c8STreehugger Robot const int kSignalCount = 500;
18*7c3d14c8STreehugger Robot
19*7c3d14c8STreehugger Robot __thread int process_signals;
20*7c3d14c8STreehugger Robot int signals_handled;
21*7c3d14c8STreehugger Robot int done;
22*7c3d14c8STreehugger Robot int ready[kSignalCount];
23*7c3d14c8STreehugger Robot long long data[kSignalCount];
24*7c3d14c8STreehugger Robot
handler(int sig)25*7c3d14c8STreehugger Robot static void handler(int sig) {
26*7c3d14c8STreehugger Robot if (!__atomic_load_n(&process_signals, __ATOMIC_RELAXED))
27*7c3d14c8STreehugger Robot return;
28*7c3d14c8STreehugger Robot int pos = signals_handled++;
29*7c3d14c8STreehugger Robot if (pos >= kSignalCount)
30*7c3d14c8STreehugger Robot return;
31*7c3d14c8STreehugger Robot data[pos] = pos;
32*7c3d14c8STreehugger Robot __atomic_store_n(&ready[pos], 1, __ATOMIC_RELEASE);
33*7c3d14c8STreehugger Robot }
34*7c3d14c8STreehugger Robot
thr(void * p)35*7c3d14c8STreehugger Robot static void* thr(void *p) {
36*7c3d14c8STreehugger Robot AnnotateIgnoreSyncBegin(__FILE__, __LINE__);
37*7c3d14c8STreehugger Robot __atomic_store_n(&process_signals, 1, __ATOMIC_RELAXED);
38*7c3d14c8STreehugger Robot while (!__atomic_load_n(&done, __ATOMIC_RELAXED)) {
39*7c3d14c8STreehugger Robot }
40*7c3d14c8STreehugger Robot AnnotateIgnoreSyncEnd(__FILE__, __LINE__);
41*7c3d14c8STreehugger Robot return 0;
42*7c3d14c8STreehugger Robot }
43*7c3d14c8STreehugger Robot
main()44*7c3d14c8STreehugger Robot int main() {
45*7c3d14c8STreehugger Robot struct sigaction act = {};
46*7c3d14c8STreehugger Robot act.sa_handler = handler;
47*7c3d14c8STreehugger Robot if (sigaction(SIGPROF, &act, 0)) {
48*7c3d14c8STreehugger Robot perror("sigaction");
49*7c3d14c8STreehugger Robot exit(1);
50*7c3d14c8STreehugger Robot }
51*7c3d14c8STreehugger Robot itimerval t;
52*7c3d14c8STreehugger Robot t.it_value.tv_sec = 0;
53*7c3d14c8STreehugger Robot t.it_value.tv_usec = 10;
54*7c3d14c8STreehugger Robot t.it_interval = t.it_value;
55*7c3d14c8STreehugger Robot if (setitimer(ITIMER_PROF, &t, 0)) {
56*7c3d14c8STreehugger Robot perror("setitimer");
57*7c3d14c8STreehugger Robot exit(1);
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot
60*7c3d14c8STreehugger Robot pthread_t th;
61*7c3d14c8STreehugger Robot pthread_create(&th, 0, thr, 0);
62*7c3d14c8STreehugger Robot for (int pos = 0; pos < kSignalCount; pos++) {
63*7c3d14c8STreehugger Robot while (__atomic_load_n(&ready[pos], __ATOMIC_ACQUIRE) == 0) {
64*7c3d14c8STreehugger Robot }
65*7c3d14c8STreehugger Robot if (data[pos] != pos) {
66*7c3d14c8STreehugger Robot printf("at pos %d, expect %d, got %lld\n", pos, pos, data[pos]);
67*7c3d14c8STreehugger Robot exit(1);
68*7c3d14c8STreehugger Robot }
69*7c3d14c8STreehugger Robot }
70*7c3d14c8STreehugger Robot __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
71*7c3d14c8STreehugger Robot pthread_join(th, 0);
72*7c3d14c8STreehugger Robot fprintf(stderr, "DONE\n");
73*7c3d14c8STreehugger Robot return 0;
74*7c3d14c8STreehugger Robot }
75*7c3d14c8STreehugger Robot
76*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING: ThreadSanitizer:
77*7c3d14c8STreehugger Robot // CHECK: DONE
78