Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/impl/io_uring_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_IO_URING_DESCRIPTOR_SERVICE_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_IO_URING_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_HAS_IO_URING)
0021 
0022 #include <boost/asio/error.hpp>
0023 #include <boost/asio/detail/io_uring_descriptor_service.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace detail {
0030 
0031 io_uring_descriptor_service::io_uring_descriptor_service(
0032     execution_context& context)
0033   : execution_context_service_base<io_uring_descriptor_service>(context),
0034     io_uring_service_(boost::asio::use_service<io_uring_service>(context))
0035 {
0036   io_uring_service_.init_task();
0037 }
0038 
0039 void io_uring_descriptor_service::shutdown()
0040 {
0041 }
0042 
0043 void io_uring_descriptor_service::construct(
0044     io_uring_descriptor_service::implementation_type& impl)
0045 {
0046   impl.descriptor_ = -1;
0047   impl.state_ = 0;
0048   impl.io_object_data_ = 0;
0049 }
0050 
0051 void io_uring_descriptor_service::move_construct(
0052     io_uring_descriptor_service::implementation_type& impl,
0053     io_uring_descriptor_service::implementation_type& other_impl)
0054   noexcept
0055 {
0056   impl.descriptor_ = other_impl.descriptor_;
0057   other_impl.descriptor_ = -1;
0058 
0059   impl.state_ = other_impl.state_;
0060   other_impl.state_ = 0;
0061 
0062   impl.io_object_data_ = other_impl.io_object_data_;
0063   other_impl.io_object_data_ = 0;
0064 }
0065 
0066 void io_uring_descriptor_service::move_assign(
0067     io_uring_descriptor_service::implementation_type& impl,
0068     io_uring_descriptor_service& /*other_service*/,
0069     io_uring_descriptor_service::implementation_type& other_impl)
0070 {
0071   destroy(impl);
0072 
0073   impl.descriptor_ = other_impl.descriptor_;
0074   other_impl.descriptor_ = -1;
0075 
0076   impl.state_ = other_impl.state_;
0077   other_impl.state_ = 0;
0078 
0079   impl.io_object_data_ = other_impl.io_object_data_;
0080   other_impl.io_object_data_ = 0;
0081 }
0082 
0083 void io_uring_descriptor_service::destroy(
0084     io_uring_descriptor_service::implementation_type& impl)
0085 {
0086   if (is_open(impl))
0087   {
0088     BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(),
0089           "descriptor", &impl, impl.descriptor_, "close"));
0090 
0091     io_uring_service_.deregister_io_object(impl.io_object_data_);
0092     boost::system::error_code ignored_ec;
0093     descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
0094     io_uring_service_.cleanup_io_object(impl.io_object_data_);
0095   }
0096 }
0097 
0098 boost::system::error_code io_uring_descriptor_service::assign(
0099     io_uring_descriptor_service::implementation_type& impl,
0100     const native_handle_type& native_descriptor, boost::system::error_code& ec)
0101 {
0102   if (is_open(impl))
0103   {
0104     ec = boost::asio::error::already_open;
0105     BOOST_ASIO_ERROR_LOCATION(ec);
0106     return ec;
0107   }
0108 
0109   io_uring_service_.register_io_object(impl.io_object_data_);
0110 
0111   impl.descriptor_ = native_descriptor;
0112   impl.state_ = descriptor_ops::possible_dup;
0113   ec = success_ec_;
0114   return ec;
0115 }
0116 
0117 boost::system::error_code io_uring_descriptor_service::close(
0118     io_uring_descriptor_service::implementation_type& impl,
0119     boost::system::error_code& ec)
0120 {
0121   if (is_open(impl))
0122   {
0123     BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(),
0124           "descriptor", &impl, impl.descriptor_, "close"));
0125 
0126     io_uring_service_.deregister_io_object(impl.io_object_data_);
0127     descriptor_ops::close(impl.descriptor_, impl.state_, ec);
0128     io_uring_service_.cleanup_io_object(impl.io_object_data_);
0129   }
0130   else
0131   {
0132     ec = success_ec_;
0133   }
0134 
0135   // The descriptor is closed by the OS even if close() returns an error.
0136   //
0137   // (Actually, POSIX says the state of the descriptor is unspecified. On
0138   // Linux the descriptor is apparently closed anyway; e.g. see
0139   //   http://lkml.org/lkml/2005/9/10/129
0140   construct(impl);
0141 
0142   BOOST_ASIO_ERROR_LOCATION(ec);
0143   return ec;
0144 }
0145 
0146 io_uring_descriptor_service::native_handle_type
0147 io_uring_descriptor_service::release(
0148     io_uring_descriptor_service::implementation_type& impl)
0149 {
0150   native_handle_type descriptor = impl.descriptor_;
0151 
0152   if (is_open(impl))
0153   {
0154     BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(),
0155           "descriptor", &impl, impl.descriptor_, "release"));
0156 
0157     io_uring_service_.deregister_io_object(impl.io_object_data_);
0158     io_uring_service_.cleanup_io_object(impl.io_object_data_);
0159     construct(impl);
0160   }
0161 
0162   return descriptor;
0163 }
0164 
0165 boost::system::error_code io_uring_descriptor_service::cancel(
0166     io_uring_descriptor_service::implementation_type& impl,
0167     boost::system::error_code& ec)
0168 {
0169   if (!is_open(impl))
0170   {
0171     ec = boost::asio::error::bad_descriptor;
0172     BOOST_ASIO_ERROR_LOCATION(ec);
0173     return ec;
0174   }
0175 
0176   BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(),
0177         "descriptor", &impl, impl.descriptor_, "cancel"));
0178 
0179   io_uring_service_.cancel_ops(impl.io_object_data_);
0180   ec = success_ec_;
0181   return ec;
0182 }
0183 
0184 void io_uring_descriptor_service::start_op(
0185     io_uring_descriptor_service::implementation_type& impl,
0186     int op_type, io_uring_operation* op, bool is_continuation, bool noop)
0187 {
0188   if (!noop)
0189   {
0190     io_uring_service_.start_op(op_type,
0191         impl.io_object_data_, op, is_continuation);
0192   }
0193   else
0194   {
0195     io_uring_service_.post_immediate_completion(op, is_continuation);
0196   }
0197 }
0198 
0199 } // namespace detail
0200 } // namespace asio
0201 } // namespace boost
0202 
0203 #include <boost/asio/detail/pop_options.hpp>
0204 
0205 #endif // defined(BOOST_ASIO_HAS_IO_URING)
0206 
0207 #endif // BOOST_ASIO_DETAIL_IMPL_IO_URING_DESCRIPTOR_SERVICE_IPP