io_uring_file_service.ipp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // detail/impl/io_uring_file_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IMPL_IO_URING_FILE_SERVICE_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_IO_URING_FILE_SERVICE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_FILE) \
  17. && defined(BOOST_ASIO_HAS_IO_URING)
  18. #include <cstring>
  19. #include <sys/stat.h>
  20. #include <boost/asio/detail/io_uring_file_service.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. io_uring_file_service::io_uring_file_service(
  26. execution_context& context)
  27. : execution_context_service_base<io_uring_file_service>(context),
  28. descriptor_service_(context)
  29. {
  30. }
  31. void io_uring_file_service::shutdown()
  32. {
  33. descriptor_service_.shutdown();
  34. }
  35. boost::system::error_code io_uring_file_service::open(
  36. io_uring_file_service::implementation_type& impl,
  37. const char* path, file_base::flags open_flags,
  38. boost::system::error_code& ec)
  39. {
  40. if (is_open(impl))
  41. {
  42. ec = boost::asio::error::already_open;
  43. BOOST_ASIO_ERROR_LOCATION(ec);
  44. return ec;
  45. }
  46. descriptor_ops::state_type state = 0;
  47. int fd = descriptor_ops::open(path, static_cast<int>(open_flags), 0777, ec);
  48. if (fd < 0)
  49. {
  50. BOOST_ASIO_ERROR_LOCATION(ec);
  51. return ec;
  52. }
  53. // We're done. Take ownership of the serial port descriptor.
  54. if (descriptor_service_.assign(impl, fd, ec))
  55. {
  56. boost::system::error_code ignored_ec;
  57. descriptor_ops::close(fd, state, ignored_ec);
  58. }
  59. (void)::posix_fadvise(native_handle(impl), 0, 0,
  60. impl.is_stream_ ? POSIX_FADV_SEQUENTIAL : POSIX_FADV_RANDOM);
  61. BOOST_ASIO_ERROR_LOCATION(ec);
  62. return ec;
  63. }
  64. uint64_t io_uring_file_service::size(
  65. const io_uring_file_service::implementation_type& impl,
  66. boost::system::error_code& ec) const
  67. {
  68. struct stat s;
  69. int result = ::fstat(native_handle(impl), &s);
  70. descriptor_ops::get_last_error(ec, result != 0);
  71. BOOST_ASIO_ERROR_LOCATION(ec);
  72. return !ec ? s.st_size : 0;
  73. }
  74. boost::system::error_code io_uring_file_service::resize(
  75. io_uring_file_service::implementation_type& impl,
  76. uint64_t n, boost::system::error_code& ec)
  77. {
  78. int result = ::ftruncate(native_handle(impl), n);
  79. descriptor_ops::get_last_error(ec, result != 0);
  80. BOOST_ASIO_ERROR_LOCATION(ec);
  81. return ec;
  82. }
  83. boost::system::error_code io_uring_file_service::sync_all(
  84. io_uring_file_service::implementation_type& impl,
  85. boost::system::error_code& ec)
  86. {
  87. int result = ::fsync(native_handle(impl));
  88. descriptor_ops::get_last_error(ec, result != 0);
  89. return ec;
  90. }
  91. boost::system::error_code io_uring_file_service::sync_data(
  92. io_uring_file_service::implementation_type& impl,
  93. boost::system::error_code& ec)
  94. {
  95. #if defined(_POSIX_SYNCHRONIZED_IO)
  96. int result = ::fdatasync(native_handle(impl));
  97. #else // defined(_POSIX_SYNCHRONIZED_IO)
  98. int result = ::fsync(native_handle(impl));
  99. #endif // defined(_POSIX_SYNCHRONIZED_IO)
  100. descriptor_ops::get_last_error(ec, result != 0);
  101. BOOST_ASIO_ERROR_LOCATION(ec);
  102. return ec;
  103. }
  104. uint64_t io_uring_file_service::seek(
  105. io_uring_file_service::implementation_type& impl, int64_t offset,
  106. file_base::seek_basis whence, boost::system::error_code& ec)
  107. {
  108. int64_t result = ::lseek(native_handle(impl), offset, whence);
  109. descriptor_ops::get_last_error(ec, result < 0);
  110. BOOST_ASIO_ERROR_LOCATION(ec);
  111. return !ec ? static_cast<uint64_t>(result) : 0;
  112. }
  113. } // namespace detail
  114. } // namespace asio
  115. } // namespace boost
  116. #include <boost/asio/detail/pop_options.hpp>
  117. #endif // defined(BOOST_ASIO_HAS_FILE)
  118. // && defined(BOOST_ASIO_HAS_IO_URING)
  119. #endif // BOOST_ASIO_DETAIL_IMPL_IO_URING_FILE_SERVICE_IPP