group.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * \file boost/process/group.hpp
  7. *
  8. * Defines a group process class.
  9. * For additional information see the platform specific implementations:
  10. *
  11. * - [windows - job object](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684161.aspx)
  12. * - [posix - process group](http://pubs.opengroup.org/onlinepubs/009695399/functions/setpgid.html)
  13. *
  14. */
  15. #ifndef BOOST_PROCESS_GROUP_HPP
  16. #define BOOST_PROCESS_GROUP_HPP
  17. #include <boost/process/detail/config.hpp>
  18. #include <boost/process/child.hpp>
  19. #include <chrono>
  20. #include <memory>
  21. #include <boost/none.hpp>
  22. #include <atomic>
  23. #if defined(BOOST_POSIX_API)
  24. #include <boost/process/detail/posix/group_handle.hpp>
  25. #include <boost/process/detail/posix/group_ref.hpp>
  26. #include <boost/process/detail/posix/wait_group.hpp>
  27. #elif defined(BOOST_WINDOWS_API)
  28. #include <boost/process/detail/windows/group_handle.hpp>
  29. #include <boost/process/detail/windows/group_ref.hpp>
  30. #include <boost/process/detail/windows/wait_group.hpp>
  31. #endif
  32. namespace boost {
  33. namespace process {
  34. namespace detail {
  35. struct group_builder;
  36. }
  37. /**
  38. * Represents a process group.
  39. *
  40. * Groups are movable but non-copyable. The destructor
  41. * automatically closes handles to the group process.
  42. *
  43. * The group will have the same interface as std::thread.
  44. *
  45. * \note If the destructor is called without a previous detach or wait, the group will be terminated.
  46. *
  47. * \attention If a default-constructed group is used before being used in a process launch, the behaviour is undefined.
  48. *
  49. * \attention Waiting for groups is currently broken on windows and will most likely result in a dead-lock.
  50. */
  51. class group
  52. {
  53. ::boost::process::detail::api::group_handle _group_handle;
  54. bool _attached = true;
  55. public:
  56. typedef ::boost::process::detail::api::group_handle group_handle;
  57. ///Native representation of the handle.
  58. typedef group_handle::handle_t native_handle_t;
  59. explicit group(group_handle &&ch) : _group_handle(std::move(ch)) {}
  60. ///Construct the group from a native_handle
  61. explicit group(native_handle_t & handle) : _group_handle(handle) {};
  62. group(const group&) = delete;
  63. ///Move constructor
  64. group(group && lhs)
  65. : _group_handle(std::move(lhs._group_handle)),
  66. _attached (lhs._attached)
  67. {
  68. lhs._attached = false;
  69. }
  70. ///Default constructor
  71. group() = default;
  72. group& operator=(const group&) = delete;
  73. ///Move assign
  74. group& operator=(group && lhs)
  75. {
  76. _group_handle= std::move(lhs._group_handle);
  77. _attached = lhs._attached;
  78. return *this;
  79. };
  80. ///Detach the group
  81. void detach() {_attached = false; }
  82. /** Join the child. This just calls wait, but that way the naming is similar to std::thread */
  83. void join() {wait();}
  84. /** Check if the child is joinable. */
  85. bool joinable() {return _attached;}
  86. /** Destructor
  87. *
  88. * \note If the destructor is called without a previous detach or wait, the group will be terminated.
  89. *
  90. */
  91. ~group()
  92. {
  93. std::error_code ec;
  94. if ( _attached && valid())
  95. terminate(ec);
  96. }
  97. ///Obtain the native handle of the group.
  98. native_handle_t native_handle() const { return _group_handle.handle(); }
  99. ///Wait for the process group to exit.
  100. void wait()
  101. {
  102. boost::process::detail::api::wait(_group_handle);
  103. }
  104. ///\overload void wait()
  105. void wait(std::error_code & ec) noexcept
  106. {
  107. boost::process::detail::api::wait(_group_handle, ec);
  108. }
  109. /** Wait for the process group to exit for period of time.
  110. * \return True if all child processes exited while waiting.*/
  111. template< class Rep, class Period >
  112. bool wait_for (const std::chrono::duration<Rep, Period>& rel_time)
  113. {
  114. return boost::process::detail::api::wait_for(_group_handle, rel_time);
  115. }
  116. /** \overload bool wait_for(const std::chrono::duration<Rep, Period>& timeout_time ) */
  117. template< class Rep, class Period >
  118. bool wait_for (const std::chrono::duration<Rep, Period>& rel_time, std::error_code & ec) noexcept
  119. {
  120. return boost::process::detail::api::wait_for(_group_handle, rel_time, ec);
  121. }
  122. /** Wait for the process group to exit until a point in time.
  123. * \return True if all child processes exited while waiting.*/
  124. template< class Clock, class Duration >
  125. bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time )
  126. {
  127. return boost::process::detail::api::wait_until(_group_handle, timeout_time);
  128. }
  129. /** \overload bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time ) */
  130. template< class Clock, class Duration >
  131. bool wait_until(const std::chrono::time_point<Clock, Duration>& timeout_time, std::error_code & ec) noexcept
  132. {
  133. return boost::process::detail::api::wait_until(_group_handle, timeout_time, ec);
  134. }
  135. ///Check if the group has a valid handle.
  136. bool valid() const
  137. {
  138. return _group_handle.valid();
  139. }
  140. ///Convenience to call valid.
  141. explicit operator bool() const {return valid();}
  142. ///Terminate the process group, i.e. all processes in the group
  143. void terminate()
  144. {
  145. ::boost::process::detail::api::terminate(_group_handle);
  146. }
  147. ///\overload void terminate()
  148. void terminate(std::error_code & ec) noexcept
  149. {
  150. ::boost::process::detail::api::terminate(_group_handle, ec);
  151. }
  152. ///Assign a child process to the group
  153. void add(const child &c)
  154. {
  155. _group_handle.add(c.native_handle());
  156. }
  157. ///\overload void assign(const child & c)
  158. void add(const child &c, std::error_code & ec) noexcept
  159. {
  160. _group_handle.add(c.native_handle(), ec);
  161. }
  162. ///Check if the child process is in the group
  163. bool has(const child &c)
  164. {
  165. return _group_handle.has(c.native_handle());
  166. }
  167. ///\overload bool has(const child &)
  168. bool has(const child &c, std::error_code & ec) noexcept
  169. {
  170. return _group_handle.has(c.native_handle(), ec);
  171. }
  172. friend struct detail::group_builder;
  173. };
  174. namespace detail
  175. {
  176. struct group_tag;
  177. struct group_builder
  178. {
  179. group * group_p;
  180. void operator()(group & grp) {this->group_p = &grp;};
  181. typedef api::group_ref result_type;
  182. api::group_ref get_initializer() {return api::group_ref (group_p->_group_handle);};
  183. };
  184. template<>
  185. struct initializer_tag<group>
  186. {
  187. typedef group_tag type;
  188. };
  189. template<>
  190. struct initializer_builder<group_tag>
  191. {
  192. typedef group_builder type;
  193. };
  194. }
  195. }}
  196. #endif