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_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/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 #if !defined(GENERATING_DOCUMENTATION)
34 template <typename Service>
35 inline Service& use_service(io_context& ioc)
37 // Check that Service meets the necessary type requirements.
38 (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
39 (void)static_cast<const execution_context::id*>(&Service::id);
41 return ioc.service_registry_->template use_service<Service>(ioc);
45 inline detail::io_context_impl& use_service<detail::io_context_impl>(
51 #endif // !defined(GENERATING_DOCUMENTATION)
53 inline io_context::executor_type
54 io_context::get_executor() BOOST_ASIO_NOEXCEPT
56 return executor_type(*this);
59 #if defined(BOOST_ASIO_HAS_CHRONO)
61 template <typename Rep, typename Period>
62 std::size_t io_context::run_for(
63 const chrono::duration<Rep, Period>& rel_time)
65 return this->run_until(chrono::steady_clock::now() + rel_time);
68 template <typename Clock, typename Duration>
69 std::size_t io_context::run_until(
70 const chrono::time_point<Clock, Duration>& abs_time)
73 while (this->run_one_until(abs_time))
74 if (n != (std::numeric_limits<std::size_t>::max)())
79 template <typename Rep, typename Period>
80 std::size_t io_context::run_one_for(
81 const chrono::duration<Rep, Period>& rel_time)
83 return this->run_one_until(chrono::steady_clock::now() + rel_time);
86 template <typename Clock, typename Duration>
87 std::size_t io_context::run_one_until(
88 const chrono::time_point<Clock, Duration>& abs_time)
90 typename Clock::time_point now = Clock::now();
91 while (now < abs_time)
93 typename Clock::duration rel_time = abs_time - now;
94 if (rel_time > chrono::seconds(1))
95 rel_time = chrono::seconds(1);
97 boost::system::error_code ec;
98 std::size_t s = impl_.wait_one(
99 static_cast<long>(chrono::duration_cast<
100 chrono::microseconds>(rel_time).count()), ec);
101 boost::asio::detail::throw_error(ec);
103 if (s || impl_.stopped())
112 #endif // defined(BOOST_ASIO_HAS_CHRONO)
114 #if !defined(BOOST_ASIO_NO_DEPRECATED)
116 inline void io_context::reset()
121 struct io_context::initiate_dispatch
123 template <typename LegacyCompletionHandler>
124 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
125 io_context* self) const
127 // If you get an error on the following line it means that your handler does
128 // not meet the documented type requirements for a LegacyCompletionHandler.
129 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
130 LegacyCompletionHandler, handler) type_check;
132 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
133 if (self->impl_.can_dispatch())
135 detail::fenced_block b(detail::fenced_block::full);
136 boost_asio_handler_invoke_helpers::invoke(
137 handler2.value, handler2.value);
141 // Allocate and construct an operation to wrap the handler.
142 typedef detail::completion_handler<
143 typename decay<LegacyCompletionHandler>::type, executor_type> op;
144 typename op::ptr p = { detail::addressof(handler2.value),
145 op::ptr::allocate(handler2.value), 0 };
146 p.p = new (p.v) op(handler2.value, self->get_executor());
148 BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
149 "io_context", self, 0, "dispatch"));
151 self->impl_.do_dispatch(p.p);
157 template <typename LegacyCompletionHandler>
158 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
159 io_context::dispatch(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
161 return async_initiate<LegacyCompletionHandler, void ()>(
162 initiate_dispatch(), handler, this);
165 struct io_context::initiate_post
167 template <typename LegacyCompletionHandler>
168 void operator()(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
169 io_context* self) const
171 // If you get an error on the following line it means that your handler does
172 // not meet the documented type requirements for a LegacyCompletionHandler.
173 BOOST_ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
174 LegacyCompletionHandler, handler) type_check;
176 detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
178 bool is_continuation =
179 boost_asio_handler_cont_helpers::is_continuation(handler2.value);
181 // Allocate and construct an operation to wrap the handler.
182 typedef detail::completion_handler<
183 typename decay<LegacyCompletionHandler>::type, executor_type> op;
184 typename op::ptr p = { detail::addressof(handler2.value),
185 op::ptr::allocate(handler2.value), 0 };
186 p.p = new (p.v) op(handler2.value, self->get_executor());
188 BOOST_ASIO_HANDLER_CREATION((*self, *p.p,
189 "io_context", self, 0, "post"));
191 self->impl_.post_immediate_completion(p.p, is_continuation);
196 template <typename LegacyCompletionHandler>
197 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(LegacyCompletionHandler, void ())
198 io_context::post(BOOST_ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
200 return async_initiate<LegacyCompletionHandler, void ()>(
201 initiate_post(), handler, this);
204 template <typename Handler>
205 #if defined(GENERATING_DOCUMENTATION)
208 inline detail::wrapped_handler<io_context&, Handler>
210 io_context::wrap(Handler handler)
212 return detail::wrapped_handler<io_context&, Handler>(*this, handler);
215 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
217 template <typename Allocator, uintptr_t Bits>
218 io_context::basic_executor_type<Allocator, Bits>&
219 io_context::basic_executor_type<Allocator, Bits>::operator=(
220 const basic_executor_type& other) BOOST_ASIO_NOEXCEPT
224 static_cast<Allocator&>(*this) = static_cast<const Allocator&>(other);
225 io_context* old_io_context = context_ptr();
226 target_ = other.target_;
227 if (Bits & outstanding_work_tracked)
230 context_ptr()->impl_.work_started();
232 old_io_context->impl_.work_finished();
238 #if defined(BOOST_ASIO_HAS_MOVE)
239 template <typename Allocator, uintptr_t Bits>
240 io_context::basic_executor_type<Allocator, Bits>&
241 io_context::basic_executor_type<Allocator, Bits>::operator=(
242 basic_executor_type&& other) BOOST_ASIO_NOEXCEPT
246 static_cast<Allocator&>(*this) = static_cast<Allocator&&>(other);
247 io_context* old_io_context = context_ptr();
248 target_ = other.target_;
249 if (Bits & outstanding_work_tracked)
253 old_io_context->impl_.work_finished();
258 #endif // defined(BOOST_ASIO_HAS_MOVE)
260 template <typename Allocator, uintptr_t Bits>
261 inline bool io_context::basic_executor_type<Allocator,
262 Bits>::running_in_this_thread() const BOOST_ASIO_NOEXCEPT
264 return context_ptr()->impl_.can_dispatch();
267 template <typename Allocator, uintptr_t Bits>
268 template <typename Function>
269 void io_context::basic_executor_type<Allocator, Bits>::execute(
270 BOOST_ASIO_MOVE_ARG(Function) f) const
272 typedef typename decay<Function>::type function_type;
274 // Invoke immediately if the blocking.possibly property is enabled and we are
275 // already inside the thread pool.
276 if ((bits() & blocking_never) == 0 && context_ptr()->impl_.can_dispatch())
278 // Make a local, non-const copy of the function.
279 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
281 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
282 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
285 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
286 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
287 detail::fenced_block b(detail::fenced_block::full);
288 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
290 #if defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR) \
291 && !defined(BOOST_ASIO_NO_EXCEPTIONS)
295 context_ptr()->impl_.capture_current_exception();
298 #endif // defined(BOOST_ASIO_HAS_STD_EXCEPTION_PTR)
299 // && !defined(BOOST_ASIO_NO_EXCEPTIONS)
302 // Allocate and construct an operation to wrap the function.
303 typedef detail::executor_op<function_type, Allocator, detail::operation> op;
304 typename op::ptr p = {
305 detail::addressof(static_cast<const Allocator&>(*this)),
306 op::ptr::allocate(static_cast<const Allocator&>(*this)), 0 };
307 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f),
308 static_cast<const Allocator&>(*this));
310 BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
311 "io_context", context_ptr(), 0, "execute"));
313 context_ptr()->impl_.post_immediate_completion(p.p,
314 (bits() & relationship_continuation) != 0);
318 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
319 template <typename Allocator, uintptr_t Bits>
320 inline io_context& io_context::basic_executor_type<
321 Allocator, Bits>::context() const BOOST_ASIO_NOEXCEPT
323 return *context_ptr();
326 template <typename Allocator, uintptr_t Bits>
327 inline void io_context::basic_executor_type<Allocator,
328 Bits>::on_work_started() const BOOST_ASIO_NOEXCEPT
330 context_ptr()->impl_.work_started();
333 template <typename Allocator, uintptr_t Bits>
334 inline void io_context::basic_executor_type<Allocator,
335 Bits>::on_work_finished() const BOOST_ASIO_NOEXCEPT
337 context_ptr()->impl_.work_finished();
340 template <typename Allocator, uintptr_t Bits>
341 template <typename Function, typename OtherAllocator>
342 void io_context::basic_executor_type<Allocator, Bits>::dispatch(
343 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
345 typedef typename decay<Function>::type function_type;
347 // Invoke immediately if we are already inside the thread pool.
348 if (context_ptr()->impl_.can_dispatch())
350 // Make a local, non-const copy of the function.
351 function_type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
353 detail::fenced_block b(detail::fenced_block::full);
354 boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
358 // Allocate and construct an operation to wrap the function.
359 typedef detail::executor_op<function_type,
360 OtherAllocator, detail::operation> op;
361 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
362 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
364 BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
365 "io_context", context_ptr(), 0, "dispatch"));
367 context_ptr()->impl_.post_immediate_completion(p.p, false);
371 template <typename Allocator, uintptr_t Bits>
372 template <typename Function, typename OtherAllocator>
373 void io_context::basic_executor_type<Allocator, Bits>::post(
374 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
376 typedef typename decay<Function>::type function_type;
378 // Allocate and construct an operation to wrap the function.
379 typedef detail::executor_op<function_type,
380 OtherAllocator, detail::operation> op;
381 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
382 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
384 BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
385 "io_context", context_ptr(), 0, "post"));
387 context_ptr()->impl_.post_immediate_completion(p.p, false);
391 template <typename Allocator, uintptr_t Bits>
392 template <typename Function, typename OtherAllocator>
393 void io_context::basic_executor_type<Allocator, Bits>::defer(
394 BOOST_ASIO_MOVE_ARG(Function) f, const OtherAllocator& a) const
396 typedef typename decay<Function>::type function_type;
398 // Allocate and construct an operation to wrap the function.
399 typedef detail::executor_op<function_type,
400 OtherAllocator, detail::operation> op;
401 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
402 p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
404 BOOST_ASIO_HANDLER_CREATION((*context_ptr(), *p.p,
405 "io_context", context_ptr(), 0, "defer"));
407 context_ptr()->impl_.post_immediate_completion(p.p, true);
410 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
412 #if !defined(BOOST_ASIO_NO_DEPRECATED)
413 inline io_context::work::work(boost::asio::io_context& io_context)
414 : io_context_impl_(io_context.impl_)
416 io_context_impl_.work_started();
419 inline io_context::work::work(const work& other)
420 : io_context_impl_(other.io_context_impl_)
422 io_context_impl_.work_started();
425 inline io_context::work::~work()
427 io_context_impl_.work_finished();
430 inline boost::asio::io_context& io_context::work::get_io_context()
432 return static_cast<boost::asio::io_context&>(io_context_impl_.context());
434 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
436 inline boost::asio::io_context& io_context::service::get_io_context()
438 return static_cast<boost::asio::io_context&>(context());
444 #include <boost/asio/detail/pop_options.hpp>
446 #endif // BOOST_ASIO_IMPL_IO_CONTEXT_HPP