[clang-format] Rename ExportBlockIndentation -> IndentExportBlock (#123493)
[llvm-project.git] / compiler-rt / test / tsan / signal_in_mutex_lock.cpp
blob49b8536539389defc2f01f36c7511ed2380a863f
1 // RUN: %clang_tsan %s -lstdc++ -o %t && %run %t 2>&1 | FileCheck %s
3 #include "test.h"
4 #include <pthread.h>
5 #include <signal.h>
6 #include <stdio.h>
8 #include <atomic>
9 #include <cassert>
10 #include <condition_variable>
11 #include <mutex>
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;
16 bool done = false;
17 std::atomic<bool> spin = true;
19 void *ThreadFunc(void *x) {
20 while (spin) {
21 // Lock the mutex
22 std::lock_guard<std::mutex> guard(sampler_mutex);
23 // Mutex is released at the end
26 return nullptr;
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);
34 done = true;
35 cv.notify_one();
39 int main() {
40 alarm(60); // Kill the test if it hangs.
42 // Install the signal handler
43 struct sigaction sa;
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");
49 abort();
52 // Spawn a thread that will just loop and get the mutex lock:
53 pthread_t thread;
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);
63 assert(r == 0);
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.
74 spin = false;
75 pthread_join(thread, nullptr);
78 // CHECK-NOT: WARNING: ThreadSanitizer:
79 // CHECK: PASS