diff options
| author | Stefan Boberg <[email protected]> | 2021-09-16 21:04:35 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-16 21:04:35 +0200 |
| commit | 4b1b3edc21acf3b19649a85e49a57464bf169314 (patch) | |
| tree | 4a2ccf74343c57d8c3698efb8d76bf549227da08 | |
| parent | Minor CbPackage serialization tweaks (diff) | |
| download | zen-4b1b3edc21acf3b19649a85e49a57464bf169314.tar.xz zen-4b1b3edc21acf3b19649a85e49a57464bf169314.zip | |
Added ProcessMonitor class, which is used to monitor a number of sponsor processes, to control Zen instance lifetime
| -rw-r--r-- | zencore/include/zencore/thread.h | 27 | ||||
| -rw-r--r-- | zencore/thread.cpp | 75 |
2 files changed, 101 insertions, 1 deletions
diff --git a/zencore/include/zencore/thread.h b/zencore/include/zencore/thread.h index b18da6031..0e34d6518 100644 --- a/zencore/include/zencore/thread.h +++ b/zencore/include/zencore/thread.h @@ -8,6 +8,8 @@ # include <shared_mutex> #endif +#include <vector> + namespace zen { /** @@ -93,6 +95,7 @@ public: ZENCORE_API void Set(); ZENCORE_API void Reset(); ZENCORE_API bool Wait(int TimeoutMs = -1); + ZENCORE_API void Close(); protected: explicit Event(void* EventHandle) : m_EventHandle(EventHandle) {} @@ -125,7 +128,7 @@ private: }; /** Basic process abstraction - */ + */ class ProcessHandle { public: @@ -150,6 +153,28 @@ private: int m_Pid = 0; }; +/** Process monitor - monitors a list of running processes via polling + + Intended to be used to monitor a set of "sponsor" processes, where + we need to determine when none of them remain alive + + */ + +class ProcessMonitor +{ +public: + ProcessMonitor(); + ~ProcessMonitor(); + + ZENCORE_API bool IsRunning(); + ZENCORE_API void AddPid(int Pid); + ZENCORE_API bool IsActive() const; + +private: + mutable RwLock m_Lock; + std::vector<void*> m_ProcessHandles; +}; + ZENCORE_API bool IsProcessRunning(int pid); ZENCORE_API int GetCurrentProcessId(); diff --git a/zencore/thread.cpp b/zencore/thread.cpp index 598466bb4..ee00d38d4 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -76,6 +76,13 @@ Event::Reset() ResetEvent(m_EventHandle); } +void +Event::Close() +{ + CloseHandle(m_EventHandle); + m_EventHandle = nullptr; +} + bool Event::Wait(int TimeoutMs) { @@ -168,6 +175,7 @@ ProcessHandle::Initialize(void* ProcessHandle) ZEN_ASSERT(m_ProcessHandle == nullptr); // TODO: perform some debug verification here to verify it's a valid handle? m_ProcessHandle = ProcessHandle; + m_Pid = GetProcessId(m_ProcessHandle); } ProcessHandle::~ProcessHandle() @@ -255,6 +263,73 @@ ProcessHandle::Wait(int TimeoutMs) return false; } +////////////////////////////////////////////////////////////////////////// + +ProcessMonitor::ProcessMonitor() +{ +} + +ProcessMonitor::~ProcessMonitor() +{ + RwLock::ExclusiveLockScope _(m_Lock); + + for (HANDLE& Proc : m_ProcessHandles) + { + CloseHandle(Proc); + Proc = 0; + } +} + +bool +ProcessMonitor::IsRunning() +{ + RwLock::ExclusiveLockScope _(m_Lock); + + bool FoundOne = false; + + for (HANDLE& Proc : m_ProcessHandles) + { + DWORD ExitCode = 0; + GetExitCodeProcess(Proc, &ExitCode); + + if (ExitCode != STILL_ACTIVE) + { + CloseHandle(Proc); + Proc = 0; + } + else + { + // Still alive + FoundOne = true; + } + } + + std::erase_if(m_ProcessHandles, [](HANDLE Handle) { return Handle == 0; }); + + return FoundOne; +} + +void +ProcessMonitor::AddPid(int Pid) +{ + HANDLE ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, Pid); + + if (ProcessHandle) + { + RwLock::ExclusiveLockScope _(m_Lock); + m_ProcessHandles.push_back(ProcessHandle); + } +} + +bool +ProcessMonitor::IsActive() const +{ + RwLock::SharedLockScope _(m_Lock); + return m_ProcessHandles.empty() == false; +} + +////////////////////////////////////////////////////////////////////////// + bool IsProcessRunning(int pid) { |