aboutsummaryrefslogtreecommitdiff
path: root/zencore/thread.cpp
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2021-11-09 16:53:26 +0100
committerMartin Ridgers <[email protected]>2021-11-09 16:53:26 +0100
commit1b117ee64ae3ec6735fe95a998ef977cef9aa8ac (patch)
tree08136622a19d38ce482a1c8a50fe37ba81389e78 /zencore/thread.cpp
parentProcessHandle::Reset() for Linux (diff)
downloadzen-1b117ee64ae3ec6735fe95a998ef977cef9aa8ac.tar.xz
zen-1b117ee64ae3ec6735fe95a998ef977cef9aa8ac.zip
Implemented ProcessHandle::Wait() for Linux
Diffstat (limited to 'zencore/thread.cpp')
-rw-r--r--zencore/thread.cpp35
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;
}