wait_group.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
  11. #include <boost/process/detail/config.hpp>
  12. #include <boost/process/detail/posix/group_handle.hpp>
  13. #include <chrono>
  14. #include <system_error>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. namespace boost { namespace process { namespace detail { namespace posix {
  18. inline void wait(const group_handle &p, std::error_code &ec) noexcept
  19. {
  20. pid_t ret;
  21. int status;
  22. do
  23. {
  24. ret = ::waitpid(-p.grp, &status, 0);
  25. }
  26. while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
  27. if (ret == -1)
  28. ec = boost::process::detail::get_last_error();
  29. else
  30. ec.clear();
  31. }
  32. inline void wait(const group_handle &p) noexcept
  33. {
  34. std::error_code ec;
  35. wait(p, ec);
  36. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
  37. }
  38. template< class Clock, class Duration >
  39. inline bool wait_until(
  40. const group_handle &p,
  41. const std::chrono::time_point<Clock, Duration>& time_out,
  42. std::error_code & ec) noexcept
  43. {
  44. pid_t ret;
  45. int status;
  46. bool timed_out;
  47. do
  48. {
  49. ret = ::waitpid(-p.grp, &status, WNOHANG);
  50. if (ret == 0)
  51. {
  52. timed_out = Clock::now() >= time_out;
  53. if (timed_out)
  54. return false;
  55. }
  56. }
  57. while ((ret == 0) ||
  58. (((ret == -1) && errno == EINTR) ||
  59. ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status))));
  60. if (ret == -1)
  61. ec = boost::process::detail::get_last_error();
  62. else
  63. ec.clear();
  64. return true;
  65. }
  66. template< class Clock, class Duration >
  67. inline bool wait_until(
  68. const group_handle &p,
  69. const std::chrono::time_point<Clock, Duration>& time_out) noexcept
  70. {
  71. std::error_code ec;
  72. bool b = wait_until(p, time_out, ec);
  73. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
  74. return b;
  75. }
  76. template< class Rep, class Period >
  77. inline bool wait_for(
  78. const group_handle &p,
  79. const std::chrono::duration<Rep, Period>& rel_time,
  80. std::error_code & ec) noexcept
  81. {
  82. return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
  83. }
  84. template< class Rep, class Period >
  85. inline bool wait_for(
  86. const group_handle &p,
  87. const std::chrono::duration<Rep, Period>& rel_time) noexcept
  88. {
  89. std::error_code ec;
  90. bool b = wait_for(p, rel_time, ec);
  91. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
  92. return b;
  93. }
  94. }}}}
  95. #endif