]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal |
2 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling | |
3 | // Copyright (c) 2009 Boris Schaeling | |
4 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling | |
5 | // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | ||
10 | #ifndef BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP | |
11 | #define BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP | |
12 | ||
13 | #include <boost/process/detail/config.hpp> | |
14 | #include <boost/process/detail/posix/group_handle.hpp> | |
11fdf7f2 | 15 | #include <chrono> |
b32b8144 FG |
16 | #include <system_error> |
17 | #include <sys/types.h> | |
18 | #include <sys/wait.h> | |
19 | ||
20 | namespace boost { namespace process { namespace detail { namespace posix { | |
21 | ||
b32b8144 FG |
22 | inline void wait(const group_handle &p, std::error_code &ec) noexcept |
23 | { | |
24 | pid_t ret; | |
25 | int status; | |
26 | ||
27 | do | |
28 | { | |
29 | ret = ::waitpid(-p.grp, &status, 0); | |
30 | } | |
31 | while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status))); | |
11fdf7f2 | 32 | |
b32b8144 FG |
33 | if (ret == -1) |
34 | ec = boost::process::detail::get_last_error(); | |
b32b8144 FG |
35 | else |
36 | ec.clear(); | |
b32b8144 FG |
37 | } |
38 | ||
11fdf7f2 | 39 | inline void wait(const group_handle &p) noexcept |
b32b8144 | 40 | { |
11fdf7f2 TL |
41 | std::error_code ec; |
42 | wait(p, ec); | |
43 | boost::process::detail::throw_error(ec, "waitpid(2) failed in wait"); | |
b32b8144 FG |
44 | } |
45 | ||
11fdf7f2 TL |
46 | template< class Clock, class Duration > |
47 | inline bool wait_until( | |
b32b8144 | 48 | const group_handle &p, |
11fdf7f2 | 49 | const std::chrono::time_point<Clock, Duration>& time_out, |
b32b8144 FG |
50 | std::error_code & ec) noexcept |
51 | { | |
b32b8144 FG |
52 | pid_t ret; |
53 | int status; | |
54 | ||
11fdf7f2 | 55 | bool timed_out; |
b32b8144 | 56 | |
b32b8144 FG |
57 | do |
58 | { | |
11fdf7f2 TL |
59 | ret = ::waitpid(-p.grp, &status, WNOHANG); |
60 | if (ret == 0) | |
b32b8144 | 61 | { |
11fdf7f2 TL |
62 | timed_out = Clock::now() >= time_out; |
63 | if (timed_out) | |
64 | return false; | |
b32b8144 | 65 | } |
11fdf7f2 TL |
66 | } |
67 | while ((ret == 0) || | |
68 | (((ret == -1) && errno == EINTR) || | |
69 | ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)))); | |
b32b8144 FG |
70 | |
71 | if (ret == -1) | |
72 | ec = boost::process::detail::get_last_error(); | |
b32b8144 FG |
73 | else |
74 | ec.clear(); | |
75 | ||
11fdf7f2 | 76 | return true; |
b32b8144 FG |
77 | } |
78 | ||
11fdf7f2 | 79 | template< class Clock, class Duration > |
b32b8144 FG |
80 | inline bool wait_until( |
81 | const group_handle &p, | |
11fdf7f2 | 82 | const std::chrono::time_point<Clock, Duration>& time_out) noexcept |
b32b8144 | 83 | { |
11fdf7f2 TL |
84 | std::error_code ec; |
85 | bool b = wait_until(p, time_out, ec); | |
86 | boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until"); | |
87 | return b; | |
b32b8144 FG |
88 | } |
89 | ||
b32b8144 | 90 | template< class Rep, class Period > |
11fdf7f2 | 91 | inline bool wait_for( |
b32b8144 | 92 | const group_handle &p, |
11fdf7f2 | 93 | const std::chrono::duration<Rep, Period>& rel_time, |
b32b8144 FG |
94 | std::error_code & ec) noexcept |
95 | { | |
11fdf7f2 TL |
96 | return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec); |
97 | } | |
b32b8144 | 98 | |
11fdf7f2 TL |
99 | template< class Rep, class Period > |
100 | inline bool wait_for( | |
101 | const group_handle &p, | |
102 | const std::chrono::duration<Rep, Period>& rel_time) noexcept | |
103 | { | |
104 | std::error_code ec; | |
105 | bool b = wait_for(p, rel_time, ec); | |
106 | boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for"); | |
107 | return b; | |
b32b8144 FG |
108 | } |
109 | ||
110 | }}}} | |
111 | ||
112 | #endif |