From 1b117ee64ae3ec6735fe95a998ef977cef9aa8ac Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Tue, 9 Nov 2021 16:53:26 +0100 Subject: Implemented ProcessHandle::Wait() for Linux --- zencore/thread.cpp | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'zencore/thread.cpp') diff --git a/zencore/thread.cpp b/zencore/thread.cpp index ba26fef36..a90d31a6c 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -15,6 +15,7 @@ # include # include +# include # include #endif @@ -411,6 +412,9 @@ ProcessHandle::Reset() bool ProcessHandle::Wait(int TimeoutMs) { + using namespace std::literals; + +#if ZEN_PLATFORM_WINDOWS const DWORD Timeout = (TimeoutMs < 0) ? INFINITE : TimeoutMs; const DWORD WaitResult = WaitForSingleObject(m_ProcessHandle, Timeout); @@ -424,10 +428,35 @@ ProcessHandle::Wait(int TimeoutMs) return false; case WAIT_FAILED: - // What might go wrong here, and what is meaningful to act on? - using namespace std::literals; - ThrowLastError("Process::Wait failed"sv); + break; } +#elif ZEN_PLATFORM_LINUX + const int SleepMs = 20; + timespec SleepTime = { 0, SleepMs * 1000 * 1000 }; + for (int i = 0; ; i += SleepMs) + { + if (i >= TimeoutMs) + { + return false; + } + + if (kill(m_Pid, 0) < 0) + { + if (zen::GetLastError() == ESRCH) + { + return true; + } + break; + } + + nanosleep(&SleepTime, nullptr); + } +#else +# error Check kill() on this platform +#endif + + // What might go wrong here, and what is meaningful to act on? + ThrowLastError("Process::Wait failed"sv); return false; } -- cgit v1.2.3