16#include <condition_variable>
29 bool launch(std::string_view executable,
30 const std::vector<std::string>& extra_args = {});
36 int pid()
const {
return pid_.load(); }
53 std::function<void(
int exit_code)>
on_exit;
63 bool join_monitor_thread(
bool detach_current_thread);
64 bool begin_message_callback();
65 void end_message_callback();
66 void stop_accepting_message_callbacks();
67 void wait_for_message_callbacks();
71 std::string pipe_name_;
72 std::atomic<int> pid_{-1};
73 std::atomic<bool> running_{
false};
74 bool cancel_requested_ =
false;
75 bool exit_ready_ =
false;
77 std::mutex state_mutex_;
78 std::condition_variable exit_cv_;
79 std::mutex monitor_mutex_;
80 std::condition_variable monitor_cv_;
81 bool monitor_launch_pending_ =
false;
82 bool monitor_join_in_progress_ =
false;
83 std::thread::id monitor_thread_id_;
84 std::thread monitor_thread_;
85 std::mutex message_callback_mutex_;
86 std::condition_variable message_callback_cv_;
87 int active_message_callbacks_ = 0;
89 bool accepting_message_callbacks_ =
true;
103 const std::vector<std::string>& args = {});
125 std::vector<std::shared_ptr<ConnectedChildProcess>> children_;
126 mutable std::mutex mutex_;
ChildProcessManager & operator=(const ChildProcessManager &)=delete
int active_count() const
Number of active child processes.
void kill_all()
Kill all child processes.
std::function< void(ConnectedChildProcess *, int exit_code)> on_child_exit
Called when any child exits.
Definition child_process_manager.hpp:118
ChildProcessManager(const ChildProcessManager &)=delete
void cleanup()
Remove completed processes from the pool.
void wait_all(int timeout_ms=0)
Wait for all children to exit.
ChildProcessManager()=default
ConnectedChildProcess * launch(std::string_view executable, const std::vector< std::string > &args={})
Launch a child process and add it to the pool.
A child process with a bidirectional IPC channel.
Definition child_process_manager.hpp:22
ConnectedChildProcess & operator=(const ConnectedChildProcess &)=delete
bool launch(std::string_view executable, const std::vector< std::string > &extra_args={})
Launch a child process with an IPC pipe.
InterprocessConnection & connection()
The IPC connection (for advanced use).
Definition child_process_manager.hpp:56
bool is_running() const
Whether the child process is running.
Definition child_process_manager.hpp:33
ConnectedChildProcess()=default
std::function< void(int exit_code)> on_exit
Called when the child process exits.
Definition child_process_manager.hpp:53
int pid() const
Get the child's PID.
Definition child_process_manager.hpp:36
void kill()
Kill the child process.
int wait_for_exit(int timeout_ms=0)
Wait for the child to exit. Returns exit code.
std::function< void(std::string_view)> on_message
Called when a message is received from the child.
Definition child_process_manager.hpp:50
ConnectedChildProcess(const ConnectedChildProcess &)=delete
bool send_message(std::string_view message)
Send a message to the child.
Interprocess connection — send and receive length-prefixed messages.
Definition interprocess_connection.hpp:33
Definition child_process_manager.hpp:18