pipe_select_interrupter.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // pipe_select_interrupter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
  11. #define ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  21. #include "asio/detail/push_options.hpp"
  22. #include <fcntl.h>
  23. #include "asio/detail/pop_options.hpp"
  24. #include "asio/error.hpp"
  25. #include "asio/system_error.hpp"
  26. #include "asio/detail/socket_types.hpp"
  27. namespace asio {
  28. namespace detail {
  29. class pipe_select_interrupter
  30. {
  31. public:
  32. // Constructor.
  33. pipe_select_interrupter()
  34. {
  35. int pipe_fds[2];
  36. if (pipe(pipe_fds) == 0)
  37. {
  38. read_descriptor_ = pipe_fds[0];
  39. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  40. write_descriptor_ = pipe_fds[1];
  41. ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
  42. }
  43. else
  44. {
  45. asio::error_code ec(errno,
  46. asio::error::get_system_category());
  47. asio::system_error e(ec, "pipe_select_interrupter");
  48. boost::throw_exception(e);
  49. }
  50. }
  51. // Destructor.
  52. ~pipe_select_interrupter()
  53. {
  54. if (read_descriptor_ != -1)
  55. ::close(read_descriptor_);
  56. if (write_descriptor_ != -1)
  57. ::close(write_descriptor_);
  58. }
  59. // Interrupt the select call.
  60. void interrupt()
  61. {
  62. char byte = 0;
  63. int result = ::write(write_descriptor_, &byte, 1);
  64. (void)result;
  65. }
  66. // Reset the select interrupt. Returns true if the call was interrupted.
  67. bool reset()
  68. {
  69. for (;;)
  70. {
  71. char data[1024];
  72. int bytes_read = ::read(read_descriptor_, data, sizeof(data));
  73. if (bytes_read < 0 && errno == EINTR)
  74. continue;
  75. bool was_interrupted = (bytes_read > 0);
  76. while (bytes_read == sizeof(data))
  77. bytes_read = ::read(read_descriptor_, data, sizeof(data));
  78. return was_interrupted;
  79. }
  80. }
  81. // Get the read descriptor to be passed to select.
  82. int read_descriptor() const
  83. {
  84. return read_descriptor_;
  85. }
  86. private:
  87. // The read end of a connection used to interrupt the select call. This file
  88. // descriptor is passed to select such that when it is time to stop, a single
  89. // byte will be written on the other end of the connection and this
  90. // descriptor will become readable.
  91. int read_descriptor_;
  92. // The write end of a connection used to interrupt the select call. A single
  93. // byte may be written to this to wake up the select which is waiting for the
  94. // other end to become readable.
  95. int write_descriptor_;
  96. };
  97. } // namespace detail
  98. } // namespace asio
  99. #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  100. #include "asio/detail/pop_options.hpp"
  101. #endif // ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP