2 // detail/impl/reactive_descriptor_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_WINDOWS) \
21 && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
22 && !defined(__CYGWIN__) \
23 && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/detail/reactive_descriptor_service.hpp>
28 #include <boost/asio/detail/push_options.hpp>
34 reactive_descriptor_service::reactive_descriptor_service(
35 execution_context& context)
36 : execution_context_service_base<reactive_descriptor_service>(context),
37 reactor_(boost::asio::use_service<reactor>(context))
42 void reactive_descriptor_service::shutdown()
46 void reactive_descriptor_service::construct(
47 reactive_descriptor_service::implementation_type& impl)
49 impl.descriptor_ = -1;
53 void reactive_descriptor_service::move_construct(
54 reactive_descriptor_service::implementation_type& impl,
55 reactive_descriptor_service::implementation_type& other_impl)
58 impl.descriptor_ = other_impl.descriptor_;
59 other_impl.descriptor_ = -1;
61 impl.state_ = other_impl.state_;
62 other_impl.state_ = 0;
64 reactor_.move_descriptor(impl.descriptor_,
65 impl.reactor_data_, other_impl.reactor_data_);
68 void reactive_descriptor_service::move_assign(
69 reactive_descriptor_service::implementation_type& impl,
70 reactive_descriptor_service& other_service,
71 reactive_descriptor_service::implementation_type& other_impl)
75 impl.descriptor_ = other_impl.descriptor_;
76 other_impl.descriptor_ = -1;
78 impl.state_ = other_impl.state_;
79 other_impl.state_ = 0;
81 other_service.reactor_.move_descriptor(impl.descriptor_,
82 impl.reactor_data_, other_impl.reactor_data_);
85 void reactive_descriptor_service::destroy(
86 reactive_descriptor_service::implementation_type& impl)
90 BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
91 "descriptor", &impl, impl.descriptor_, "close"));
93 reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
94 (impl.state_ & descriptor_ops::possible_dup) == 0);
96 boost::system::error_code ignored_ec;
97 descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
99 reactor_.cleanup_descriptor_data(impl.reactor_data_);
103 boost::system::error_code reactive_descriptor_service::assign(
104 reactive_descriptor_service::implementation_type& impl,
105 const native_handle_type& native_descriptor, boost::system::error_code& ec)
109 ec = boost::asio::error::already_open;
113 if (int err = reactor_.register_descriptor(
114 native_descriptor, impl.reactor_data_))
116 ec = boost::system::error_code(err,
117 boost::asio::error::get_system_category());
121 impl.descriptor_ = native_descriptor;
122 impl.state_ = descriptor_ops::possible_dup;
123 ec = boost::system::error_code();
127 boost::system::error_code reactive_descriptor_service::close(
128 reactive_descriptor_service::implementation_type& impl,
129 boost::system::error_code& ec)
133 BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
134 "descriptor", &impl, impl.descriptor_, "close"));
136 reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
137 (impl.state_ & descriptor_ops::possible_dup) == 0);
139 descriptor_ops::close(impl.descriptor_, impl.state_, ec);
141 reactor_.cleanup_descriptor_data(impl.reactor_data_);
145 ec = boost::system::error_code();
148 // The descriptor is closed by the OS even if close() returns an error.
150 // (Actually, POSIX says the state of the descriptor is unspecified. On
151 // Linux the descriptor is apparently closed anyway; e.g. see
152 // http://lkml.org/lkml/2005/9/10/129
153 // We'll just have to assume that other OSes follow the same behaviour.)
159 reactive_descriptor_service::native_handle_type
160 reactive_descriptor_service::release(
161 reactive_descriptor_service::implementation_type& impl)
163 native_handle_type descriptor = impl.descriptor_;
167 BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
168 "descriptor", &impl, impl.descriptor_, "release"));
170 reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_, false);
171 reactor_.cleanup_descriptor_data(impl.reactor_data_);
178 boost::system::error_code reactive_descriptor_service::cancel(
179 reactive_descriptor_service::implementation_type& impl,
180 boost::system::error_code& ec)
184 ec = boost::asio::error::bad_descriptor;
188 BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
189 "descriptor", &impl, impl.descriptor_, "cancel"));
191 reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
192 ec = boost::system::error_code();
196 void reactive_descriptor_service::start_op(
197 reactive_descriptor_service::implementation_type& impl,
198 int op_type, reactor_op* op, bool is_continuation,
199 bool is_non_blocking, bool noop)
203 if ((impl.state_ & descriptor_ops::non_blocking) ||
204 descriptor_ops::set_internal_non_blocking(
205 impl.descriptor_, impl.state_, true, op->ec_))
207 reactor_.start_op(op_type, impl.descriptor_,
208 impl.reactor_data_, op, is_continuation, is_non_blocking);
213 reactor_.post_immediate_completion(op, is_continuation);
216 } // namespace detail
220 #include <boost/asio/detail/pop_options.hpp>
222 #endif // !defined(BOOST_ASIO_WINDOWS)
223 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
224 // && !defined(__CYGWIN__)
225 // && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
227 #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP