5 // Copyright (c) 2003-2018 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_IMPL_IO_CONTEXT_HPP
12 #define BOOST_ASIO_IMPL_IO_CONTEXT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/completion_handler.hpp>
19 #include <boost/asio/detail/executor_op.hpp>
20 #include <boost/asio/detail/fenced_block.hpp>
21 #include <boost/asio/detail/handler_type_requirements.hpp>
22 #include <boost/asio/detail/recycling_allocator.hpp>
23 #include <boost/asio/detail/service_registry.hpp>
24 #include <boost/asio/detail/throw_error.hpp>
25 #include <boost/asio/detail/type_traits.hpp>
27 #include <boost/asio/detail/push_options.hpp>
32 template <typename Service>
33 inline Service& use_service(io_context& ioc)
35 // Check that Service meets the necessary type requirements.
36 (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
37 (void)static_cast<const execution_context::id*>(&Service::id);
39 return ioc.service_registry_->template use_service<Service>(ioc);
43 inline detail::io_context_impl& use_service<detail::io_context_impl>(
52 #include <boost/asio/detail/pop_options.hpp>
54 #if defined(BOOST_ASIO_HAS_IOCP)
55 # include <boost/asio/detail/win_iocp_io_context.hpp>
57 # include <boost/asio/detail/scheduler.hpp>
60 #include <boost/asio/detail/push_options.hpp>
65 inline io_context::executor_type
66 io_context::get_executor() BOOST_ASIO_NOEXCEPT
68 return executor_type(*this);
71 #if defined(BOOST_ASIO_HAS_CHRONO)
73 template <typename Rep, typename Period>
74 std::size_t io_context::run_for(
75 const chrono::duration<Rep, Period>& rel_time)
77 return this->run_until(chrono::steady_clock::now() + rel_time);
80 template <typename Clock, typename Duration>
81 std::size_t io_context::run_until(
82 const chrono::time_point<Clock, Duration>& abs_time)
85 while (this->run_one_until(abs_time))
86 if (n != (std::numeric_limits<std::size_t>::max)())
91 template <typename Rep, typename Period>
92 std::size_t io_context::run_one_for(
93 const chrono::duration<Rep, Period>& rel_time)
95 return this->run_one_until(chrono::steady_clock::now() + rel_time);
98 template <typename Clock, typename Duration>
99 std::size_t io_context::run_one_until(
100 const chrono::time_point<Clock, Duration>& abs_time)
102 typename Clock::time_point now = Clock::now();
103 while (now < abs_time)
105 typename Clock::duration rel_time = abs_time - now;
106 if (rel_time > chrono::seconds(1))
107 rel_time = chrono::seconds(1);
109 boost::system::error_code ec;
110 std::size_t s = impl_.wait_one(
111 static_cast<long>(chrono::duration_cast<
112 chrono::microseconds>(rel_time).count()), ec);
113 boost::asio::detail::throw_error(ec);
115 if (s || impl_.stopped())
124 #endif // defined(BOOST_ASIO_HAS_CHRONO)
126 #if !defined(BOOST_ASIO_NO_DEPRECATED)
128 inline void io_context::reset()
133 template <typename LegacyCompletionHandler>
134 BOOST_ASIO_INITFN_RESULT_TYPE(LegacyCompletionHandler, void ())
135 io_context::dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
137 // If you get an error on the following line it means that your handler does
138 // not meet the documented type requirements for a LegacyCompletionHandler.
139 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
140 LegacyCompletionHandler, handler) type_check;
142 async_completion<LegacyCompletionHandler, void ()> init(handler);
144 if (impl_.can_dispatch())
146 detail::fenced_block b(detail::fenced_block::full);
147 boost_asio_handler_invoke_helpers::invoke(
148 init.completion_handler, init.completion_handler);
152 // Allocate and construct an operation to wrap the handler.
153 typedef detail::completion_handler<
154 typename handler_type<LegacyCompletionHandler, void ()>::type> op;
155 typename op::ptr p = { detail::addressof(init.completion_handler),
156 op::ptr::allocate(init.completion_handler), 0 };
157 p.p = new (p.v) op(init.completion_handler);
159 BOOST_ASIO_HANDLER_CREATION((*this, *p.p,
160 "io_context", this, 0, "dispatch"));
162 impl_.do_dispatch(p.p);
166 return init.result.get();
169 template <typename LegacyCompletionHandler>
170 BOOST_ASIO_INITFN_RESULT_TYPE(LegacyCompletionHandler, void ())
171 io_context::post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
173 // If you get an error on the following line it means that your handler does
174 // not meet the documented type requirements for a LegacyCompletionHandler.
175 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
176 LegacyCompletionHandler, handler) type_check;
178 async_completion<LegacyCompletionHandler, void ()> init(handler);
180 bool is_continuation =
181 boost_asio_handler_cont_helpers::is_continuation(init.completion_handler);
183 // Allocate and construct an operation to wrap the handler.
184 typedef detail::completion_handler<
185 typename handler_type<LegacyCompletionHandler, void ()>::type> op;
186 typename op::ptr p = { detail::addressof(init.completion_handler),
187 op::ptr::allocate(init.completion_handler), 0 };
188 p.p = new (p.v) op(init.completion_handler);
190 BOOST_ASIO_HANDLER_CREATION((*this, *p.p,
191 "io_context", this, 0, "post"));
193 impl_.post_immediate_completion(p.p, is_continuation);
196 return init.result.get();
199 template <typename Handler>
200 #if defined(GENERATING_DOCUMENTATION)
203 inline detail::wrapped_handler<io_context&, Handler>
205 io_context::wrap(Handler handler)
207 return detail::wrapped_handler<io_context&, Handler>(*this, handler);
210 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
213 io_context::executor_type::context() const BOOST_ASIO_NOEXCEPT
219 io_context::executor_type::on_work_started() const BOOST_ASIO_NOEXCEPT
221 io_context_.impl_.work_started();
225 io_context::executor_type::on_work_finished() const BOOST_ASIO_NOEXCEPT
227 io_context_.impl_.work_finished();
230 template <typename Function, typename Allocator>
231 void io_context::executor_type::dispatch(
232 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
234 typedef typename decay<Function>::type function_type;
236 // Invoke immediately if we are already inside the thread pool.
237 if (io_context_.impl_.can_dispatch())
239 // Make a local, non-const copy of the function.
240 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
242 detail::fenced_block b(detail::fenced_block::full);
243 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
247 // Allocate and construct an operation to wrap the function.
248 typedef detail::executor_op<function_type, Allocator, detail::operation> op;
249 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
250 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
252 BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
253 "io_context", &this->context(), 0, "post"));
255 io_context_.impl_.post_immediate_completion(p.p, false);
259 template <typename Function, typename Allocator>
260 void io_context::executor_type::post(
261 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
263 typedef typename decay<Function>::type function_type;
265 // Allocate and construct an operation to wrap the function.
266 typedef detail::executor_op<function_type, Allocator, detail::operation> op;
267 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
268 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
270 BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
271 "io_context", &this->context(), 0, "post"));
273 io_context_.impl_.post_immediate_completion(p.p, false);
277 template <typename Function, typename Allocator>
278 void io_context::executor_type::defer(
279 BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
281 typedef typename decay<Function>::type function_type;
283 // Allocate and construct an operation to wrap the function.
284 typedef detail::executor_op<function_type, Allocator, detail::operation> op;
285 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
286 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
288 BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
289 "io_context", &this->context(), 0, "defer"));
291 io_context_.impl_.post_immediate_completion(p.p, true);
296 io_context::executor_type::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
298 return io_context_.impl_.can_dispatch();
301 #if !defined(BOOST_ASIO_NO_DEPRECATED)
302 inline io_context::work::work(boost::asio::io_context& io_context)
303 : io_context_impl_(io_context.impl_)
305 io_context_impl_.work_started();
308 inline io_context::work::work(const work& other)
309 : io_context_impl_(other.io_context_impl_)
311 io_context_impl_.work_started();
314 inline io_context::work::~work()
316 io_context_impl_.work_finished();
319 inline boost::asio::io_context& io_context::work::get_io_context()
321 return static_cast<boost::asio::io_context&>(io_context_impl_.context());
324 inline boost::asio::io_context& io_context::work::get_io_service()
326 return static_cast<boost::asio::io_context&>(io_context_impl_.context());
328 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
330 inline boost::asio::io_context& io_context::service::get_io_context()
332 return static_cast<boost::asio::io_context&>(context());
335 #if !defined(BOOST_ASIO_NO_DEPRECATED)
336 inline boost::asio::io_context& io_context::service::get_io_service()
338 return static_cast<boost::asio::io_context&>(context());
340 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
345 #include <boost/asio/detail/pop_options.hpp>
347 #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP