wait_group.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <unistd.h>
  18. namespace boost { namespace process { namespace detail { namespace posix {
  19. inline void wait(const group_handle &p, std::error_code &ec) noexcept
  20. {
  21. pid_t ret;
  22. siginfo_t status;
  23. do
  24. {
  25. ret = ::waitpid(-p.grp, &status.si_status, 0);
  26. if (ret == -1)
  27. {
  28. ec = get_last_error();
  29. return;
  30. }
  31. //ECHILD --> no child processes left.
  32. ret = ::waitid(P_PGID, p.grp, &status, WEXITED | WNOHANG);
  33. }
  34. while ((ret != -1) || (errno != ECHILD));
  35. if (errno != ECHILD)
  36. ec = boost::process::detail::get_last_error();
  37. else
  38. ec.clear();
  39. }
  40. inline void wait(const group_handle &p) noexcept
  41. {
  42. std::error_code ec;
  43. wait(p, ec);
  44. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
  45. }
  46. template< class Clock, class Duration >
  47. inline bool wait_until(
  48. const group_handle &p,
  49. const std::chrono::time_point<Clock, Duration>& time_out,
  50. std::error_code & ec) noexcept
  51. {
  52. ::siginfo_t siginfo;
  53. bool timed_out = false;
  54. int ret;
  55. ::timespec sleep_interval;
  56. sleep_interval.tv_sec = 0;
  57. sleep_interval.tv_nsec = 100000000;
  58. while (!(timed_out = (Clock::now() > time_out)))
  59. {
  60. ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WSTOPPED | WNOHANG);
  61. if (ret == -1)
  62. {
  63. if ((errno == ECHILD) || (errno == ESRCH))
  64. {
  65. ec.clear();
  66. return true;
  67. }
  68. ec = boost::process::detail::get_last_error();
  69. return false;
  70. }
  71. //we can wait, because unlike in the wait_for_exit, we have no race condition regarding eh exit code.
  72. ::nanosleep(&sleep_interval, nullptr);
  73. }
  74. return !timed_out;
  75. }
  76. template< class Clock, class Duration >
  77. inline bool wait_until(
  78. const group_handle &p,
  79. const std::chrono::time_point<Clock, Duration>& time_out) noexcept
  80. {
  81. std::error_code ec;
  82. bool b = wait_until(p, time_out, ec);
  83. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
  84. return b;
  85. }
  86. template< class Rep, class Period >
  87. inline bool wait_for(
  88. const group_handle &p,
  89. const std::chrono::duration<Rep, Period>& rel_time,
  90. std::error_code & ec) noexcept
  91. {
  92. return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
  93. }
  94. template< class Rep, class Period >
  95. inline bool wait_for(
  96. const group_handle &p,
  97. const std::chrono::duration<Rep, Period>& rel_time) noexcept
  98. {
  99. std::error_code ec;
  100. bool b = wait_for(p, rel_time, ec);
  101. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
  102. return b;
  103. }
  104. }}}}
  105. #endif