Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/io_uring_file_service.hpp
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_IO_URING_FILE_SERVICE_HPP
0012 #define BOOST_ASIO_DETAIL_IO_URING_FILE_SERVICE_HPP
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_FILE) \
0021   && defined(BOOST_ASIO_HAS_IO_URING)
0022 
0023 #include <string>
0024 #include <boost/asio/detail/cstdint.hpp>
0025 #include <boost/asio/detail/descriptor_ops.hpp>
0026 #include <boost/asio/detail/io_uring_descriptor_service.hpp>
0027 #include <boost/asio/error.hpp>
0028 #include <boost/asio/execution_context.hpp>
0029 #include <boost/asio/file_base.hpp>
0030 
0031 #include <boost/asio/detail/push_options.hpp>
0032 
0033 namespace boost {
0034 namespace asio {
0035 namespace detail {
0036 
0037 // Extend the io_uring_descriptor_service to provide file support.
0038 class io_uring_file_service :
0039   public execution_context_service_base<io_uring_file_service>
0040 {
0041 public:
0042   typedef io_uring_descriptor_service descriptor_service;
0043 
0044   // The native type of a file.
0045   typedef descriptor_service::native_handle_type native_handle_type;
0046 
0047   // The implementation type of the file.
0048   class implementation_type : descriptor_service::implementation_type
0049   {
0050   private:
0051     // Only this service will have access to the internal values.
0052     friend class io_uring_file_service;
0053 
0054     bool is_stream_;
0055   };
0056 
0057   BOOST_ASIO_DECL io_uring_file_service(execution_context& context);
0058 
0059   // Destroy all user-defined handler objects owned by the service.
0060   BOOST_ASIO_DECL void shutdown();
0061 
0062   // Construct a new file implementation.
0063   void construct(implementation_type& impl)
0064   {
0065     descriptor_service_.construct(impl);
0066     impl.is_stream_ = false;
0067   }
0068 
0069   // Move-construct a new file implementation.
0070   void move_construct(implementation_type& impl,
0071       implementation_type& other_impl)
0072   {
0073     descriptor_service_.move_construct(impl, other_impl);
0074     impl.is_stream_ = other_impl.is_stream_;
0075   }
0076 
0077   // Move-assign from another file implementation.
0078   void move_assign(implementation_type& impl,
0079       io_uring_file_service& other_service,
0080       implementation_type& other_impl)
0081   {
0082     descriptor_service_.move_assign(impl,
0083         other_service.descriptor_service_, other_impl);
0084     impl.is_stream_ = other_impl.is_stream_;
0085   }
0086 
0087   // Destroy a file implementation.
0088   void destroy(implementation_type& impl)
0089   {
0090     descriptor_service_.destroy(impl);
0091   }
0092 
0093   // Open the file using the specified path name.
0094   BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
0095       const char* path, file_base::flags open_flags,
0096       boost::system::error_code& ec);
0097 
0098   // Assign a native descriptor to a file implementation.
0099   boost::system::error_code assign(implementation_type& impl,
0100       const native_handle_type& native_descriptor,
0101       boost::system::error_code& ec)
0102   {
0103     return descriptor_service_.assign(impl, native_descriptor, ec);
0104   }
0105 
0106   // Set whether the implementation is stream-oriented.
0107   void set_is_stream(implementation_type& impl, bool is_stream)
0108   {
0109     impl.is_stream_ = is_stream;
0110   }
0111 
0112   // Determine whether the file is open.
0113   bool is_open(const implementation_type& impl) const
0114   {
0115     return descriptor_service_.is_open(impl);
0116   }
0117 
0118   // Destroy a file implementation.
0119   boost::system::error_code close(implementation_type& impl,
0120       boost::system::error_code& ec)
0121   {
0122     return descriptor_service_.close(impl, ec);
0123   }
0124 
0125   // Get the native file representation.
0126   native_handle_type native_handle(const implementation_type& impl) const
0127   {
0128     return descriptor_service_.native_handle(impl);
0129   }
0130 
0131   // Release ownership of the native descriptor representation.
0132   native_handle_type release(implementation_type& impl,
0133       boost::system::error_code& ec)
0134   {
0135     return descriptor_service_.release(impl, ec);
0136   }
0137 
0138   // Cancel all operations associated with the file.
0139   boost::system::error_code cancel(implementation_type& impl,
0140       boost::system::error_code& ec)
0141   {
0142     return descriptor_service_.cancel(impl, ec);
0143   }
0144 
0145   // Get the size of the file.
0146   BOOST_ASIO_DECL uint64_t size(const implementation_type& impl,
0147       boost::system::error_code& ec) const;
0148 
0149   // Alter the size of the file.
0150   BOOST_ASIO_DECL boost::system::error_code resize(implementation_type& impl,
0151       uint64_t n, boost::system::error_code& ec);
0152 
0153   // Synchronise the file to disk.
0154   BOOST_ASIO_DECL boost::system::error_code sync_all(implementation_type& impl,
0155       boost::system::error_code& ec);
0156 
0157   // Synchronise the file data to disk.
0158   BOOST_ASIO_DECL boost::system::error_code sync_data(implementation_type& impl,
0159       boost::system::error_code& ec);
0160 
0161   // Seek to a position in the file.
0162   BOOST_ASIO_DECL uint64_t seek(implementation_type& impl, int64_t offset,
0163       file_base::seek_basis whence, boost::system::error_code& ec);
0164 
0165   // Write the given data. Returns the number of bytes written.
0166   template <typename ConstBufferSequence>
0167   size_t write_some(implementation_type& impl,
0168       const ConstBufferSequence& buffers, boost::system::error_code& ec)
0169   {
0170     return descriptor_service_.write_some(impl, buffers, ec);
0171   }
0172 
0173   // Start an asynchronous write. The data being written must be valid for the
0174   // lifetime of the asynchronous operation.
0175   template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
0176   void async_write_some(implementation_type& impl,
0177       const ConstBufferSequence& buffers,
0178       Handler& handler, const IoExecutor& io_ex)
0179   {
0180     descriptor_service_.async_write_some(impl, buffers, handler, io_ex);
0181   }
0182 
0183   // Write the given data at the specified location. Returns the number of
0184   // bytes written.
0185   template <typename ConstBufferSequence>
0186   size_t write_some_at(implementation_type& impl, uint64_t offset,
0187       const ConstBufferSequence& buffers, boost::system::error_code& ec)
0188   {
0189     return descriptor_service_.write_some_at(impl, offset, buffers, ec);
0190   }
0191 
0192   // Start an asynchronous write at the specified location. The data being
0193   // written must be valid for the lifetime of the asynchronous operation.
0194   template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
0195   void async_write_some_at(implementation_type& impl,
0196       uint64_t offset, const ConstBufferSequence& buffers,
0197       Handler& handler, const IoExecutor& io_ex)
0198   {
0199     descriptor_service_.async_write_some_at(
0200         impl, offset, buffers, handler, io_ex);
0201   }
0202 
0203   // Read some data. Returns the number of bytes read.
0204   template <typename MutableBufferSequence>
0205   size_t read_some(implementation_type& impl,
0206       const MutableBufferSequence& buffers, boost::system::error_code& ec)
0207   {
0208     return descriptor_service_.read_some(impl, buffers, ec);
0209   }
0210 
0211   // Start an asynchronous read. The buffer for the data being read must be
0212   // valid for the lifetime of the asynchronous operation.
0213   template <typename MutableBufferSequence,
0214       typename Handler, typename IoExecutor>
0215   void async_read_some(implementation_type& impl,
0216       const MutableBufferSequence& buffers,
0217       Handler& handler, const IoExecutor& io_ex)
0218   {
0219     descriptor_service_.async_read_some(impl, buffers, handler, io_ex);
0220   }
0221 
0222   // Read some data. Returns the number of bytes read.
0223   template <typename MutableBufferSequence>
0224   size_t read_some_at(implementation_type& impl, uint64_t offset,
0225       const MutableBufferSequence& buffers, boost::system::error_code& ec)
0226   {
0227     return descriptor_service_.read_some_at(impl, offset, buffers, ec);
0228   }
0229 
0230   // Start an asynchronous read. The buffer for the data being read must be
0231   // valid for the lifetime of the asynchronous operation.
0232   template <typename MutableBufferSequence,
0233       typename Handler, typename IoExecutor>
0234   void async_read_some_at(implementation_type& impl,
0235       uint64_t offset, const MutableBufferSequence& buffers,
0236       Handler& handler, const IoExecutor& io_ex)
0237   {
0238     descriptor_service_.async_read_some_at(
0239         impl, offset, buffers, handler, io_ex);
0240   }
0241 
0242 private:
0243   // The implementation used for initiating asynchronous operations.
0244   descriptor_service descriptor_service_;
0245 
0246   // Cached success value to avoid accessing category singleton.
0247   const boost::system::error_code success_ec_;
0248 };
0249 
0250 } // namespace detail
0251 } // namespace asio
0252 } // namespace boost
0253 
0254 #include <boost/asio/detail/pop_options.hpp>
0255 
0256 #if defined(BOOST_ASIO_HEADER_ONLY)
0257 # include <boost/asio/detail/impl/io_uring_file_service.ipp>
0258 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0259 
0260 #endif // defined(BOOST_ASIO_HAS_FILE)
0261        //   && defined(BOOST_ASIO_HAS_IO_URING)
0262 
0263 #endif // BOOST_ASIO_DETAIL_IO_URING_FILE_SERVICE_HPP