Skip to content

Fix flaky test: signal_in_mutex_lock.cpp #92587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions compiler-rt/test/tsan/signal_in_mutex_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <signal.h>
#include <stdio.h>

#include <atomic>
#include <cassert>
#include <condition_variable>
#include <mutex>
Expand All @@ -13,9 +14,10 @@ std::mutex sampler_mutex; //dummy mutex to lock in the thread we spawn.
std::mutex done_mutex; // guards the cv and done variables.
std::condition_variable cv;
bool done = false;
std::atomic<bool> spin = true;

void *ThreadFunc(void *x) {
while (true) {
while (spin) {
// Lock the mutex
std::lock_guard<std::mutex> guard(sampler_mutex);
// Mutex is released at the end
Expand Down Expand Up @@ -51,20 +53,26 @@ int main() {
pthread_t thread;
pthread_create(&thread, NULL, ThreadFunc, NULL);

// Lock the mutex before sending the signal
std::lock_guard<std::mutex> guard(sampler_mutex);
// From now on thread 1 will be waiting for the lock
{
// Lock the mutex before sending the signal
std::lock_guard<std::mutex> guard(sampler_mutex);
// From now on thread 1 will be waiting for the lock

// Send the SIGPROF signal to thread.
int r = pthread_kill(thread, SIGPROF);
assert(r == 0);
// Send the SIGPROF signal to thread.
int r = pthread_kill(thread, SIGPROF);
assert(r == 0);

// Wait until signal handler sends the data.
std::unique_lock lk(done_mutex);
cv.wait(lk, [] { return done; });
// Wait until signal handler sends the data.
std::unique_lock lk(done_mutex);
cv.wait(lk, [] { return done; });

// We got the done variable from the signal handler. Exiting successfully.
fprintf(stderr, "PASS\n");
}

// We got the done variable from the signal handler. Exiting successfully.
fprintf(stderr, "PASS\n");
// Wait for thread to prevent it from spinning on a released mutex.
spin = false;
pthread_join(thread, nullptr);
}

// CHECK-NOT: WARNING: ThreadSanitizer:
Expand Down
Loading