aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/process/exitwatcher.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenutil/process/exitwatcher.h')
-rw-r--r--src/zenutil/process/exitwatcher.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/zenutil/process/exitwatcher.h b/src/zenutil/process/exitwatcher.h
new file mode 100644
index 000000000..24906d7d0
--- /dev/null
+++ b/src/zenutil/process/exitwatcher.h
@@ -0,0 +1,48 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/process.h>
+#include <zencore/zencore.h>
+
+#include <functional>
+#include <memory>
+
+namespace asio {
+class io_context;
+}
+
+namespace zen {
+
+/// Async process exit watcher.
+///
+/// Uses platform-specific mechanisms for scalable, non-polling exit detection:
+/// Linux: pidfd_open() + asio::posix::stream_descriptor
+/// Windows: asio::windows::object_handle
+/// macOS: kqueue EVFILT_PROC/NOTE_EXIT + asio::posix::stream_descriptor
+///
+/// The callback is invoked exactly once when the process exits, posted to the
+/// io_context. Call Cancel() to suppress the callback.
+class ProcessExitWatcher
+{
+public:
+ explicit ProcessExitWatcher(asio::io_context& IoContext);
+ ~ProcessExitWatcher();
+
+ ProcessExitWatcher(const ProcessExitWatcher&) = delete;
+ ProcessExitWatcher& operator=(const ProcessExitWatcher&) = delete;
+
+ /// Begin watching the given process. The callback is posted to the io_context
+ /// when the process exits. Only one Watch() may be active at a time.
+ void Watch(const ProcessHandle& Handle, std::function<void(int ExitCode)> OnExit);
+
+ /// Cancel any outstanding watch. The callback will not be invoked after this
+ /// returns. Safe to call if no watch is active.
+ void Cancel();
+
+private:
+ struct Impl;
+ std::unique_ptr<Impl> m_Impl;
+};
+
+} // namespace zen