5 // Copyright (c) 2003-2020 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/non_const_lvalue.hpp>
23 #include <boost/asio/detail/recycling_allocator.hpp>
24 #include <boost/asio/detail/service_registry.hpp>
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/detail/type_traits.hpp>
28 #include <boost/asio/detail/push_options.hpp>
33 #if !defined(GENERATING_DOCUMENTATION)
35 template <typename Service>
36 inline Service& use_service(io_context& ioc)
38 // Check that Service meets the necessary type requirements.
39 (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
40 (void)static_cast<const execution_context::id*>(&Service::id);
42 return ioc.service_registry_->template use_service<Service>(ioc);
46 inline detail::io_context_impl& use_service<detail::io_context_impl>(
52 #endif // !defined(GENERATING_DOCUMENTATION)
54 inline io_context::executor_type
55 io_context::get_executor() BOOST_ASIO_NOEXCEPT
57 return executor_type(*this);
60 #if defined(BOOST_ASIO_HAS_CHRONO)
62 template <typename Rep, typename Period>
63 std::size_t io_context::run_for(
64 const chrono::duration<Rep, Period>& rel_time)
66 return this->run_until(chrono::steady_clock::now() + rel_time);
69 template <typename Clock, typename Duration>
70 std::size_t io_context::run_until(
71 const chrono::time_point<Clock, Duration>& abs_time)
74 while (this->run_one_until(abs_time))
75 if (n != (std::numeric_limits<std::size_t>::max)())
80 template <typename Rep, typename Period>
81 std::size_t io_context::run_one_for(
82 const chrono::duration<Rep, Period>& rel_time)
84 return this->run_one_until(chrono::steady_clock::now() + rel_time);
87 template <typename Clock, typename Duration>
88 std::size_t io_context::run_one_until(
89 const chrono::time_point<Clock, Duration>& abs_time)
91 typename Clock::time_point now = Clock::now();
92 while (now < abs_time)
94 typename Clock::duration rel_time = abs_time - now;
95 if (rel_time > chrono::seconds(1))
96 rel_time = chrono::seconds(1);
98 boost::system::error_code ec;
99 std::size_t s = impl_.wait_one(
100 static_cast<long>(chrono::duration_cast<
101 chrono::microseconds>(rel_time).count()), ec);
102 boost::asio::detail::throw_error(ec);
104 if (s || impl_.stopped())
113 #endif // defined(BOOST_ASIO_HAS_CHRONO)
115 #if !defined(BOOST_ASIO_NO_DEPRECATED)
117 inline void io_context::reset()
122 struct io_context::initiate_dispatch
124 template <typename LegacyCompletionHandler>
125 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
126 io_context* self) const
128 // If you get an error on the following line it means that your handler does
129 // not meet the documented type requirements for a LegacyCompletionHandler.
130 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
131 LegacyCompletionHandler, handler) type_check;
133 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
134 if (self->impl_.can_dispatch())
136 detail::fenced_block b(detail::fenced_block::full);
137 boost_asio_handler_invoke_helpers::invoke(
138 handler2.value, handler2.value);
142 // Allocate and construct an operation to wrap the handler.
143 typedef detail::completion_handler<
144 typename decay<LegacyCompletionHandler>::type, executor_type> op;
145 typename op::ptr p = { detail::addressof(handler2.value),
146 op::ptr::allocate(handler2.value), 0 };
147 p.p = new (p.v) op(handler2.value, self->get_executor());
149 BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
150 "io_context", self, 0, "dispatch"));
152 self->impl_.do_dispatch(p.p);
158 template <typename LegacyCompletionHandler>
159 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
160 io_context::dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
162 return async_initiate<LegacyCompletionHandler, void ()>(
163 initiate_dispatch(), handler, this);
166 struct io_context::initiate_post
168 template <typename LegacyCompletionHandler>
169 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
170 io_context* self) const
172 // If you get an error on the following line it means that your handler does
173 // not meet the documented type requirements for a LegacyCompletionHandler.
174 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
175 LegacyCompletionHandler, handler) type_check;
177 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
179 bool is_continuation =
180 boost_asio_handler_cont_helpers::is_continuation(handler2.value);
182 // Allocate and construct an operation to wrap the handler.
183 typedef detail::completion_handler<
184 typename decay<LegacyCompletionHandler>::type, executor_type> op;
185 typename op::ptr p = { detail::addressof(handler2.value),
186 op::ptr::allocate(handler2.value), 0 };
187 p.p = new (p.v) op(handler2.value, self->get_executor());
189 BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
190 "io_context", self, 0, "post"));
192 self->impl_.post_immediate_completion(p.p, is_continuation);
197 template <typename LegacyCompletionHandler>
198 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
199 io_context::post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
201 return async_initiate<LegacyCompletionHandler, void ()>(
202 initiate_post(), handler, this);
205 template <typename Handler>
206 #if defined(GENERATING_DOCUMENTATION)
209 inline detail::wrapped_handler<io_context&, Handler>
211 io_context::wrap(Handler handler)
213 return detail::wrapped_handler<io_context&, Handler>(*this, handler);
216 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
218 template <typename Allocator, unsigned int Bits>
219 io_context::basic_executor_type<Allocator, Bits>&
220 io_context::basic_executor_type<Allocator, Bits>::operator=(
221 const basic_executor_type& other) BOOST_ASIO_NOEXCEPT
225 io_context* old_io_context = io_context_;
226 io_context_ = other.io_context_;
227 allocator_ = other.allocator_;
229 if (Bits & outstanding_work_tracked)
232 io_context_->impl_.work_started();
234 old_io_context->impl_.work_finished();
240 #if defined(BOOST_ASIO_HAS_MOVE)
241 template <typename Allocator, unsigned int Bits>
242 io_context::basic_executor_type<Allocator, Bits>&
243 io_context::basic_executor_type<Allocator, Bits>::operator=(
244 basic_executor_type&& other) BOOST_ASIO_NOEXCEPT
248 io_context_ = other.io_context_;
249 allocator_ = std::move(other.allocator_);
251 if (Bits & outstanding_work_tracked)
252 other.io_context_ = 0;
256 #endif // defined(BOOST_ASIO_HAS_MOVE)
258 template <typename Allocator, unsigned int Bits>
259 inline bool io_context::basic_executor_type<Allocator,
260 Bits>::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
262 return io_context_->impl_.can_dispatch();
265 template <typename Allocator, unsigned int Bits>
266 template <typename Function>
267 void io_context::basic_executor_type<Allocator, Bits>::execute(
268 BOOST_ASIO_MOVE_ARG(Function) f) const
270 typedef typename decay<Function>::type function_type;
272 // Invoke immediately if the blocking.possibly property is enabled and we are
273 // already inside the thread pool.
274 if ((bits_ & blocking_never) == 0 && io_context_->impl_.can_dispatch())
276 // Make a local, non-const copy of the function.
277 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
279 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
280 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
283 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
284 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
285 detail::fenced_block b(detail::fenced_block::full);
286 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
288 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
289 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
293 io_context_->impl_.capture_current_exception();
296 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
297 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
300 // Allocate and construct an operation to wrap the function.
301 typedef detail::executor_op<function_type, Allocator, detail::operation> op;
302 typename op::ptr p = { detail::addressof(allocator_),
303 op::ptr::allocate(allocator_), 0 };
304 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), allocator_);
306 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
307 "io_context", io_context_, 0, "execute"));
309 io_context_->impl_.post_immediate_completion(p.p,
310 (bits_ & relationship_continuation) != 0);
314 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
315 template <typename Allocator, unsigned int Bits>
316 inline io_context& io_context::basic_executor_type<
317 Allocator, Bits>::context() const BOOST_ASIO_NOEXCEPT
322 template <typename Allocator, unsigned int Bits>
323 inline void io_context::basic_executor_type<Allocator,
324 Bits>::on_work_started() const BOOST_ASIO_NOEXCEPT
326 io_context_->impl_.work_started();
329 template <typename Allocator, unsigned int Bits>
330 inline void io_context::basic_executor_type<Allocator,
331 Bits>::on_work_finished() const BOOST_ASIO_NOEXCEPT
333 io_context_->impl_.work_finished();
336 template <typename Allocator, unsigned int Bits>
337 template <typename Function, typename OtherAllocator>
338 void io_context::basic_executor_type<Allocator, Bits>::dispatch(
339 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
341 typedef typename decay<Function>::type function_type;
343 // Invoke immediately if we are already inside the thread pool.
344 if (io_context_->impl_.can_dispatch())
346 // Make a local, non-const copy of the function.
347 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
349 detail::fenced_block b(detail::fenced_block::full);
350 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
354 // Allocate and construct an operation to wrap the function.
355 typedef detail::executor_op<function_type,
356 OtherAllocator, detail::operation> op;
357 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
358 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
360 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
361 "io_context", io_context_, 0, "dispatch"));
363 io_context_->impl_.post_immediate_completion(p.p, false);
367 template <typename Allocator, unsigned int Bits>
368 template <typename Function, typename OtherAllocator>
369 void io_context::basic_executor_type<Allocator, Bits>::post(
370 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
372 typedef typename decay<Function>::type function_type;
374 // Allocate and construct an operation to wrap the function.
375 typedef detail::executor_op<function_type,
376 OtherAllocator, detail::operation> op;
377 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
378 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
380 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
381 "io_context", io_context_, 0, "post"));
383 io_context_->impl_.post_immediate_completion(p.p, false);
387 template <typename Allocator, unsigned int Bits>
388 template <typename Function, typename OtherAllocator>
389 void io_context::basic_executor_type<Allocator, Bits>::defer(
390 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
392 typedef typename decay<Function>::type function_type;
394 // Allocate and construct an operation to wrap the function.
395 typedef detail::executor_op<function_type,
396 OtherAllocator, detail::operation> op;
397 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
398 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
400 BOOST_ASIO_HANDLER_CREATION((*io_context_, *p.p,
401 "io_context", io_context_, 0, "defer"));
403 io_context_->impl_.post_immediate_completion(p.p, true);
406 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
408 #if !defined(BOOST_ASIO_NO_DEPRECATED)
409 inline io_context::work::work(boost::asio::io_context& io_context)
410 : io_context_impl_(io_context.impl_)
412 io_context_impl_.work_started();
415 inline io_context::work::work(const work& other)
416 : io_context_impl_(other.io_context_impl_)
418 io_context_impl_.work_started();
421 inline io_context::work::~work()
423 io_context_impl_.work_finished();
426 inline boost::asio::io_context& io_context::work::get_io_context()
428 return static_cast<boost::asio::io_context&>(io_context_impl_.context());
430 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
432 inline boost::asio::io_context& io_context::service::get_io_context()
434 return static_cast<boost::asio::io_context&>(context());
440 #include <boost/asio/detail/pop_options.hpp>
442 #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP