1 // RUN: %clang_tsan %s -lstdc++ -o %t && %run %t 2>&1 | FileCheck %s
10 #include <condition_variable>
13 std::mutex sampler_mutex
; //dummy mutex to lock in the thread we spawn.
14 std::mutex done_mutex
; // guards the cv and done variables.
15 std::condition_variable cv
;
17 std::atomic
<bool> spin
= true;
19 void *ThreadFunc(void *x
) {
22 std::lock_guard
<std::mutex
> guard(sampler_mutex
);
23 // Mutex is released at the end
29 static void SigprofHandler(int signal
, siginfo_t
*info
, void *context
) {
30 // Assuming we did some work, change the variable to let the main thread
31 // know that we are done.
33 std::unique_lock
<std::mutex
> lck(done_mutex
);
40 alarm(60); // Kill the test if it hangs.
42 // Install the signal handler
44 sa
.sa_sigaction
= SigprofHandler
;
45 sigemptyset(&sa
.sa_mask
);
46 sa
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
47 if (sigaction(SIGPROF
, &sa
, 0) != 0) {
48 fprintf(stderr
, "failed to install signal handler\n");
52 // Spawn a thread that will just loop and get the mutex lock:
54 pthread_create(&thread
, NULL
, ThreadFunc
, NULL
);
57 // Lock the mutex before sending the signal
58 std::lock_guard
<std::mutex
> guard(sampler_mutex
);
59 // From now on thread 1 will be waiting for the lock
61 // Send the SIGPROF signal to thread.
62 int r
= pthread_kill(thread
, SIGPROF
);
65 // Wait until signal handler sends the data.
66 std::unique_lock
lk(done_mutex
);
67 cv
.wait(lk
, [] { return done
; });
69 // We got the done variable from the signal handler. Exiting successfully.
70 fprintf(stderr
, "PASS\n");
73 // Wait for thread to prevent it from spinning on a released mutex.
75 pthread_join(thread
, nullptr);
78 // CHECK-NOT: WARNING: ThreadSanitizer: