io_uring_descriptor_read_at_op.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // detail/io_uring_descriptor_read_at_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_AT_OP_HPP
  11. #define ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_AT_OP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_IO_URING)
  17. #include "asio/detail/bind_handler.hpp"
  18. #include "asio/detail/buffer_sequence_adapter.hpp"
  19. #include "asio/detail/cstdint.hpp"
  20. #include "asio/detail/descriptor_ops.hpp"
  21. #include "asio/detail/fenced_block.hpp"
  22. #include "asio/detail/handler_work.hpp"
  23. #include "asio/detail/io_uring_operation.hpp"
  24. #include "asio/detail/memory.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. namespace detail {
  28. template <typename MutableBufferSequence>
  29. class io_uring_descriptor_read_at_op_base : public io_uring_operation
  30. {
  31. public:
  32. io_uring_descriptor_read_at_op_base(
  33. const asio::error_code& success_ec, int descriptor,
  34. descriptor_ops::state_type state, uint64_t offset,
  35. const MutableBufferSequence& buffers, func_type complete_func)
  36. : io_uring_operation(success_ec,
  37. &io_uring_descriptor_read_at_op_base::do_prepare,
  38. &io_uring_descriptor_read_at_op_base::do_perform, complete_func),
  39. descriptor_(descriptor),
  40. state_(state),
  41. offset_(offset),
  42. buffers_(buffers),
  43. bufs_(buffers)
  44. {
  45. }
  46. static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
  47. {
  48. ASIO_ASSUME(base != 0);
  49. io_uring_descriptor_read_at_op_base* o(
  50. static_cast<io_uring_descriptor_read_at_op_base*>(base));
  51. if ((o->state_ & descriptor_ops::internal_non_blocking) != 0)
  52. {
  53. ::io_uring_prep_poll_add(sqe, o->descriptor_, POLLIN);
  54. }
  55. else if (o->bufs_.is_single_buffer && o->bufs_.is_registered_buffer)
  56. {
  57. ::io_uring_prep_read_fixed(sqe, o->descriptor_,
  58. o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len,
  59. o->offset_, o->bufs_.registered_id().native_handle());
  60. }
  61. else
  62. {
  63. ::io_uring_prep_readv(sqe, o->descriptor_,
  64. o->bufs_.buffers(), o->bufs_.count(), o->offset_);
  65. }
  66. }
  67. static bool do_perform(io_uring_operation* base, bool after_completion)
  68. {
  69. ASIO_ASSUME(base != 0);
  70. io_uring_descriptor_read_at_op_base* o(
  71. static_cast<io_uring_descriptor_read_at_op_base*>(base));
  72. if ((o->state_ & descriptor_ops::internal_non_blocking) != 0)
  73. {
  74. if (o->bufs_.is_single_buffer)
  75. {
  76. return descriptor_ops::non_blocking_read_at1(o->descriptor_,
  77. o->offset_, o->bufs_.first(o->buffers_).data(),
  78. o->bufs_.first(o->buffers_).size(), o->ec_,
  79. o->bytes_transferred_);
  80. }
  81. else
  82. {
  83. return descriptor_ops::non_blocking_read_at(o->descriptor_,
  84. o->offset_, o->bufs_.buffers(), o->bufs_.count(),
  85. o->ec_, o->bytes_transferred_);
  86. }
  87. }
  88. else if (after_completion)
  89. {
  90. if (!o->ec_ && o->bytes_transferred_ == 0)
  91. o->ec_ = asio::error::eof;
  92. }
  93. if (o->ec_ && o->ec_ == asio::error::would_block)
  94. {
  95. o->state_ |= descriptor_ops::internal_non_blocking;
  96. return false;
  97. }
  98. return after_completion;
  99. }
  100. private:
  101. int descriptor_;
  102. descriptor_ops::state_type state_;
  103. uint64_t offset_;
  104. MutableBufferSequence buffers_;
  105. buffer_sequence_adapter<asio::mutable_buffer,
  106. MutableBufferSequence> bufs_;
  107. };
  108. template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
  109. class io_uring_descriptor_read_at_op
  110. : public io_uring_descriptor_read_at_op_base<MutableBufferSequence>
  111. {
  112. public:
  113. ASIO_DEFINE_HANDLER_PTR(io_uring_descriptor_read_at_op);
  114. io_uring_descriptor_read_at_op(const asio::error_code& success_ec,
  115. int descriptor, descriptor_ops::state_type state, uint64_t offset,
  116. const MutableBufferSequence& buffers,
  117. Handler& handler, const IoExecutor& io_ex)
  118. : io_uring_descriptor_read_at_op_base<MutableBufferSequence>(
  119. success_ec, descriptor, state, offset, buffers,
  120. &io_uring_descriptor_read_at_op::do_complete),
  121. handler_(static_cast<Handler&&>(handler)),
  122. work_(handler_, io_ex)
  123. {
  124. }
  125. static void do_complete(void* owner, operation* base,
  126. const asio::error_code& /*ec*/,
  127. std::size_t /*bytes_transferred*/)
  128. {
  129. // Take ownership of the handler object.
  130. ASIO_ASSUME(base != 0);
  131. io_uring_descriptor_read_at_op* o
  132. (static_cast<io_uring_descriptor_read_at_op*>(base));
  133. ptr p = { asio::detail::addressof(o->handler_), o, o };
  134. ASIO_HANDLER_COMPLETION((*o));
  135. // Take ownership of the operation's outstanding work.
  136. handler_work<Handler, IoExecutor> w(
  137. static_cast<handler_work<Handler, IoExecutor>&&>(
  138. o->work_));
  139. ASIO_ERROR_LOCATION(o->ec_);
  140. // Make a copy of the handler so that the memory can be deallocated before
  141. // the upcall is made. Even if we're not about to make an upcall, a
  142. // sub-object of the handler may be the true owner of the memory associated
  143. // with the handler. Consequently, a local copy of the handler is required
  144. // to ensure that any owning sub-object remains valid until after we have
  145. // deallocated the memory here.
  146. detail::binder2<Handler, asio::error_code, std::size_t>
  147. handler(o->handler_, o->ec_, o->bytes_transferred_);
  148. p.h = asio::detail::addressof(handler.handler_);
  149. p.reset();
  150. // Make the upcall if required.
  151. if (owner)
  152. {
  153. fenced_block b(fenced_block::half);
  154. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  155. w.complete(handler, handler.handler_);
  156. ASIO_HANDLER_INVOCATION_END;
  157. }
  158. }
  159. private:
  160. Handler handler_;
  161. handler_work<Handler, IoExecutor> work_;
  162. };
  163. } // namespace detail
  164. } // namespace asio
  165. #include "asio/detail/pop_options.hpp"
  166. #endif // defined(ASIO_HAS_IO_URING)
  167. #endif // ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_AT_OP_HPP