aboutsummaryrefslogtreecommitdiff
path: root/zencore
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2021-09-16 21:19:14 +0200
committerPer Larsson <[email protected]>2021-09-16 21:19:14 +0200
commitcae9f697a13840eb87cb9f6fc32eb80e3a65b29a (patch)
tree721af351316d5a1fe2ac022fad2a4e9017b043ae /zencore
parentzcache - minor cleanup. (diff)
parentMerge branch 'main' of https://github.com/EpicGames/zen (diff)
downloadzen-cae9f697a13840eb87cb9f6fc32eb80e3a65b29a.tar.xz
zen-cae9f697a13840eb87cb9f6fc32eb80e3a65b29a.zip
Merge branch 'main' of https://github.com/EpicGames/zen
Diffstat (limited to 'zencore')
-rw-r--r--zencore/include/zencore/logging.h7
-rw-r--r--zencore/include/zencore/thread.h27
-rw-r--r--zencore/thread.cpp75
3 files changed, 108 insertions, 1 deletions
diff --git a/zencore/include/zencore/logging.h b/zencore/include/zencore/logging.h
index 8e6b3a244..e98509bf8 100644
--- a/zencore/include/zencore/logging.h
+++ b/zencore/include/zencore/logging.h
@@ -80,3 +80,10 @@ using zen::Log;
using namespace std::literals; \
Log().critical(fmtstr##sv, __VA_ARGS__); \
} while (false)
+
+#define ZEN_CONSOLE(fmtstr, ...) \
+ do \
+ { \
+ using namespace std::literals; \
+ ConsoleLog().info(fmtstr##sv, __VA_ARGS__); \
+ } while (false)
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)
{