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