eventfd_select_interrupter.ipp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // detail/impl/eventfd_select_interrupter.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Roelof Naude (roelof.naude at gmail dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_IMPL_EVENTFD_SELECT_INTERRUPTER_IPP
  12. #define BOOST_ASIO_DETAIL_IMPL_EVENTFD_SELECT_INTERRUPTER_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_EVENTFD)
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <fcntl.h>
  21. #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  22. # include <asm/unistd.h>
  23. #else // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  24. # include <sys/eventfd.h>
  25. #endif // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  26. #include <boost/asio/detail/cstdint.hpp>
  27. #include <boost/asio/detail/eventfd_select_interrupter.hpp>
  28. #include <boost/asio/detail/throw_error.hpp>
  29. #include <boost/asio/error.hpp>
  30. #include <boost/asio/detail/push_options.hpp>
  31. namespace boost {
  32. namespace asio {
  33. namespace detail {
  34. eventfd_select_interrupter::eventfd_select_interrupter()
  35. {
  36. open_descriptors();
  37. }
  38. void eventfd_select_interrupter::open_descriptors()
  39. {
  40. #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  41. write_descriptor_ = read_descriptor_ = syscall(__NR_eventfd, 0);
  42. if (read_descriptor_ != -1)
  43. {
  44. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  45. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  46. }
  47. #else // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  48. # if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  49. write_descriptor_ = read_descriptor_ =
  50. ::eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
  51. # else // defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  52. errno = EINVAL;
  53. write_descriptor_ = read_descriptor_ = -1;
  54. # endif // defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  55. if (read_descriptor_ == -1 && errno == EINVAL)
  56. {
  57. write_descriptor_ = read_descriptor_ = ::eventfd(0, 0);
  58. if (read_descriptor_ != -1)
  59. {
  60. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  61. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  62. }
  63. }
  64. #endif // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  65. if (read_descriptor_ == -1)
  66. {
  67. int pipe_fds[2];
  68. if (pipe(pipe_fds) == 0)
  69. {
  70. read_descriptor_ = pipe_fds[0];
  71. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  72. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  73. write_descriptor_ = pipe_fds[1];
  74. ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
  75. ::fcntl(write_descriptor_, F_SETFD, FD_CLOEXEC);
  76. }
  77. else
  78. {
  79. boost::system::error_code ec(errno,
  80. boost::asio::error::get_system_category());
  81. boost::asio::detail::throw_error(ec, "eventfd_select_interrupter");
  82. }
  83. }
  84. }
  85. eventfd_select_interrupter::~eventfd_select_interrupter()
  86. {
  87. close_descriptors();
  88. }
  89. void eventfd_select_interrupter::close_descriptors()
  90. {
  91. if (write_descriptor_ != -1 && write_descriptor_ != read_descriptor_)
  92. ::close(write_descriptor_);
  93. if (read_descriptor_ != -1)
  94. ::close(read_descriptor_);
  95. }
  96. void eventfd_select_interrupter::recreate()
  97. {
  98. close_descriptors();
  99. write_descriptor_ = -1;
  100. read_descriptor_ = -1;
  101. open_descriptors();
  102. }
  103. void eventfd_select_interrupter::interrupt()
  104. {
  105. uint64_t counter(1UL);
  106. int result = ::write(write_descriptor_, &counter, sizeof(uint64_t));
  107. (void)result;
  108. }
  109. bool eventfd_select_interrupter::reset()
  110. {
  111. if (write_descriptor_ == read_descriptor_)
  112. {
  113. for (;;)
  114. {
  115. // Only perform one read. The kernel maintains an atomic counter.
  116. uint64_t counter(0);
  117. errno = 0;
  118. int bytes_read = ::read(read_descriptor_, &counter, sizeof(uint64_t));
  119. if (bytes_read < 0 && errno == EINTR)
  120. continue;
  121. bool was_interrupted = (bytes_read > 0);
  122. return was_interrupted;
  123. }
  124. }
  125. else
  126. {
  127. for (;;)
  128. {
  129. // Clear all data from the pipe.
  130. char data[1024];
  131. int bytes_read = ::read(read_descriptor_, data, sizeof(data));
  132. if (bytes_read < 0 && errno == EINTR)
  133. continue;
  134. bool was_interrupted = (bytes_read > 0);
  135. while (bytes_read == sizeof(data))
  136. bytes_read = ::read(read_descriptor_, data, sizeof(data));
  137. return was_interrupted;
  138. }
  139. }
  140. }
  141. } // namespace detail
  142. } // namespace asio
  143. } // namespace boost
  144. #include <boost/asio/detail/pop_options.hpp>
  145. #endif // defined(BOOST_ASIO_HAS_EVENTFD)
  146. #endif // BOOST_ASIO_DETAIL_IMPL_EVENTFD_SELECT_INTERRUPTER_IPP