Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
child_process_manager.hpp
Go to the documentation of this file.
1#pragma once
2
3// ConnectedChildProcess — launch a child process with an IPC channel.
4// ChildProcessManager — manage a pool of child processes (for plugin scanning etc.)
5
8#include <string>
9#include <string_view>
10#include <vector>
11#include <memory>
12#include <functional>
13#include <atomic>
14#include <thread>
15#include <mutex>
16#include <condition_variable>
17
18namespace pulp::events {
19
23public:
26
29 bool launch(std::string_view executable,
30 const std::vector<std::string>& extra_args = {});
31
33 bool is_running() const { return running_.load(); }
34
36 int pid() const { return pid_.load(); }
37
39 bool send_message(std::string_view message);
40
42 void kill();
43
45 int wait_for_exit(int timeout_ms = 0);
46
50 std::function<void(std::string_view)> on_message;
51
53 std::function<void(int exit_code)> on_exit;
54
56 InterprocessConnection& connection() { return connection_; }
57
58 // No copy
61
62private:
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();
68
69 InterprocessConnection connection_;
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;
76 int exit_code_ = -1;
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;
88 // Closed only by the destructor; normal kill/relaunch keeps callbacks armed.
89 bool accepting_message_callbacks_ = true;
90};
91
96public:
99
102 ConnectedChildProcess* launch(std::string_view executable,
103 const std::vector<std::string>& args = {});
104
106 int active_count() const;
107
109 void kill_all();
110
112 void wait_all(int timeout_ms = 0);
113
115 void cleanup();
116
118 std::function<void(ConnectedChildProcess*, int exit_code)> on_child_exit;
119
120 // No copy
123
124private:
125 std::vector<std::shared_ptr<ConnectedChildProcess>> children_;
126 mutable std::mutex mutex_;
127};
128
129} // namespace pulp::events
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.
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
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
Cross-platform child process with timeout, cancellation, and line-by-line output callbacks.
Definition child_process.hpp:91
Definition child_process_manager.hpp:18