123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- //
- // detail/handler_work.hpp
- // ~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef ASIO_DETAIL_HANDLER_WORK_HPP
- #define ASIO_DETAIL_HANDLER_WORK_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include "asio/detail/config.hpp"
- #include "asio/associated_allocator.hpp"
- #include "asio/associated_executor.hpp"
- #include "asio/associated_immediate_executor.hpp"
- #include "asio/detail/initiate_dispatch.hpp"
- #include "asio/detail/type_traits.hpp"
- #include "asio/detail/work_dispatcher.hpp"
- #include "asio/execution/allocator.hpp"
- #include "asio/execution/blocking.hpp"
- #include "asio/execution/executor.hpp"
- #include "asio/execution/outstanding_work.hpp"
- #include "asio/executor_work_guard.hpp"
- #include "asio/prefer.hpp"
- #include "asio/detail/push_options.hpp"
- namespace asio {
- class executor;
- class io_context;
- #if !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
- class any_completion_executor;
- class any_io_executor;
- #endif // !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
- namespace execution {
- template <typename...> class any_executor;
- } // namespace execution
- namespace detail {
- template <typename Executor, typename CandidateExecutor = void,
- typename IoContext = io_context,
- typename PolymorphicExecutor = executor, typename = void>
- class handler_work_base
- {
- public:
- explicit handler_work_base(int, int, const Executor& ex) noexcept
- : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
- {
- }
- template <typename OtherExecutor>
- handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
- const OtherExecutor& /*candidate*/) noexcept
- : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
- {
- }
- handler_work_base(const handler_work_base& other) noexcept
- : executor_(other.executor_)
- {
- }
- handler_work_base(handler_work_base&& other) noexcept
- : executor_(static_cast<executor_type&&>(other.executor_))
- {
- }
- bool owns_work() const noexcept
- {
- return true;
- }
- template <typename Function, typename Handler>
- void dispatch(Function& function, Handler& handler)
- {
- asio::prefer(executor_,
- execution::allocator((get_associated_allocator)(handler))
- ).execute(static_cast<Function&&>(function));
- }
- private:
- typedef decay_t<
- prefer_result_t<Executor, execution::outstanding_work_t::tracked_t>
- > executor_type;
- executor_type executor_;
- };
- template <typename Executor, typename CandidateExecutor,
- typename IoContext, typename PolymorphicExecutor>
- class handler_work_base<Executor, CandidateExecutor,
- IoContext, PolymorphicExecutor,
- enable_if_t<
- !execution::is_executor<Executor>::value
- && (!is_same<Executor, PolymorphicExecutor>::value
- || !is_same<CandidateExecutor, void>::value)
- >
- >
- {
- public:
- explicit handler_work_base(int, int, const Executor& ex) noexcept
- : executor_(ex),
- owns_work_(true)
- {
- executor_.on_work_started();
- }
- handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
- const Executor& candidate) noexcept
- : executor_(ex),
- owns_work_(ex != candidate)
- {
- if (owns_work_)
- executor_.on_work_started();
- }
- template <typename OtherExecutor>
- handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
- const OtherExecutor& /*candidate*/) noexcept
- : executor_(ex),
- owns_work_(true)
- {
- executor_.on_work_started();
- }
- handler_work_base(const handler_work_base& other) noexcept
- : executor_(other.executor_),
- owns_work_(other.owns_work_)
- {
- if (owns_work_)
- executor_.on_work_started();
- }
- handler_work_base(handler_work_base&& other) noexcept
- : executor_(static_cast<Executor&&>(other.executor_)),
- owns_work_(other.owns_work_)
- {
- other.owns_work_ = false;
- }
- ~handler_work_base()
- {
- if (owns_work_)
- executor_.on_work_finished();
- }
- bool owns_work() const noexcept
- {
- return owns_work_;
- }
- template <typename Function, typename Handler>
- void dispatch(Function& function, Handler& handler)
- {
- executor_.dispatch(static_cast<Function&&>(function),
- asio::get_associated_allocator(handler));
- }
- private:
- Executor executor_;
- bool owns_work_;
- };
- template <typename Executor, typename IoContext, typename PolymorphicExecutor>
- class handler_work_base<Executor, void, IoContext, PolymorphicExecutor,
- enable_if_t<
- is_same<
- Executor,
- typename IoContext::executor_type
- >::value
- >
- >
- {
- public:
- explicit handler_work_base(int, int, const Executor&)
- {
- }
- bool owns_work() const noexcept
- {
- return false;
- }
- template <typename Function, typename Handler>
- void dispatch(Function& function, Handler&)
- {
- // When using a native implementation, I/O completion handlers are
- // already dispatched according to the execution context's executor's
- // rules. We can call the function directly.
- static_cast<Function&&>(function)();
- }
- };
- template <typename Executor, typename IoContext>
- class handler_work_base<Executor, void, IoContext, Executor>
- {
- public:
- explicit handler_work_base(int, int, const Executor& ex) noexcept
- #if !defined(ASIO_NO_TYPEID)
- : executor_(
- ex.target_type() == typeid(typename IoContext::executor_type)
- ? Executor() : ex)
- #else // !defined(ASIO_NO_TYPEID)
- : executor_(ex)
- #endif // !defined(ASIO_NO_TYPEID)
- {
- if (executor_)
- executor_.on_work_started();
- }
- handler_work_base(bool /*base1_owns_work*/, const Executor& ex,
- const Executor& candidate) noexcept
- : executor_(ex != candidate ? ex : Executor())
- {
- if (executor_)
- executor_.on_work_started();
- }
- template <typename OtherExecutor>
- handler_work_base(const Executor& ex,
- const OtherExecutor&) noexcept
- : executor_(ex)
- {
- executor_.on_work_started();
- }
- handler_work_base(const handler_work_base& other) noexcept
- : executor_(other.executor_)
- {
- if (executor_)
- executor_.on_work_started();
- }
- handler_work_base(handler_work_base&& other) noexcept
- : executor_(static_cast<Executor&&>(other.executor_))
- {
- }
- ~handler_work_base()
- {
- if (executor_)
- executor_.on_work_finished();
- }
- bool owns_work() const noexcept
- {
- return !!executor_;
- }
- template <typename Function, typename Handler>
- void dispatch(Function& function, Handler& handler)
- {
- executor_.dispatch(static_cast<Function&&>(function),
- asio::get_associated_allocator(handler));
- }
- private:
- Executor executor_;
- };
- template <typename... SupportableProperties, typename CandidateExecutor,
- typename IoContext, typename PolymorphicExecutor>
- class handler_work_base<execution::any_executor<SupportableProperties...>,
- CandidateExecutor, IoContext, PolymorphicExecutor>
- {
- public:
- typedef execution::any_executor<SupportableProperties...> executor_type;
- explicit handler_work_base(int, int, const executor_type& ex) noexcept
- #if !defined(ASIO_NO_TYPEID)
- : executor_(
- ex.target_type() == typeid(typename IoContext::executor_type)
- ? executor_type()
- : asio::prefer(ex, execution::outstanding_work.tracked))
- #else // !defined(ASIO_NO_TYPEID)
- : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
- #endif // !defined(ASIO_NO_TYPEID)
- {
- }
- handler_work_base(bool base1_owns_work, const executor_type& ex,
- const executor_type& candidate) noexcept
- : executor_(
- !base1_owns_work && ex == candidate
- ? executor_type()
- : asio::prefer(ex, execution::outstanding_work.tracked))
- {
- }
- template <typename OtherExecutor>
- handler_work_base(bool /*base1_owns_work*/, const executor_type& ex,
- const OtherExecutor& /*candidate*/) noexcept
- : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
- {
- }
- handler_work_base(const handler_work_base& other) noexcept
- : executor_(other.executor_)
- {
- }
- handler_work_base(handler_work_base&& other) noexcept
- : executor_(static_cast<executor_type&&>(other.executor_))
- {
- }
- bool owns_work() const noexcept
- {
- return !!executor_;
- }
- template <typename Function, typename Handler>
- void dispatch(Function& function, Handler&)
- {
- executor_.execute(static_cast<Function&&>(function));
- }
- private:
- executor_type executor_;
- };
- #if !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
- template <typename Executor, typename CandidateExecutor,
- typename IoContext, typename PolymorphicExecutor>
- class handler_work_base<
- Executor, CandidateExecutor,
- IoContext, PolymorphicExecutor,
- enable_if_t<
- is_same<Executor, any_completion_executor>::value
- || is_same<Executor, any_io_executor>::value
- >
- >
- {
- public:
- typedef Executor executor_type;
- explicit handler_work_base(int, int,
- const executor_type& ex) noexcept
- #if !defined(ASIO_NO_TYPEID)
- : executor_(
- ex.target_type() == typeid(typename IoContext::executor_type)
- ? executor_type()
- : asio::prefer(ex, execution::outstanding_work.tracked))
- #else // !defined(ASIO_NO_TYPEID)
- : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
- #endif // !defined(ASIO_NO_TYPEID)
- {
- }
- handler_work_base(bool base1_owns_work, const executor_type& ex,
- const executor_type& candidate) noexcept
- : executor_(
- !base1_owns_work && ex == candidate
- ? executor_type()
- : asio::prefer(ex, execution::outstanding_work.tracked))
- {
- }
- template <typename OtherExecutor>
- handler_work_base(bool /*base1_owns_work*/, const executor_type& ex,
- const OtherExecutor& /*candidate*/) noexcept
- : executor_(asio::prefer(ex, execution::outstanding_work.tracked))
- {
- }
- handler_work_base(const handler_work_base& other) noexcept
- : executor_(other.executor_)
- {
- }
- handler_work_base(handler_work_base&& other) noexcept
- : executor_(static_cast<executor_type&&>(other.executor_))
- {
- }
- bool owns_work() const noexcept
- {
- return !!executor_;
- }
- template <typename Function, typename Handler>
- void dispatch(Function& function, Handler&)
- {
- executor_.execute(static_cast<Function&&>(function));
- }
- private:
- executor_type executor_;
- };
- #endif // !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
- template <typename Handler, typename IoExecutor, typename = void>
- class handler_work :
- handler_work_base<IoExecutor>,
- handler_work_base<associated_executor_t<Handler, IoExecutor>, IoExecutor>
- {
- public:
- typedef handler_work_base<IoExecutor> base1_type;
- typedef handler_work_base<associated_executor_t<Handler, IoExecutor>,
- IoExecutor> base2_type;
- handler_work(Handler& handler, const IoExecutor& io_ex) noexcept
- : base1_type(0, 0, io_ex),
- base2_type(base1_type::owns_work(),
- asio::get_associated_executor(handler, io_ex), io_ex)
- {
- }
- template <typename Function>
- void complete(Function& function, Handler& handler)
- {
- if (!base1_type::owns_work() && !base2_type::owns_work())
- {
- // When using a native implementation, I/O completion handlers are
- // already dispatched according to the execution context's executor's
- // rules. We can call the function directly.
- static_cast<Function&&>(function)();
- }
- else
- {
- base2_type::dispatch(function, handler);
- }
- }
- };
- template <typename Handler, typename IoExecutor>
- class handler_work<
- Handler, IoExecutor,
- enable_if_t<
- is_same<
- typename associated_executor<Handler,
- IoExecutor>::asio_associated_executor_is_unspecialised,
- void
- >::value
- >
- > : handler_work_base<IoExecutor>
- {
- public:
- typedef handler_work_base<IoExecutor> base1_type;
- handler_work(Handler&, const IoExecutor& io_ex) noexcept
- : base1_type(0, 0, io_ex)
- {
- }
- template <typename Function>
- void complete(Function& function, Handler& handler)
- {
- if (!base1_type::owns_work())
- {
- // When using a native implementation, I/O completion handlers are
- // already dispatched according to the execution context's executor's
- // rules. We can call the function directly.
- static_cast<Function&&>(function)();
- }
- else
- {
- base1_type::dispatch(function, handler);
- }
- }
- };
- template <typename Handler, typename IoExecutor>
- class immediate_handler_work
- {
- public:
- typedef handler_work<Handler, IoExecutor> handler_work_type;
- explicit immediate_handler_work(handler_work_type&& w)
- : handler_work_(static_cast<handler_work_type&&>(w))
- {
- }
- template <typename Function>
- void complete(Function& function, Handler& handler, const void* io_ex)
- {
- typedef associated_immediate_executor_t<Handler, IoExecutor>
- immediate_ex_type;
- immediate_ex_type immediate_ex = (get_associated_immediate_executor)(
- handler, *static_cast<const IoExecutor*>(io_ex));
- (initiate_dispatch_with_executor<immediate_ex_type>(immediate_ex))(
- static_cast<Function&&>(function));
- }
- private:
- handler_work_type handler_work_;
- };
- } // namespace detail
- } // namespace asio
- #include "asio/detail/pop_options.hpp"
- #endif // ASIO_DETAIL_HANDLER_WORK_HPP
|