diff options
Diffstat (limited to 'boost/process/detail/posix/wait_for_exit.hpp')
-rw-r--r-- | boost/process/detail/posix/wait_for_exit.hpp | 162 |
1 files changed, 30 insertions, 132 deletions
diff --git a/boost/process/detail/posix/wait_for_exit.hpp b/boost/process/detail/posix/wait_for_exit.hpp index 308aaf6908..21e277baea 100644 --- a/boost/process/detail/posix/wait_for_exit.hpp +++ b/boost/process/detail/posix/wait_for_exit.hpp @@ -19,21 +19,6 @@ namespace boost { namespace process { namespace detail { namespace posix { -inline void wait(const child_handle &p, int & exit_code) -{ - pid_t ret; - int status; - do - { - ret = ::waitpid(p.pid, &status, 0); - } while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status))); - if (ret == -1) - boost::process::detail::throw_last_error("waitpid(2) failed"); - if (WIFSIGNALED(status)) - throw process_error(std::error_code(), "process terminated due to receipt of a signal"); - exit_code = status; -} - inline void wait(const child_handle &p, int & exit_code, std::error_code &ec) noexcept { pid_t ret; @@ -44,7 +29,7 @@ inline void wait(const child_handle &p, int & exit_code, std::error_code &ec) no ret = ::waitpid(p.pid, &status, 0); } while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status))); - + if (ret == -1) ec = boost::process::detail::get_last_error(); else @@ -52,60 +37,25 @@ inline void wait(const child_handle &p, int & exit_code, std::error_code &ec) no ec.clear(); exit_code = status; } - - } -template< class Rep, class Period > -inline bool wait_for( - const child_handle &p, - int & exit_code, - const std::chrono::duration<Rep, Period>& rel_time) +inline void wait(const child_handle &p, int & exit_code) noexcept { - - pid_t ret; - int status; - - auto start = std::chrono::system_clock::now(); - auto time_out = start + rel_time; - - bool timed_out; - do - { - ret = ::waitpid(p.pid, &status, WNOHANG); - if (ret == 0) - { - timed_out = std::chrono::system_clock::now() >= time_out; - if (timed_out) - return false; - } - } - while ((ret == 0) || - ((ret == -1) && errno == EINTR) || - ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status))); - - if (ret == -1) - boost::process::detail::throw_last_error("waitpid(2) failed"); - - exit_code = status; - - return true; + std::error_code ec; + wait(p, exit_code, ec); + boost::process::detail::throw_error(ec, "waitpid(2) failed in wait"); } - -template< class Rep, class Period > -inline bool wait_for( +template< class Clock, class Duration > +inline bool wait_until( const child_handle &p, int & exit_code, - const std::chrono::duration<Rep, Period>& rel_time, + const std::chrono::time_point<Clock, Duration>& time_out, std::error_code & ec) noexcept { - pid_t ret; int status; - auto start = std::chrono::system_clock::now(); - auto time_out = start + rel_time; bool timed_out; do @@ -113,19 +63,15 @@ inline bool wait_for( ret = ::waitpid(p.pid, &status, WNOHANG); if (ret == 0) { - timed_out = std::chrono::system_clock::now() >= time_out; + timed_out = Clock::now() >= time_out; if (timed_out) return false; } - } + } while ((ret == 0) || (((ret == -1) && errno == EINTR) || ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)))); - - if (timed_out && (ret == -1)) - return false; - if (ret == -1) ec = boost::process::detail::get_last_error(); else @@ -137,86 +83,38 @@ inline bool wait_for( return true; } - - template< class Clock, class Duration > inline bool wait_until( const child_handle &p, int & exit_code, - const std::chrono::time_point<Clock, Duration>& time_out) + const std::chrono::time_point<Clock, Duration>& time_out) noexcept { - - pid_t ret; - int status; - - bool timed_out; - do - { - ret = ::waitpid(p.pid, &status, WNOHANG); - if (ret == 0) - { - timed_out = std::chrono::system_clock::now() >= time_out; - if (timed_out) - return false; - } - } - while ((ret == 0) || - (((ret == -1) && errno == EINTR) || - ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)))); - - - if (timed_out && !WIFEXITED(status)) - return false; - - if (ret == -1) - boost::process::detail::throw_last_error("waitpid(2) failed"); - exit_code = status; - - return true; + std::error_code ec; + bool b = wait_until(p, exit_code, time_out, ec); + boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until"); + return b; } - -template< class Clock, class Duration > -inline bool wait_until( +template< class Rep, class Period > +inline bool wait_for( const child_handle &p, int & exit_code, - const std::chrono::time_point<Clock, Duration>& time_out, + const std::chrono::duration<Rep, Period>& rel_time, std::error_code & ec) noexcept { + return wait_until(p, exit_code, std::chrono::steady_clock::now() + rel_time, ec); +} - pid_t ret; - int status; - - bool timed_out; - - do - { - ret = ::waitpid(p.pid, &status, WNOHANG); - if (ret == 0) - { - timed_out = std::chrono::system_clock::now() >= time_out; - if (timed_out) - return false; - } - } - while ((ret == 0) || - (((ret == -1) && errno == EINTR) || - ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)))); - - - - if (timed_out && !WIFEXITED(status)) - return false; - - if (ret == -1) - ec = boost::process::detail::get_last_error(); - else - { - ec.clear(); - exit_code = status; - } - - return true; +template< class Rep, class Period > +inline bool wait_for( + const child_handle &p, + int & exit_code, + const std::chrono::duration<Rep, Period>& rel_time) noexcept +{ + std::error_code ec; + bool b = wait_for(p, exit_code, rel_time, ec); + boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for"); + return b; } }}}} |