Skip to content

Commit

Permalink
ThreadManager: Add compability with MSYS
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Dec 6, 2024
1 parent e46d6c1 commit 53499b3
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/ThreadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@
#endif

#ifdef _WIN32
#include <windows.h> // TerminateThread
#include <windows.h> // TerminateThread

#define THREAD_FORCE_KILL_SUPPORTED
#endif

#ifdef THREAD_FORCE_KILL_SUPPORTED
void kill_thread(std::thread::native_handle_type handle) {
#ifdef _POSIX_C_SOURCE
pthread_kill(handle, SIGUSR1);
#endif
#ifdef _WIN32
#ifdef _MSC_VER
TerminateThread(handle, 0);
#else /* _MSC_VER */
HANDLE hThread =
OpenThread(THREAD_TERMINATE, FALSE, static_cast<DWORD>(handle));
if (hThread) {
TerminateThread(hThread, 0);
CloseHandle(hThread); // Always close the handle after use
}
#endif // _MSC_VER
#endif // _WIN32
}
#endif

constexpr std::chrono::seconds kShutdownDelay(5);

void ThreadManager::destroy() {
Expand All @@ -40,16 +60,13 @@ void ThreadManager::destroy() {
std::unique_lock<std::mutex> lock(mutex);
if (!condvar.wait_for(lock, kShutdownDelay,
[this] { return barrier.try_wait(); })) {
LOG(ERROR) << fmt::format("Timed out waiting for threads to finish (waited {})", kShutdownDelay);
LOG(ERROR) << fmt::format(
"Timed out waiting for threads to finish (waited {})",
kShutdownDelay);
for (const auto& thread : kControllers) {
if (thread.second && thread.second->running()) {
LOG(ERROR) << fmt::format("Killing thread {}", thread.first);
#ifdef _POSIX_C_SOURCE
pthread_kill(thread.second->threadP.native_handle(), SIGUSR1);
#endif
#ifdef _WIN32
TerminateThread(thread.second->threadP.native_handle(), 0);
#endif
kill_thread(thread.second->threadP.native_handle());
}
}
}
Expand Down

0 comments on commit 53499b3

Please sign in to comment.