userver: userver/engine/subprocess/child_process.hpp Source File
Loading...
Searching...
No Matches
child_process.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief @copybrief engine::subprocess::ChildProcess
5
6#include <chrono>
7
8#include <userver/compiler/select.hpp>
9#include <userver/engine/deadline.hpp>
10#include <userver/engine/subprocess/child_process_status.hpp>
11#include <userver/utils/fast_pimpl.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace engine::subprocess {
16
17class ChildProcessImpl;
18
19/// @brief Class that allows controlling child process; usually retrieved from engine::subprocess::ProcessStarter
20class ChildProcess final {
21public:
22 /// @cond
23 explicit ChildProcess(ChildProcessImpl&& impl) noexcept;
24 /// @endcond
25
26 ChildProcess(ChildProcess&&) noexcept;
27 ChildProcess& operator=(ChildProcess&&) noexcept;
28
29 /// @brief Does not terminate the child process (just detaches from it).
31
32 /// @brief Returns pid of the child process.
33 int GetPid() const;
34
35 /// @brief Wait for the child process to terminate.
36 /// Ignores cancellations of the current task.
37 void Wait();
38
39 /// @brief Wait for the child process to terminate.
40 /// @returns false if this did not happen for the specified timeout duration or a task cancellation happened; true
41 /// otherwise.
42 template <typename Rep, typename Period>
43 [[nodiscard]] bool WaitFor(std::chrono::duration<Rep, Period> duration) {
44 return WaitUntil(Deadline::FromDuration(duration));
45 }
46
47 /// @brief Wait for the child process to terminate.
48 /// @returns false if this did not happen until the specified time point has been reached or a task cancellation
49 // happened; true otherwise.
50 template <typename Clock, typename Duration>
51 [[nodiscard]] bool WaitUntil(std::chrono::time_point<Clock, Duration> until) {
52 return WaitUntil(Deadline::FromTimePoint(until));
53 }
54
55 /// Wait for the child process to terminate.
56 /// Returns if this did not happen until the specified Deadline has been
57 /// reached.
58 [[nodiscard]] bool WaitUntil(Deadline deadline);
59
60 /// @brief Wait for the child process to terminate, ignoring cancellations.
61 /// @returns ChildProcessStatus of finished subprocess
62 [[nodiscard]] ChildProcessStatus Get();
63
64 /// @brief Send a signal to the child process.
65 void SendSignal(int signum);
66
67private:
68 static constexpr std::size_t kImplSize = compiler::SelectSize().For64Bit(24).For32Bit(12);
69 static constexpr std::size_t kImplAlignment = alignof(void*);
70 utils::FastPimpl<ChildProcessImpl, kImplSize, kImplAlignment> impl_;
71};
72
73} // namespace engine::subprocess
74
75USERVER_NAMESPACE_END