123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- #ifndef ASIO_EXECUTOR_WORK_GUARD_HPP
- #define ASIO_EXECUTOR_WORK_GUARD_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include "asio/detail/config.hpp"
- #include "asio/associated_executor.hpp"
- #include "asio/detail/type_traits.hpp"
- #include "asio/execution.hpp"
- #include "asio/is_executor.hpp"
- #include "asio/detail/push_options.hpp"
- namespace asio {
- #if !defined(ASIO_EXECUTOR_WORK_GUARD_DECL)
- #define ASIO_EXECUTOR_WORK_GUARD_DECL
- template <typename Executor, typename = void, typename = void>
- class executor_work_guard;
- #endif
- #if defined(GENERATING_DOCUMENTATION)
- template <typename Executor>
- class executor_work_guard
- {
- public:
-
- typedef Executor executor_type;
-
-
- explicit executor_work_guard(const executor_type& e) noexcept;
-
- executor_work_guard(const executor_work_guard& other) noexcept;
-
- executor_work_guard(executor_work_guard&& other) noexcept;
-
-
- ~executor_work_guard();
-
- executor_type get_executor() const noexcept;
-
- bool owns_work() const noexcept;
-
-
- void reset() noexcept;
- };
- #endif
- #if !defined(GENERATING_DOCUMENTATION)
- #if !defined(ASIO_NO_TS_EXECUTORS)
- template <typename Executor>
- class executor_work_guard<Executor,
- enable_if_t<
- is_executor<Executor>::value
- >>
- {
- public:
- typedef Executor executor_type;
- explicit executor_work_guard(const executor_type& e) noexcept
- : executor_(e),
- owns_(true)
- {
- executor_.on_work_started();
- }
- executor_work_guard(const executor_work_guard& other) noexcept
- : executor_(other.executor_),
- owns_(other.owns_)
- {
- if (owns_)
- executor_.on_work_started();
- }
- executor_work_guard(executor_work_guard&& other) noexcept
- : executor_(static_cast<Executor&&>(other.executor_)),
- owns_(other.owns_)
- {
- other.owns_ = false;
- }
- ~executor_work_guard()
- {
- if (owns_)
- executor_.on_work_finished();
- }
- executor_type get_executor() const noexcept
- {
- return executor_;
- }
- bool owns_work() const noexcept
- {
- return owns_;
- }
- void reset() noexcept
- {
- if (owns_)
- {
- executor_.on_work_finished();
- owns_ = false;
- }
- }
- private:
-
- executor_work_guard& operator=(const executor_work_guard&);
- executor_type executor_;
- bool owns_;
- };
- #endif
- template <typename Executor>
- class executor_work_guard<Executor,
- enable_if_t<
- !is_executor<Executor>::value
- >,
- enable_if_t<
- execution::is_executor<Executor>::value
- >>
- {
- public:
- typedef Executor executor_type;
- explicit executor_work_guard(const executor_type& e) noexcept
- : executor_(e),
- owns_(true)
- {
- new (&work_) work_type(asio::prefer(executor_,
- execution::outstanding_work.tracked));
- }
- executor_work_guard(const executor_work_guard& other) noexcept
- : executor_(other.executor_),
- owns_(other.owns_)
- {
- if (owns_)
- {
- new (&work_) work_type(asio::prefer(executor_,
- execution::outstanding_work.tracked));
- }
- }
- executor_work_guard(executor_work_guard&& other) noexcept
- : executor_(static_cast<Executor&&>(other.executor_)),
- owns_(other.owns_)
- {
- if (owns_)
- {
- new (&work_) work_type(
- static_cast<work_type&&>(
- *static_cast<work_type*>(
- static_cast<void*>(&other.work_))));
- other.owns_ = false;
- }
- }
- ~executor_work_guard()
- {
- if (owns_)
- static_cast<work_type*>(static_cast<void*>(&work_))->~work_type();
- }
- executor_type get_executor() const noexcept
- {
- return executor_;
- }
- bool owns_work() const noexcept
- {
- return owns_;
- }
- void reset() noexcept
- {
- if (owns_)
- {
- static_cast<work_type*>(static_cast<void*>(&work_))->~work_type();
- owns_ = false;
- }
- }
- private:
-
- executor_work_guard& operator=(const executor_work_guard&);
- typedef decay_t<
- prefer_result_t<
- const executor_type&,
- execution::outstanding_work_t::tracked_t
- >
- > work_type;
- executor_type executor_;
- aligned_storage_t<sizeof(work_type), alignment_of<work_type>::value> work_;
- bool owns_;
- };
- #endif
- template <typename Executor>
- ASIO_NODISCARD inline executor_work_guard<Executor>
- make_work_guard(const Executor& ex,
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0)
- {
- return executor_work_guard<Executor>(ex);
- }
- template <typename ExecutionContext>
- ASIO_NODISCARD inline
- executor_work_guard<typename ExecutionContext::executor_type>
- make_work_guard(ExecutionContext& ctx,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- {
- return executor_work_guard<typename ExecutionContext::executor_type>(
- ctx.get_executor());
- }
- template <typename T>
- ASIO_NODISCARD inline
- executor_work_guard<
- typename constraint_t<
- !is_executor<T>::value
- && !execution::is_executor<T>::value
- && !is_convertible<T&, execution_context&>::value,
- associated_executor<T>
- >::type>
- make_work_guard(const T& t)
- {
- return executor_work_guard<associated_executor_t<T>>(
- associated_executor<T>::get(t));
- }
- template <typename T, typename Executor>
- ASIO_NODISCARD inline
- executor_work_guard<associated_executor_t<T, Executor>>
- make_work_guard(const T& t, const Executor& ex,
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0)
- {
- return executor_work_guard<associated_executor_t<T, Executor>>(
- associated_executor<T, Executor>::get(t, ex));
- }
- template <typename T, typename ExecutionContext>
- ASIO_NODISCARD inline executor_work_guard<
- associated_executor_t<T, typename ExecutionContext::executor_type>>
- make_work_guard(const T& t, ExecutionContext& ctx,
- constraint_t<
- !is_executor<T>::value
- > = 0,
- constraint_t<
- !execution::is_executor<T>::value
- > = 0,
- constraint_t<
- !is_convertible<T&, execution_context&>::value
- > = 0,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- {
- return executor_work_guard<
- associated_executor_t<T, typename ExecutionContext::executor_type>>(
- associated_executor<T, typename ExecutionContext::executor_type>::get(
- t, ctx.get_executor()));
- }
- }
- #include "asio/detail/pop_options.hpp"
- #endif
|