Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/impl/reactive_descriptor_service.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_REACTIVE_DESCRIPTOR_SERVICE_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_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) \
0021   && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
0022   && !defined(__CYGWIN__) \
0023   && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
0024 
0025 #include <boost/asio/error.hpp>
0026 #include <boost/asio/detail/reactive_descriptor_service.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace detail {
0033 
0034 reactive_descriptor_service::reactive_descriptor_service(
0035     execution_context& context)
0036   : execution_context_service_base<reactive_descriptor_service>(context),
0037     reactor_(boost::asio::use_service<reactor>(context))
0038 {
0039   reactor_.init_task();
0040 }
0041 
0042 void reactive_descriptor_service::shutdown()
0043 {
0044 }
0045 
0046 void reactive_descriptor_service::construct(
0047     reactive_descriptor_service::implementation_type& impl)
0048 {
0049   impl.descriptor_ = -1;
0050   impl.state_ = 0;
0051   impl.reactor_data_ = reactor::per_descriptor_data();
0052 }
0053 
0054 void reactive_descriptor_service::move_construct(
0055     reactive_descriptor_service::implementation_type& impl,
0056     reactive_descriptor_service::implementation_type& other_impl)
0057   noexcept
0058 {
0059   impl.descriptor_ = other_impl.descriptor_;
0060   other_impl.descriptor_ = -1;
0061 
0062   impl.state_ = other_impl.state_;
0063   other_impl.state_ = 0;
0064 
0065   reactor_.move_descriptor(impl.descriptor_,
0066       impl.reactor_data_, other_impl.reactor_data_);
0067 }
0068 
0069 void reactive_descriptor_service::move_assign(
0070     reactive_descriptor_service::implementation_type& impl,
0071     reactive_descriptor_service& other_service,
0072     reactive_descriptor_service::implementation_type& other_impl)
0073 {
0074   destroy(impl);
0075 
0076   impl.descriptor_ = other_impl.descriptor_;
0077   other_impl.descriptor_ = -1;
0078 
0079   impl.state_ = other_impl.state_;
0080   other_impl.state_ = 0;
0081 
0082   other_service.reactor_.move_descriptor(impl.descriptor_,
0083       impl.reactor_data_, other_impl.reactor_data_);
0084 }
0085 
0086 void reactive_descriptor_service::destroy(
0087     reactive_descriptor_service::implementation_type& impl)
0088 {
0089   if (is_open(impl))
0090   {
0091     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0092           "descriptor", &impl, impl.descriptor_, "close"));
0093 
0094     reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
0095         (impl.state_ & descriptor_ops::possible_dup) == 0);
0096 
0097     boost::system::error_code ignored_ec;
0098     descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
0099 
0100     reactor_.cleanup_descriptor_data(impl.reactor_data_);
0101   }
0102 }
0103 
0104 boost::system::error_code reactive_descriptor_service::assign(
0105     reactive_descriptor_service::implementation_type& impl,
0106     const native_handle_type& native_descriptor, boost::system::error_code& ec)
0107 {
0108   if (is_open(impl))
0109   {
0110     ec = boost::asio::error::already_open;
0111     BOOST_ASIO_ERROR_LOCATION(ec);
0112     return ec;
0113   }
0114 
0115   if (int err = reactor_.register_descriptor(
0116         native_descriptor, impl.reactor_data_))
0117   {
0118     ec = boost::system::error_code(err,
0119         boost::asio::error::get_system_category());
0120     BOOST_ASIO_ERROR_LOCATION(ec);
0121     return ec;
0122   }
0123 
0124   impl.descriptor_ = native_descriptor;
0125   impl.state_ = descriptor_ops::possible_dup;
0126   ec = boost::system::error_code();
0127   return ec;
0128 }
0129 
0130 boost::system::error_code reactive_descriptor_service::close(
0131     reactive_descriptor_service::implementation_type& impl,
0132     boost::system::error_code& ec)
0133 {
0134   if (is_open(impl))
0135   {
0136     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0137           "descriptor", &impl, impl.descriptor_, "close"));
0138 
0139     reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
0140         (impl.state_ & descriptor_ops::possible_dup) == 0);
0141 
0142     descriptor_ops::close(impl.descriptor_, impl.state_, ec);
0143 
0144     reactor_.cleanup_descriptor_data(impl.reactor_data_);
0145   }
0146   else
0147   {
0148     ec = boost::system::error_code();
0149   }
0150 
0151   // The descriptor is closed by the OS even if close() returns an error.
0152   //
0153   // (Actually, POSIX says the state of the descriptor is unspecified. On
0154   // Linux the descriptor is apparently closed anyway; e.g. see
0155   //   http://lkml.org/lkml/2005/9/10/129
0156   // We'll just have to assume that other OSes follow the same behaviour.)
0157   construct(impl);
0158 
0159   BOOST_ASIO_ERROR_LOCATION(ec);
0160   return ec;
0161 }
0162 
0163 reactive_descriptor_service::native_handle_type
0164 reactive_descriptor_service::release(
0165     reactive_descriptor_service::implementation_type& impl)
0166 {
0167   native_handle_type descriptor = impl.descriptor_;
0168 
0169   if (is_open(impl))
0170   {
0171     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0172           "descriptor", &impl, impl.descriptor_, "release"));
0173 
0174     reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_, false);
0175     reactor_.cleanup_descriptor_data(impl.reactor_data_);
0176     construct(impl);
0177   }
0178 
0179   return descriptor;
0180 }
0181 
0182 boost::system::error_code reactive_descriptor_service::cancel(
0183     reactive_descriptor_service::implementation_type& impl,
0184     boost::system::error_code& ec)
0185 {
0186   if (!is_open(impl))
0187   {
0188     ec = boost::asio::error::bad_descriptor;
0189     BOOST_ASIO_ERROR_LOCATION(ec);
0190     return ec;
0191   }
0192 
0193   BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0194         "descriptor", &impl, impl.descriptor_, "cancel"));
0195 
0196   reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
0197   ec = boost::system::error_code();
0198   return ec;
0199 }
0200 
0201 void reactive_descriptor_service::do_start_op(implementation_type& impl,
0202     int op_type, reactor_op* op, bool is_continuation, bool is_non_blocking,
0203     bool noop, void (*on_immediate)(operation* op, bool, const void*),
0204     const void* immediate_arg)
0205 {
0206   if (!noop)
0207   {
0208     if ((impl.state_ & descriptor_ops::non_blocking) ||
0209         descriptor_ops::set_internal_non_blocking(
0210           impl.descriptor_, impl.state_, true, op->ec_))
0211     {
0212       reactor_.start_op(op_type, impl.descriptor_, impl.reactor_data_, op,
0213           is_continuation, is_non_blocking, on_immediate, immediate_arg);
0214       return;
0215     }
0216   }
0217 
0218   on_immediate(op, is_continuation, immediate_arg);
0219 }
0220 
0221 } // namespace detail
0222 } // namespace asio
0223 } // namespace boost
0224 
0225 #include <boost/asio/detail/pop_options.hpp>
0226 
0227 #endif // !defined(BOOST_ASIO_WINDOWS)
0228        //   && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
0229        //   && !defined(__CYGWIN__)
0230        //   && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
0231 
0232 #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP