pipe_select_interrupter.ipp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // detail/impl/pipe_select_interrupter.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  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_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  17. #if !defined(BOOST_ASIO_WINDOWS)
  18. #if !defined(__CYGWIN__)
  19. #if !defined(__SYMBIAN32__)
  20. #if !defined(BOOST_ASIO_HAS_EVENTFD)
  21. #include <fcntl.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. #include <boost/asio/detail/pipe_select_interrupter.hpp>
  26. #include <boost/asio/detail/socket_types.hpp>
  27. #include <boost/asio/detail/throw_error.hpp>
  28. #include <boost/asio/error.hpp>
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace detail {
  33. pipe_select_interrupter::pipe_select_interrupter()
  34. {
  35. open_descriptors();
  36. }
  37. void pipe_select_interrupter::open_descriptors()
  38. {
  39. int pipe_fds[2];
  40. if (pipe(pipe_fds) == 0)
  41. {
  42. read_descriptor_ = pipe_fds[0];
  43. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  44. write_descriptor_ = pipe_fds[1];
  45. ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
  46. #if defined(FD_CLOEXEC)
  47. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  48. ::fcntl(write_descriptor_, F_SETFD, FD_CLOEXEC);
  49. #endif // defined(FD_CLOEXEC)
  50. }
  51. else
  52. {
  53. boost::system::error_code ec(errno,
  54. boost::asio::error::get_system_category());
  55. boost::asio::detail::throw_error(ec, "pipe_select_interrupter");
  56. }
  57. }
  58. pipe_select_interrupter::~pipe_select_interrupter()
  59. {
  60. close_descriptors();
  61. }
  62. void pipe_select_interrupter::close_descriptors()
  63. {
  64. if (read_descriptor_ != -1)
  65. ::close(read_descriptor_);
  66. if (write_descriptor_ != -1)
  67. ::close(write_descriptor_);
  68. }
  69. void pipe_select_interrupter::recreate()
  70. {
  71. close_descriptors();
  72. write_descriptor_ = -1;
  73. read_descriptor_ = -1;
  74. open_descriptors();
  75. }
  76. void pipe_select_interrupter::interrupt()
  77. {
  78. char byte = 0;
  79. signed_size_type result = ::write(write_descriptor_, &byte, 1);
  80. (void)result;
  81. }
  82. bool pipe_select_interrupter::reset()
  83. {
  84. for (;;)
  85. {
  86. char data[1024];
  87. signed_size_type bytes_read = ::read(read_descriptor_, data, sizeof(data));
  88. if (bytes_read < 0 && errno == EINTR)
  89. continue;
  90. bool was_interrupted = (bytes_read > 0);
  91. while (bytes_read == sizeof(data))
  92. bytes_read = ::read(read_descriptor_, data, sizeof(data));
  93. return was_interrupted;
  94. }
  95. }
  96. } // namespace detail
  97. } // namespace asio
  98. } // namespace boost
  99. #include <boost/asio/detail/pop_options.hpp>
  100. #endif // !defined(BOOST_ASIO_HAS_EVENTFD)
  101. #endif // !defined(__SYMBIAN32__)
  102. #endif // !defined(__CYGWIN__)
  103. #endif // !defined(BOOST_ASIO_WINDOWS)
  104. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  105. #endif // BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP