eventfd_select_interrupter.ipp 4.5 KB

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