reactive_descriptor_service.ipp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // detail/impl/reactive_descriptor_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_REACTIVE_DESCRIPTOR_SERVICE_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_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_WINDOWS) \
  17. && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
  18. && !defined(__CYGWIN__) \
  19. && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. reactive_descriptor_service::reactive_descriptor_service(
  27. execution_context& context)
  28. : execution_context_service_base<reactive_descriptor_service>(context),
  29. reactor_(boost::asio::use_service<reactor>(context))
  30. {
  31. reactor_.init_task();
  32. }
  33. void reactive_descriptor_service::shutdown()
  34. {
  35. }
  36. void reactive_descriptor_service::construct(
  37. reactive_descriptor_service::implementation_type& impl)
  38. {
  39. impl.descriptor_ = -1;
  40. impl.state_ = 0;
  41. impl.reactor_data_ = reactor::per_descriptor_data();
  42. }
  43. void reactive_descriptor_service::move_construct(
  44. reactive_descriptor_service::implementation_type& impl,
  45. reactive_descriptor_service::implementation_type& other_impl)
  46. noexcept
  47. {
  48. impl.descriptor_ = other_impl.descriptor_;
  49. other_impl.descriptor_ = -1;
  50. impl.state_ = other_impl.state_;
  51. other_impl.state_ = 0;
  52. reactor_.move_descriptor(impl.descriptor_,
  53. impl.reactor_data_, other_impl.reactor_data_);
  54. }
  55. void reactive_descriptor_service::move_assign(
  56. reactive_descriptor_service::implementation_type& impl,
  57. reactive_descriptor_service& other_service,
  58. reactive_descriptor_service::implementation_type& other_impl)
  59. {
  60. destroy(impl);
  61. impl.descriptor_ = other_impl.descriptor_;
  62. other_impl.descriptor_ = -1;
  63. impl.state_ = other_impl.state_;
  64. other_impl.state_ = 0;
  65. other_service.reactor_.move_descriptor(impl.descriptor_,
  66. impl.reactor_data_, other_impl.reactor_data_);
  67. }
  68. void reactive_descriptor_service::destroy(
  69. reactive_descriptor_service::implementation_type& impl)
  70. {
  71. if (is_open(impl))
  72. {
  73. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  74. "descriptor", &impl, impl.descriptor_, "close"));
  75. reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
  76. (impl.state_ & descriptor_ops::possible_dup) == 0);
  77. boost::system::error_code ignored_ec;
  78. descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
  79. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  80. }
  81. }
  82. boost::system::error_code reactive_descriptor_service::assign(
  83. reactive_descriptor_service::implementation_type& impl,
  84. const native_handle_type& native_descriptor, boost::system::error_code& ec)
  85. {
  86. if (is_open(impl))
  87. {
  88. ec = boost::asio::error::already_open;
  89. BOOST_ASIO_ERROR_LOCATION(ec);
  90. return ec;
  91. }
  92. if (int err = reactor_.register_descriptor(
  93. native_descriptor, impl.reactor_data_))
  94. {
  95. ec = boost::system::error_code(err,
  96. boost::asio::error::get_system_category());
  97. BOOST_ASIO_ERROR_LOCATION(ec);
  98. return ec;
  99. }
  100. impl.descriptor_ = native_descriptor;
  101. impl.state_ = descriptor_ops::possible_dup;
  102. ec = boost::system::error_code();
  103. return ec;
  104. }
  105. boost::system::error_code reactive_descriptor_service::close(
  106. reactive_descriptor_service::implementation_type& impl,
  107. boost::system::error_code& ec)
  108. {
  109. if (is_open(impl))
  110. {
  111. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  112. "descriptor", &impl, impl.descriptor_, "close"));
  113. reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
  114. (impl.state_ & descriptor_ops::possible_dup) == 0);
  115. descriptor_ops::close(impl.descriptor_, impl.state_, ec);
  116. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  117. }
  118. else
  119. {
  120. ec = boost::system::error_code();
  121. }
  122. // The descriptor is closed by the OS even if close() returns an error.
  123. //
  124. // (Actually, POSIX says the state of the descriptor is unspecified. On
  125. // Linux the descriptor is apparently closed anyway; e.g. see
  126. // http://lkml.org/lkml/2005/9/10/129
  127. // We'll just have to assume that other OSes follow the same behaviour.)
  128. construct(impl);
  129. BOOST_ASIO_ERROR_LOCATION(ec);
  130. return ec;
  131. }
  132. reactive_descriptor_service::native_handle_type
  133. reactive_descriptor_service::release(
  134. reactive_descriptor_service::implementation_type& impl)
  135. {
  136. native_handle_type descriptor = impl.descriptor_;
  137. if (is_open(impl))
  138. {
  139. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  140. "descriptor", &impl, impl.descriptor_, "release"));
  141. reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_, false);
  142. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  143. construct(impl);
  144. }
  145. return descriptor;
  146. }
  147. boost::system::error_code reactive_descriptor_service::cancel(
  148. reactive_descriptor_service::implementation_type& impl,
  149. boost::system::error_code& ec)
  150. {
  151. if (!is_open(impl))
  152. {
  153. ec = boost::asio::error::bad_descriptor;
  154. BOOST_ASIO_ERROR_LOCATION(ec);
  155. return ec;
  156. }
  157. BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
  158. "descriptor", &impl, impl.descriptor_, "cancel"));
  159. reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
  160. ec = boost::system::error_code();
  161. return ec;
  162. }
  163. void reactive_descriptor_service::do_start_op(implementation_type& impl,
  164. int op_type, reactor_op* op, bool is_continuation,
  165. bool allow_speculative, bool noop, bool needs_non_blocking,
  166. void (*on_immediate)(operation* op, bool, const void*),
  167. const void* immediate_arg)
  168. {
  169. if (!noop)
  170. {
  171. if ((impl.state_ & descriptor_ops::non_blocking)
  172. || !needs_non_blocking
  173. || descriptor_ops::set_internal_non_blocking(
  174. impl.descriptor_, impl.state_, true, op->ec_))
  175. {
  176. reactor_.start_op(op_type, impl.descriptor_, impl.reactor_data_, op,
  177. is_continuation, allow_speculative, on_immediate, immediate_arg);
  178. return;
  179. }
  180. }
  181. on_immediate(op, is_continuation, immediate_arg);
  182. }
  183. } // namespace detail
  184. } // namespace asio
  185. } // namespace boost
  186. #include <boost/asio/detail/pop_options.hpp>
  187. #endif // !defined(BOOST_ASIO_WINDOWS)
  188. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  189. // && !defined(__CYGWIN__)
  190. // && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  191. #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP