pipe_select_interrupter.hpp 3.1 KB

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