Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:32

0001 //
0002 // detail/impl/pipe_select_interrupter.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
0021 #if !defined(BOOST_ASIO_WINDOWS)
0022 #if !defined(__CYGWIN__)
0023 #if !defined(__SYMBIAN32__)
0024 #if !defined(BOOST_ASIO_HAS_EVENTFD)
0025 
0026 #include <fcntl.h>
0027 #include <sys/stat.h>
0028 #include <sys/types.h>
0029 #include <unistd.h>
0030 #include <boost/asio/detail/pipe_select_interrupter.hpp>
0031 #include <boost/asio/detail/socket_types.hpp>
0032 #include <boost/asio/detail/throw_error.hpp>
0033 #include <boost/asio/error.hpp>
0034 
0035 #include <boost/asio/detail/push_options.hpp>
0036 
0037 namespace boost {
0038 namespace asio {
0039 namespace detail {
0040 
0041 pipe_select_interrupter::pipe_select_interrupter()
0042 {
0043   open_descriptors();
0044 }
0045 
0046 void pipe_select_interrupter::open_descriptors()
0047 {
0048   int pipe_fds[2];
0049   if (pipe(pipe_fds) == 0)
0050   {
0051     read_descriptor_ = pipe_fds[0];
0052     ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
0053     write_descriptor_ = pipe_fds[1];
0054     ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
0055 
0056 #if defined(FD_CLOEXEC)
0057     ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
0058     ::fcntl(write_descriptor_, F_SETFD, FD_CLOEXEC);
0059 #endif // defined(FD_CLOEXEC)
0060   }
0061   else
0062   {
0063     boost::system::error_code ec(errno,
0064         boost::asio::error::get_system_category());
0065     boost::asio::detail::throw_error(ec, "pipe_select_interrupter");
0066   }
0067 }
0068 
0069 pipe_select_interrupter::~pipe_select_interrupter()
0070 {
0071   close_descriptors();
0072 }
0073 
0074 void pipe_select_interrupter::close_descriptors()
0075 {
0076   if (read_descriptor_ != -1)
0077     ::close(read_descriptor_);
0078   if (write_descriptor_ != -1)
0079     ::close(write_descriptor_);
0080 }
0081 
0082 void pipe_select_interrupter::recreate()
0083 {
0084   close_descriptors();
0085 
0086   write_descriptor_ = -1;
0087   read_descriptor_ = -1;
0088 
0089   open_descriptors();
0090 }
0091 
0092 void pipe_select_interrupter::interrupt()
0093 {
0094   char byte = 0;
0095   signed_size_type result = ::write(write_descriptor_, &byte, 1);
0096   (void)result;
0097 }
0098 
0099 bool pipe_select_interrupter::reset()
0100 {
0101   for (;;)
0102   {
0103     char data[1024];
0104     signed_size_type bytes_read = ::read(read_descriptor_, data, sizeof(data));
0105     if (bytes_read == sizeof(data))
0106       continue;
0107     if (bytes_read > 0)
0108       return true;
0109     if (bytes_read == 0)
0110       return false;
0111     if (errno == EINTR)
0112       continue;
0113     if (errno == EWOULDBLOCK || errno == EAGAIN)
0114       return true;
0115     return false;
0116   }
0117 }
0118 
0119 } // namespace detail
0120 } // namespace asio
0121 } // namespace boost
0122 
0123 #include <boost/asio/detail/pop_options.hpp>
0124 
0125 #endif // !defined(BOOST_ASIO_HAS_EVENTFD)
0126 #endif // !defined(__SYMBIAN32__)
0127 #endif // !defined(__CYGWIN__)
0128 #endif // !defined(BOOST_ASIO_WINDOWS)
0129 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
0130 
0131 #endif // BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP