2 // detail/impl/pipe_select_interrupter.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
21 #if !defined(BOOST_ASIO_WINDOWS)
22 #if !defined(__CYGWIN__)
23 #if !defined(__SYMBIAN32__)
24 #if !defined(BOOST_ASIO_HAS_EVENTFD)
28 #include <sys/types.h>
30 #include <boost/asio/detail/pipe_select_interrupter.hpp>
31 #include <boost/asio/detail/socket_types.hpp>
32 #include <boost/asio/detail/throw_error.hpp>
33 #include <boost/asio/error.hpp>
35 #include <boost/asio/detail/push_options.hpp>
41 pipe_select_interrupter::pipe_select_interrupter()
46 void pipe_select_interrupter::open_descriptors()
49 if (pipe(pipe_fds) == 0)
51 read_descriptor_ = pipe_fds[0];
52 ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
53 write_descriptor_ = pipe_fds[1];
54 ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
56 #if defined(FD_CLOEXEC)
57 ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
58 ::fcntl(write_descriptor_, F_SETFD, FD_CLOEXEC);
59 #endif // defined(FD_CLOEXEC)
63 boost::system::error_code ec(errno,
64 boost::asio::error::get_system_category());
65 boost::asio::detail::throw_error(ec, "pipe_select_interrupter");
69 pipe_select_interrupter::~pipe_select_interrupter()
74 void pipe_select_interrupter::close_descriptors()
76 if (read_descriptor_ != -1)
77 ::close(read_descriptor_);
78 if (write_descriptor_ != -1)
79 ::close(write_descriptor_);
82 void pipe_select_interrupter::recreate()
86 write_descriptor_ = -1;
87 read_descriptor_ = -1;
92 void pipe_select_interrupter::interrupt()
95 signed_size_type result = ::write(write_descriptor_, &byte, 1);
99 bool pipe_select_interrupter::reset()
104 signed_size_type bytes_read = ::read(read_descriptor_, data, sizeof(data));
105 if (bytes_read < 0 && errno == EINTR)
107 bool was_interrupted = (bytes_read > 0);
108 while (bytes_read == sizeof(data))
109 bytes_read = ::read(read_descriptor_, data, sizeof(data));
110 return was_interrupted;
114 } // namespace detail
118 #include <boost/asio/detail/pop_options.hpp>
120 #endif // !defined(BOOST_ASIO_HAS_EVENTFD)
121 #endif // !defined(__SYMBIAN32__)
122 #endif // !defined(__CYGWIN__)
123 #endif // !defined(BOOST_ASIO_WINDOWS)
124 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
126 #endif // BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP