diff options
Diffstat (limited to 'zencore/thread.cpp')
| -rw-r--r-- | zencore/thread.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
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 <pthread.h> # include <signal.h> +# include <time.h> # include <unistd.h> #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; } |