annotate DEPENDENCIES/generic/include/boost/asio/detail/impl/eventfd_select_interrupter.ipp @ 133:4acb5d8d80b6 tip

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