diff options
| author | Martin Ridgers <[email protected]> | 2021-11-16 17:01:45 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-11-16 17:01:45 +0100 |
| commit | 0520342c5aa894d3174207512a2b516f717ba1c5 (patch) | |
| tree | 5b17800a813480369fdbee432d00a859c2ee8cb9 /zencore/thread.cpp | |
| parent | Adding missing check to see if the process is the fork or not (diff) | |
| download | zen-0520342c5aa894d3174207512a2b516f717ba1c5.tar.xz zen-0520342c5aa894d3174207512a2b516f717ba1c5.zip | |
Implemented ProcessMonitor for POSIX platforms
Diffstat (limited to 'zencore/thread.cpp')
| -rw-r--r-- | zencore/thread.cpp | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/zencore/thread.cpp b/zencore/thread.cpp index 72148f57d..335f425b5 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -792,8 +792,6 @@ CreateProcResult CreateProc( ////////////////////////////////////////////////////////////////////////// -#if ZEN_PLATFORM_WINDOWS - ProcessMonitor::ProcessMonitor() { } @@ -802,9 +800,11 @@ ProcessMonitor::~ProcessMonitor() { RwLock::ExclusiveLockScope _(m_Lock); - for (HANDLE& Proc : m_ProcessHandles) + for (HandleType& Proc : m_ProcessHandles) { +#if ZEN_PLATFORM_WINDOWS CloseHandle(Proc); +#endif Proc = 0; } } @@ -816,24 +816,34 @@ ProcessMonitor::IsRunning() bool FoundOne = false; - for (HANDLE& Proc : m_ProcessHandles) + for (HandleType& Proc : m_ProcessHandles) { + bool ProcIsActive; + +#if ZEN_PLATFORM_WINDOWS DWORD ExitCode = 0; GetExitCodeProcess(Proc, &ExitCode); - if (ExitCode != STILL_ACTIVE) + ProcIsActive = (ExitCode != STILL_ACTIVE); + if (!ProcIsActive) { CloseHandle(Proc); - Proc = 0; } - else +#else + int Pid = int(intptr_t(Proc)); + ProcIsActive = IsProcessRunning(Pid); +#endif + + if (!ProcIsActive) { - // Still alive - FoundOne = true; + Proc = 0; } + + // Still alive + FoundOne |= ProcIsActive; } - std::erase_if(m_ProcessHandles, [](HANDLE Handle) { return Handle == 0; }); + std::erase_if(m_ProcessHandles, [](HandleType Handle) { return Handle == 0; }); return FoundOne; } @@ -841,7 +851,13 @@ ProcessMonitor::IsRunning() void ProcessMonitor::AddPid(int Pid) { - HANDLE ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, Pid); + HandleType ProcessHandle; + +#if ZEN_PLATFORM_WINDOWS + ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, Pid); +#else + ProcessHandle = HandleType(intptr_t(Pid)); +#endif if (ProcessHandle) { @@ -857,8 +873,6 @@ ProcessMonitor::IsActive() const return m_ProcessHandles.empty() == false; } -#endif // ZEN_PLATFORM_WINDOWS - ////////////////////////////////////////////////////////////////////////// bool |