Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:08

0001 // Copyright (c) 2016 Klemens D. Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_
0007 #define BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_
0008 
0009 #include <boost/process/detail/config.hpp>
0010 #include <boost/process/detail/windows/group_handle.hpp>
0011 #include <boost/winapi/jobs.hpp>
0012 #include <boost/winapi/wait.hpp>
0013 #include <chrono>
0014 
0015 namespace boost { namespace process { namespace detail { namespace windows {
0016 
0017 struct group_handle;
0018 
0019 
0020 inline bool wait_impl(const group_handle & p, std::error_code & ec, std::chrono::system_clock::rep wait_time)
0021 {
0022     ::boost::winapi::DWORD_ completion_code;
0023     ::boost::winapi::ULONG_PTR_ completion_key;
0024     ::boost::winapi::LPOVERLAPPED_ overlapped;
0025 
0026     auto start_time = std::chrono::system_clock::now();
0027 
0028     while (workaround::get_queued_completion_status(
0029                                        p._io_port, &completion_code,
0030                                        &completion_key, &overlapped, static_cast<::boost::winapi::DWORD_>(wait_time)))
0031     {
0032         if (reinterpret_cast<::boost::winapi::HANDLE_>(completion_key) == p._job_object &&
0033              completion_code == workaround::JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO_)
0034         {
0035 
0036             //double check, could be a different handle from a child
0037             workaround::JOBOBJECT_BASIC_ACCOUNTING_INFORMATION_ info;
0038             if (!workaround::query_information_job_object(
0039                     p._job_object,
0040                     workaround::JobObjectBasicAccountingInformation_,
0041                     static_cast<void *>(&info),
0042                     sizeof(info), nullptr))
0043             {
0044                 ec = get_last_error();
0045                 return false;
0046             }
0047             else if (info.ActiveProcesses == 0)
0048                 return false; //correct, nothing left.
0049         }
0050         //reduce the remaining wait time -> in case interrupted by something else
0051         if (wait_time != static_cast<int>(::boost::winapi::infinite))
0052         {
0053             auto now = std::chrono::system_clock::now();
0054             auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
0055             wait_time -= static_cast<std::chrono::system_clock::rep>(diff.count());
0056             start_time = now;
0057             if (wait_time <= 0)
0058                 return true; //timeout with other source
0059         }
0060 
0061     }
0062 
0063     auto ec_ = get_last_error();
0064     if (ec_.value() == ::boost::winapi::wait_timeout)
0065         return true; //timeout
0066 
0067     ec = ec_;
0068     return false;
0069 }
0070 
0071 inline void wait(const group_handle &p, std::error_code &ec)
0072 {
0073     wait_impl(p, ec, ::boost::winapi::infinite);
0074 }
0075 
0076 inline void wait(const group_handle &p)
0077 {
0078     std::error_code ec;
0079     wait(p, ec);
0080     boost::process::detail::throw_error(ec, "wait error");
0081 }
0082 
0083 template< class Clock, class Duration >
0084 inline bool wait_until(
0085         const group_handle &p,
0086         const std::chrono::time_point<Clock, Duration>& timeout_time,
0087         std::error_code &ec)
0088 {
0089     std::chrono::milliseconds ms =
0090             std::chrono::duration_cast<std::chrono::milliseconds>(
0091                     timeout_time - Clock::now());
0092 
0093     auto timeout = wait_impl(p, ec, ms.count());
0094     return !ec && !timeout;
0095 }
0096 
0097 template< class Clock, class Duration >
0098 inline bool wait_until(
0099         const group_handle &p,
0100         const std::chrono::time_point<Clock, Duration>& timeout_time)
0101 {
0102     std::error_code ec;
0103     bool b = wait_until(p, timeout_time, ec);
0104     boost::process::detail::throw_error(ec, "wait_until error");
0105     return b;
0106 }
0107 
0108 template< class Rep, class Period >
0109 inline bool wait_for(
0110         const group_handle &p,
0111         const std::chrono::duration<Rep, Period>& rel_time,
0112         std::error_code &ec)
0113 {
0114     auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(rel_time);
0115     auto timeout = wait_impl(p, ec, ms.count());
0116     return !ec && !timeout;
0117 }
0118 
0119 template< class Rep, class Period >
0120 inline bool wait_for(
0121         const group_handle &p,
0122         const std::chrono::duration<Rep, Period>& rel_time)
0123 {
0124     std::error_code ec;
0125     bool b = wait_for(p, rel_time, ec);
0126     boost::process::detail::throw_error(ec, "wait_for error");
0127     return b;
0128 }
0129 
0130 }}}}
0131 
0132 #endif /* BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_ */