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_file_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_FILE_SERVICE_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_IO_URING_FILE_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_FILE) \
0021   && defined(BOOST_ASIO_HAS_IO_URING)
0022 
0023 #include <cstring>
0024 #include <sys/stat.h>
0025 #include <boost/asio/detail/io_uring_file_service.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace detail {
0032 
0033 io_uring_file_service::io_uring_file_service(
0034     execution_context& context)
0035   : execution_context_service_base<io_uring_file_service>(context),
0036     descriptor_service_(context)
0037 {
0038 }
0039 
0040 void io_uring_file_service::shutdown()
0041 {
0042   descriptor_service_.shutdown();
0043 }
0044 
0045 boost::system::error_code io_uring_file_service::open(
0046     io_uring_file_service::implementation_type& impl,
0047     const char* path, file_base::flags open_flags,
0048     boost::system::error_code& ec)
0049 {
0050   if (is_open(impl))
0051   {
0052     ec = boost::asio::error::already_open;
0053     BOOST_ASIO_ERROR_LOCATION(ec);
0054     return ec;
0055   }
0056 
0057   descriptor_ops::state_type state = 0;
0058   int fd = descriptor_ops::open(path, static_cast<int>(open_flags), 0777, ec);
0059   if (fd < 0)
0060   {
0061     BOOST_ASIO_ERROR_LOCATION(ec);
0062     return ec;
0063   }
0064 
0065   // We're done. Take ownership of the serial port descriptor.
0066   if (descriptor_service_.assign(impl, fd, ec))
0067   {
0068     boost::system::error_code ignored_ec;
0069     descriptor_ops::close(fd, state, ignored_ec);
0070   }
0071 
0072   (void)::posix_fadvise(native_handle(impl), 0, 0,
0073       impl.is_stream_ ? POSIX_FADV_SEQUENTIAL : POSIX_FADV_RANDOM);
0074 
0075   BOOST_ASIO_ERROR_LOCATION(ec);
0076   return ec;
0077 }
0078 
0079 uint64_t io_uring_file_service::size(
0080     const io_uring_file_service::implementation_type& impl,
0081     boost::system::error_code& ec) const
0082 {
0083   struct stat s;
0084   int result = ::fstat(native_handle(impl), &s);
0085   descriptor_ops::get_last_error(ec, result != 0);
0086   BOOST_ASIO_ERROR_LOCATION(ec);
0087   return !ec ? s.st_size : 0;
0088 }
0089 
0090 boost::system::error_code io_uring_file_service::resize(
0091     io_uring_file_service::implementation_type& impl,
0092     uint64_t n, boost::system::error_code& ec)
0093 {
0094   int result = ::ftruncate(native_handle(impl), n);
0095   descriptor_ops::get_last_error(ec, result != 0);
0096   BOOST_ASIO_ERROR_LOCATION(ec);
0097   return ec;
0098 }
0099 
0100 boost::system::error_code io_uring_file_service::sync_all(
0101     io_uring_file_service::implementation_type& impl,
0102     boost::system::error_code& ec)
0103 {
0104   int result = ::fsync(native_handle(impl));
0105   descriptor_ops::get_last_error(ec, result != 0);
0106   return ec;
0107 }
0108 
0109 boost::system::error_code io_uring_file_service::sync_data(
0110     io_uring_file_service::implementation_type& impl,
0111     boost::system::error_code& ec)
0112 {
0113 #if defined(_POSIX_SYNCHRONIZED_IO)
0114   int result = ::fdatasync(native_handle(impl));
0115 #else // defined(_POSIX_SYNCHRONIZED_IO)
0116   int result = ::fsync(native_handle(impl));
0117 #endif // defined(_POSIX_SYNCHRONIZED_IO)
0118   descriptor_ops::get_last_error(ec, result != 0);
0119   BOOST_ASIO_ERROR_LOCATION(ec);
0120   return ec;
0121 }
0122 
0123 uint64_t io_uring_file_service::seek(
0124     io_uring_file_service::implementation_type& impl, int64_t offset,
0125     file_base::seek_basis whence, boost::system::error_code& ec)
0126 {
0127   int64_t result = ::lseek(native_handle(impl), offset, whence);
0128   descriptor_ops::get_last_error(ec, result < 0);
0129   BOOST_ASIO_ERROR_LOCATION(ec);
0130   return !ec ? static_cast<uint64_t>(result) : 0;
0131 }
0132 
0133 } // namespace detail
0134 } // namespace asio
0135 } // namespace boost
0136 
0137 #include <boost/asio/detail/pop_options.hpp>
0138 
0139 #endif // defined(BOOST_ASIO_HAS_FILE)
0140        //   && defined(BOOST_ASIO_HAS_IO_URING)
0141 
0142 #endif // BOOST_ASIO_DETAIL_IMPL_IO_URING_FILE_SERVICE_IPP