eventfd_select_interrupter.ipp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // detail/impl/eventfd_select_interrupter.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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 && !defined(__UCLIBC__)
  22. # include <asm/unistd.h>
  23. #else // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8 && !defined(__UCLIBC__)
  24. # include <sys/eventfd.h>
  25. #endif // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8 && !defined(__UCLIBC__)
  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(bool use_eventfd)
  35. {
  36. open_descriptors(use_eventfd);
  37. }
  38. void eventfd_select_interrupter::open_descriptors(bool use_eventfd)
  39. {
  40. if (use_eventfd)
  41. {
  42. #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 8 && !defined(__UCLIBC__)
  43. write_descriptor_ = read_descriptor_ = syscall(__NR_eventfd, 0);
  44. if (read_descriptor_ != -1)
  45. {
  46. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  47. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  48. }
  49. #else // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8 && !defined(__UCLIBC__)
  50. # if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  51. write_descriptor_ = read_descriptor_ =
  52. ::eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
  53. # else // defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  54. errno = EINVAL;
  55. write_descriptor_ = read_descriptor_ = -1;
  56. # endif // defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK)
  57. if (read_descriptor_ == -1 && errno == EINVAL)
  58. {
  59. write_descriptor_ = read_descriptor_ = ::eventfd(0, 0);
  60. if (read_descriptor_ != -1)
  61. {
  62. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  63. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  64. }
  65. }
  66. #endif // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8 && !defined(__UCLIBC__)
  67. }
  68. else
  69. write_descriptor_ = read_descriptor_ = -1;
  70. if (read_descriptor_ == -1)
  71. {
  72. int pipe_fds[2];
  73. if (pipe(pipe_fds) == 0)
  74. {
  75. read_descriptor_ = pipe_fds[0];
  76. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  77. ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
  78. write_descriptor_ = pipe_fds[1];
  79. ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
  80. ::fcntl(write_descriptor_, F_SETFD, FD_CLOEXEC);
  81. }
  82. else
  83. {
  84. boost::system::error_code ec(errno,
  85. boost::asio::error::get_system_category());
  86. boost::asio::detail::throw_error(ec, "eventfd_select_interrupter");
  87. }
  88. }
  89. }
  90. eventfd_select_interrupter::~eventfd_select_interrupter()
  91. {
  92. close_descriptors();
  93. }
  94. void eventfd_select_interrupter::close_descriptors()
  95. {
  96. if (write_descriptor_ != -1 && write_descriptor_ != read_descriptor_)
  97. ::close(write_descriptor_);
  98. if (read_descriptor_ != -1)
  99. ::close(read_descriptor_);
  100. }
  101. void eventfd_select_interrupter::recreate()
  102. {
  103. bool use_eventfd = (write_descriptor_ == read_descriptor_);
  104. close_descriptors();
  105. write_descriptor_ = read_descriptor_ = -1;
  106. open_descriptors(use_eventfd);
  107. }
  108. void eventfd_select_interrupter::interrupt()
  109. {
  110. uint64_t counter(1UL);
  111. int result = ::write(write_descriptor_, &counter, sizeof(uint64_t));
  112. (void)result;
  113. }
  114. bool eventfd_select_interrupter::reset()
  115. {
  116. if (write_descriptor_ == read_descriptor_)
  117. {
  118. for (;;)
  119. {
  120. // Only perform one read. The kernel maintains an atomic counter.
  121. uint64_t counter(0);
  122. errno = 0;
  123. int bytes_read = ::read(read_descriptor_, &counter, sizeof(uint64_t));
  124. if (bytes_read < 0 && errno == EINTR)
  125. continue;
  126. return true;
  127. }
  128. }
  129. else
  130. {
  131. for (;;)
  132. {
  133. // Clear all data from the pipe.
  134. char data[1024];
  135. int bytes_read = ::read(read_descriptor_, data, sizeof(data));
  136. if (bytes_read == sizeof(data))
  137. continue;
  138. if (bytes_read > 0)
  139. return true;
  140. if (bytes_read == 0)
  141. return false;
  142. if (errno == EINTR)
  143. continue;
  144. if (errno == EWOULDBLOCK)
  145. return true;
  146. if (errno == EAGAIN)
  147. return true;
  148. return false;
  149. }
  150. }
  151. }
  152. } // namespace detail
  153. } // namespace asio
  154. } // namespace boost
  155. #include <boost/asio/detail/pop_options.hpp>
  156. #endif // defined(BOOST_ASIO_HAS_EVENTFD)
  157. #endif // BOOST_ASIO_DETAIL_IMPL_EVENTFD_SELECT_INTERRUPTER_IPP