pipe_select_interrupter.ipp 2.9 KB

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