Files
TheRock/compiler/amd-llvm/lldb/test/API/tools/lldb-server/vCont-threads/main.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

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;
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;
}