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 userver/engine/subprocess/child_process.hpp
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
20class ChildProcess final {
21 public:
22 /// @cond
23 explicit ChildProcess(ChildProcessImpl&& impl) noexcept;
24 /// @endcond
25
26 ChildProcess(ChildProcess&&) noexcept;
27 ChildProcess& operator=(ChildProcess&&) noexcept;
28
29 /// Does not terminate the child process (just detaches from it).
31
32 /// Returns pid of the child process.
33 int GetPid() const;
34
35 /// Wait for the child process to terminate.
36 /// Ignores cancellations of the current task.
37 void Wait();
38
39 /// Wait for the child process to terminate.
40 /// Returns if this did not happen for the specified timeout duration.
41 template <typename Rep, typename Period>
42 [[nodiscard]] bool WaitFor(std::chrono::duration<Rep, Period> duration) {
43 return WaitUntil(Deadline::FromDuration(duration));
44 }
45
46 /// Wait for the child process to terminate.
47 /// Returns if this did not happen until the specified time point has been
48 /// reached.
49 template <typename Clock, typename Duration>
50 [[nodiscard]] bool WaitUntil(std::chrono::time_point<Clock, Duration> until) {
51 return WaitUntil(Deadline::FromTimePoint(until));
52 }
53
54 /// Wait for the child process to terminate.
55 /// Returns if this did not happen until the specified Deadline has been
56 /// reached.
57 [[nodiscard]] bool WaitUntil(Deadline deadline);
58
59 /// Wait for the child process to terminate.
60 /// Returns `ChildProcessStatus` of finished subprocess.
61 [[nodiscard]] ChildProcessStatus Get();
62
63 /// Send a signal to the child process.
64 void SendSignal(int signum);
65
66 private:
67 static constexpr std::size_t kImplSize =
68 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