eventfd_select_interrupter.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // eventfd_select_interrupter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_EVENTFD_SELECT_INTERRUPTER_HPP
  12. #define ASIO_DETAIL_EVENTFD_SELECT_INTERRUPTER_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/push_options.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. #include <boost/config.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include "asio/detail/pop_options.hpp"
  21. #if defined(__linux__)
  22. # if !defined(ASIO_DISABLE_EVENTFD)
  23. # include <linux/version.h>
  24. # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
  25. # define ASIO_HAS_EVENTFD
  26. # endif // LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
  27. # endif // !defined(ASIO_DISABLE_EVENTFD)
  28. #endif // defined(__linux__)
  29. #if defined(ASIO_HAS_EVENTFD)
  30. #include "asio/detail/push_options.hpp"
  31. #include <fcntl.h>
  32. #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  33. # include <asm/unistd.h>
  34. #else // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  35. # include <sys/eventfd.h>
  36. #endif // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  37. #include "asio/detail/pop_options.hpp"
  38. #include "asio/error.hpp"
  39. #include "asio/system_error.hpp"
  40. #include "asio/detail/socket_types.hpp"
  41. namespace asio {
  42. namespace detail {
  43. class eventfd_select_interrupter
  44. {
  45. public:
  46. // Constructor.
  47. eventfd_select_interrupter()
  48. {
  49. #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  50. write_descriptor_ = read_descriptor_ = syscall(__NR_eventfd, 0);
  51. #else // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  52. write_descriptor_ = read_descriptor_ = ::eventfd(0, 0);
  53. #endif // __GLIBC__ == 2 && __GLIBC_MINOR__ < 8
  54. if (read_descriptor_ != -1)
  55. {
  56. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  57. }
  58. else
  59. {
  60. int pipe_fds[2];
  61. if (pipe(pipe_fds) == 0)
  62. {
  63. read_descriptor_ = pipe_fds[0];
  64. ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
  65. write_descriptor_ = pipe_fds[1];
  66. ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
  67. }
  68. else
  69. {
  70. asio::error_code ec(errno,
  71. asio::error::get_system_category());
  72. asio::system_error e(ec, "eventfd_select_interrupter");
  73. boost::throw_exception(e);
  74. }
  75. }
  76. }
  77. // Destructor.
  78. ~eventfd_select_interrupter()
  79. {
  80. if (write_descriptor_ != -1 && write_descriptor_ != read_descriptor_)
  81. ::close(write_descriptor_);
  82. if (read_descriptor_ != -1)
  83. ::close(read_descriptor_);
  84. }
  85. // Interrupt the select call.
  86. void interrupt()
  87. {
  88. uint64_t counter(1UL);
  89. int result = ::write(write_descriptor_, &counter, sizeof(uint64_t));
  90. (void)result;
  91. }
  92. // Reset the select interrupt. Returns true if the call was interrupted.
  93. bool reset()
  94. {
  95. if (write_descriptor_ == read_descriptor_)
  96. {
  97. for (;;)
  98. {
  99. // Only perform one read. The kernel maintains an atomic counter.
  100. uint64_t counter(0);
  101. errno = 0;
  102. int bytes_read = ::read(read_descriptor_, &counter, sizeof(uint64_t));
  103. if (bytes_read < 0 && errno == EINTR)
  104. continue;
  105. bool was_interrupted = (bytes_read > 0);
  106. return was_interrupted;
  107. }
  108. }
  109. else
  110. {
  111. for (;;)
  112. {
  113. // Clear all data from the pipe.
  114. char data[1024];
  115. int bytes_read = ::read(read_descriptor_, data, sizeof(data));
  116. if (bytes_read < 0 && errno == EINTR)
  117. continue;
  118. bool was_interrupted = (bytes_read > 0);
  119. while (bytes_read == sizeof(data))
  120. bytes_read = ::read(read_descriptor_, data, sizeof(data));
  121. return was_interrupted;
  122. }
  123. }
  124. }
  125. // Get the read descriptor to be passed to select.
  126. int read_descriptor() const
  127. {
  128. return read_descriptor_;
  129. }
  130. private:
  131. // The read end of a connection used to interrupt the select call. This file
  132. // descriptor is passed to select such that when it is time to stop, a single
  133. // 64bit value will be written on the other end of the connection and this
  134. // descriptor will become readable.
  135. int read_descriptor_;
  136. // The write end of a connection used to interrupt the select call. A single
  137. // 64bit non-zero value may be written to this to wake up the select which is
  138. // waiting for the other end to become readable. This descriptor will only
  139. // differ from the read descriptor when a pipe is used.
  140. int write_descriptor_;
  141. };
  142. } // namespace detail
  143. } // namespace asio
  144. #endif // defined(ASIO_HAS_EVENTFD)
  145. #include "asio/detail/pop_options.hpp"
  146. #endif // ASIO_DETAIL_EVENTFD_SELECT_INTERRUPTER_HPP