]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/process/detail/windows/wait_group.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / process / detail / windows / wait_group.hpp
1 // Copyright (c) 2016 Klemens D. Morgenstern
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_
7 #define BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_
8
9 #include <boost/process/detail/config.hpp>
10 #include <boost/process/detail/windows/group_handle.hpp>
11 #include <boost/winapi/jobs.hpp>
12 #include <boost/winapi/wait.hpp>
13 #include <chrono>
14
15 namespace boost { namespace process { namespace detail { namespace windows {
16
17 struct group_handle;
18
19
20 inline bool wait_impl(const group_handle & p, std::error_code & ec, std::chrono::system_clock::rep wait_time)
21 {
22 ::boost::winapi::DWORD_ completion_code;
23 ::boost::winapi::ULONG_PTR_ completion_key;
24 ::boost::winapi::LPOVERLAPPED_ overlapped;
25
26 auto start_time = std::chrono::system_clock::now();
27
28 while (workaround::get_queued_completion_status(
29 p._io_port, &completion_code,
30 &completion_key, &overlapped, static_cast<::boost::winapi::DWORD_>(wait_time)))
31 {
32 if (reinterpret_cast<::boost::winapi::HANDLE_>(completion_key) == p._job_object &&
33 completion_code == workaround::JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO_)
34 {
35
36 //double check, could be a different handle from a child
37 workaround::JOBOBJECT_BASIC_ACCOUNTING_INFORMATION_ info;
38 if (!workaround::query_information_job_object(
39 p._job_object,
40 workaround::JobObjectBasicAccountingInformation_,
41 static_cast<void *>(&info),
42 sizeof(info), nullptr))
43 {
44 ec = get_last_error();
45 return false;
46 }
47 else if (info.ActiveProcesses == 0)
48 return false; //correct, nothing left.
49 }
50 //reduce the remaining wait time -> in case interrupted by something else
51 if (wait_time != static_cast<int>(::boost::winapi::infinite))
52 {
53 auto now = std::chrono::system_clock::now();
54 auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
55 wait_time -= static_cast<std::chrono::system_clock::rep>(diff.count());
56 start_time = now;
57 if (wait_time <= 0)
58 return true; //timeout with other source
59 }
60
61 }
62
63 auto ec_ = get_last_error();
64 if (ec_.value() == ::boost::winapi::wait_timeout)
65 return true; //timeout
66
67 ec = ec_;
68 return false;
69 }
70
71 inline void wait(const group_handle &p, std::error_code &ec)
72 {
73 wait_impl(p, ec, ::boost::winapi::infinite);
74 }
75
76 inline void wait(const group_handle &p)
77 {
78 std::error_code ec;
79 wait(p, ec);
80 boost::process::detail::throw_error(ec, "wait error");
81 }
82
83 template< class Clock, class Duration >
84 inline bool wait_until(
85 const group_handle &p,
86 const std::chrono::time_point<Clock, Duration>& timeout_time,
87 std::error_code &ec)
88 {
89 std::chrono::milliseconds ms =
90 std::chrono::duration_cast<std::chrono::milliseconds>(
91 timeout_time - Clock::now());
92
93 auto timeout = wait_impl(p, ec, ms.count());
94 return !ec && !timeout;
95 }
96
97 template< class Clock, class Duration >
98 inline bool wait_until(
99 const group_handle &p,
100 const std::chrono::time_point<Clock, Duration>& timeout_time)
101 {
102 std::error_code ec;
103 bool b = wait_until(p, timeout_time, ec);
104 boost::process::detail::throw_error(ec, "wait_until error");
105 return b;
106 }
107
108 template< class Rep, class Period >
109 inline bool wait_for(
110 const group_handle &p,
111 const std::chrono::duration<Rep, Period>& rel_time,
112 std::error_code &ec)
113 {
114 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(rel_time);
115 auto timeout = wait_impl(p, ec, ms.count());
116 return !ec && !timeout;
117 }
118
119 template< class Rep, class Period >
120 inline bool wait_for(
121 const group_handle &p,
122 const std::chrono::duration<Rep, Period>& rel_time)
123 {
124 std::error_code ec;
125 bool b = wait_for(p, rel_time, ec);
126 boost::process::detail::throw_error(ec, "wait_for error");
127 return b;
128 }
129
130 }}}}
131
132 #endif /* BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_ */