Perform a major refactoring of vCont-threads tests in order to attempt to improve their stability and performance. Split test_vCont_run_subset_of_threads() into smaller test cases, and split the whole suite into two files: one for signal-related tests, the running-subset-of tests. Eliminate output_match checks entirely, as they are fragile to fragmentation of output. Instead, for the initial thread list capture raise an explicit SIGINT from inside the test program, and for the remaining output let the test program run until exit, and check all the captured output afterwards. For resume tests, capture the LLDB's thread view before and after starting new threads in order to determine the IDs corresponding to subthreads rather than relying on program output for that. Add a mutex for output to guarantee serialization. A barrier is used to guarantee that all threads start before SIGINT, and an atomic bool is used to delay prints from happening until after SIGINT. Call std::this_thread::yield() to reduce the risk of one of the threads not being run. This fixes the test hangs on FreeBSD. Hopefully, it will also fix all the flakiness on buildbots. Thanks to Pavel Labath for figuring out why the original version did not work on Debian. Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.llvm.org/D129012
79 lines
2.0 KiB
C++
79 lines
2.0 KiB
C++
#include "pseudo_barrier.h"
|
|
#include "thread.h"
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cinttypes>
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
pseudo_barrier_t barrier;
|
|
std::mutex print_mutex;
|
|
std::atomic<bool> can_work = ATOMIC_VAR_INIT(false);
|
|
thread_local volatile sig_atomic_t can_exit_now = false;
|
|
|
|
static void sigint_handler(int signo) {}
|
|
|
|
static void sigusr1_handler(int signo) {
|
|
std::lock_guard<std::mutex> lock{print_mutex};
|
|
std::printf("received SIGUSR1 on thread id: %" PRIx64 "\n", get_thread_id());
|
|
can_exit_now = true;
|
|
}
|
|
|
|
static void thread_func() {
|
|
// this ensures that all threads start before we stop
|
|
pseudo_barrier_wait(barrier);
|
|
|
|
// wait till the main thread indicates that we can go
|
|
// (note: using a mutex here causes hang on FreeBSD when another thread
|
|
// is suspended)
|
|
while (!can_work.load())
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
// the mutex guarantees that two writes don't get interspersed
|
|
{
|
|
std::lock_guard<std::mutex> lock{print_mutex};
|
|
std::printf("thread %" PRIx64 " running\n", get_thread_id());
|
|
}
|
|
|
|
// give other threads a fair chance to run
|
|
for (int i = 0; i < 5; ++i) {
|
|
std::this_thread::yield();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
if (can_exit_now)
|
|
return;
|
|
}
|
|
|
|
// if we didn't get signaled, terminate the program explicitly.
|
|
_exit(0);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
int num = atoi(argv[1]);
|
|
|
|
pseudo_barrier_init(barrier, num + 1);
|
|
|
|
signal(SIGINT, sigint_handler);
|
|
signal(SIGUSR1, sigusr1_handler);
|
|
|
|
std::vector<std::thread> threads;
|
|
for (int i = 0; i < num; ++i)
|
|
threads.emplace_back(thread_func);
|
|
|
|
// use the barrier to make sure all threads start before we stop
|
|
pseudo_barrier_wait(barrier);
|
|
std::raise(SIGINT);
|
|
|
|
// allow the threads to work
|
|
can_work.store(true);
|
|
|
|
for (std::thread &thread : threads)
|
|
thread.join();
|
|
return 0;
|
|
}
|