diff options
Diffstat (limited to '3rdparty/asio/include/asio/strand.hpp')
-rw-r--r-- | 3rdparty/asio/include/asio/strand.hpp | 321 |
1 files changed, 302 insertions, 19 deletions
diff --git a/3rdparty/asio/include/asio/strand.hpp b/3rdparty/asio/include/asio/strand.hpp index c70162ba387..52b2314df56 100644 --- a/3rdparty/asio/include/asio/strand.hpp +++ b/3rdparty/asio/include/asio/strand.hpp @@ -2,7 +2,7 @@ // strand.hpp // ~~~~~~~~~~ // -// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2021 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) @@ -18,6 +18,9 @@ #include "asio/detail/config.hpp" #include "asio/detail/strand_executor_service.hpp" #include "asio/detail/type_traits.hpp" +#include "asio/execution/blocking.hpp" +#include "asio/execution/executor.hpp" +#include "asio/is_executor.hpp" #include "asio/detail/push_options.hpp" @@ -38,16 +41,22 @@ public: */ strand() : executor_(), - impl_(use_service<detail::strand_executor_service>( - executor_.context()).create_implementation()) + impl_(strand::create_implementation(executor_)) { } /// Construct a strand for the specified executor. - explicit strand(const Executor& e) + template <typename Executor1> + explicit strand(const Executor1& e, + typename constraint< + conditional< + !is_same<Executor1, strand>::value, + is_convertible<Executor1, Executor>, + false_type + >::type::value + >::type = 0) : executor_(e), - impl_(use_service<detail::strand_executor_service>( - executor_.context()).create_implementation()) + impl_(strand::create_implementation(executor_)) { } @@ -108,7 +117,7 @@ public: */ template <class OtherExecutor> strand(strand<OtherExecutor>&& other) ASIO_NOEXCEPT - : executor_(ASIO_MOVE_CAST(OtherExecutor)(other)), + : executor_(ASIO_MOVE_CAST(OtherExecutor)(other.executor_)), impl_(ASIO_MOVE_CAST(implementation_type)(other.impl_)) { } @@ -116,7 +125,7 @@ public: /// Move assignment operator. strand& operator=(strand&& other) ASIO_NOEXCEPT { - executor_ = ASIO_MOVE_CAST(Executor)(other); + executor_ = ASIO_MOVE_CAST(Executor)(other.executor_); impl_ = ASIO_MOVE_CAST(implementation_type)(other.impl_); return *this; } @@ -127,17 +136,16 @@ public: * convertible to @c Executor. */ template <class OtherExecutor> - strand& operator=( - const strand<OtherExecutor>&& other) ASIO_NOEXCEPT + strand& operator=(strand<OtherExecutor>&& other) ASIO_NOEXCEPT { - executor_ = ASIO_MOVE_CAST(OtherExecutor)(other); + executor_ = ASIO_MOVE_CAST(OtherExecutor)(other.executor_); impl_ = ASIO_MOVE_CAST(implementation_type)(other.impl_); return *this; } #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Destructor. - ~strand() + ~strand() ASIO_NOEXCEPT { } @@ -147,8 +155,88 @@ public: return executor_; } + /// Forward a query to the underlying executor. + /** + * Do not call this function directly. It is intended for use with the + * asio::query customisation point. + * + * For example: + * @code asio::strand<my_executor_type> ex = ...; + * if (asio::query(ex, asio::execution::blocking) + * == asio::execution::blocking.never) + * ... @endcode + */ + template <typename Property> + typename constraint< + can_query<const Executor&, Property>::value, + typename conditional< + is_convertible<Property, execution::blocking_t>::value, + execution::blocking_t, + typename query_result<const Executor&, Property>::type + >::type + >::type query(const Property& p) const + ASIO_NOEXCEPT_IF(( + is_nothrow_query<const Executor&, Property>::value)) + { + return this->query_helper( + is_convertible<Property, execution::blocking_t>(), p); + } + + /// Forward a requirement to the underlying executor. + /** + * Do not call this function directly. It is intended for use with the + * asio::require customisation point. + * + * For example: + * @code asio::strand<my_executor_type> ex1 = ...; + * auto ex2 = asio::require(ex1, + * asio::execution::blocking.never); @endcode + */ + template <typename Property> + typename constraint< + can_require<const Executor&, Property>::value + && !is_convertible<Property, execution::blocking_t::always_t>::value, + strand<typename decay< + typename require_result<const Executor&, Property>::type + >::type> + >::type require(const Property& p) const + ASIO_NOEXCEPT_IF(( + is_nothrow_require<const Executor&, Property>::value)) + { + return strand<typename decay< + typename require_result<const Executor&, Property>::type + >::type>(asio::require(executor_, p), impl_); + } + + /// Forward a preference to the underlying executor. + /** + * Do not call this function directly. It is intended for use with the + * asio::prefer customisation point. + * + * For example: + * @code asio::strand<my_executor_type> ex1 = ...; + * auto ex2 = asio::prefer(ex1, + * asio::execution::blocking.never); @endcode + */ + template <typename Property> + typename constraint< + can_prefer<const Executor&, Property>::value + && !is_convertible<Property, execution::blocking_t::always_t>::value, + strand<typename decay< + typename prefer_result<const Executor&, Property>::type + >::type> + >::type prefer(const Property& p) const + ASIO_NOEXCEPT_IF(( + is_nothrow_prefer<const Executor&, Property>::value)) + { + return strand<typename decay< + typename prefer_result<const Executor&, Property>::type + >::type>(asio::prefer(executor_, p), impl_); + } + +#if !defined(ASIO_NO_TS_EXECUTORS) /// Obtain the underlying execution context. - execution_context& context() ASIO_NOEXCEPT + execution_context& context() const ASIO_NOEXCEPT { return executor_.context(); } @@ -157,7 +245,7 @@ public: /** * The strand delegates this call to its underlying executor. */ - void on_work_started() ASIO_NOEXCEPT + void on_work_started() const ASIO_NOEXCEPT { executor_.on_work_started(); } @@ -166,11 +254,40 @@ public: /** * The strand delegates this call to its underlying executor. */ - void on_work_finished() ASIO_NOEXCEPT + void on_work_finished() const ASIO_NOEXCEPT { executor_.on_work_finished(); } +#endif // !defined(ASIO_NO_TS_EXECUTORS) + + /// Request the strand to invoke the given function object. + /** + * Do not call this function directly. It is intended for use with the + * execution::execute customisation point. + * + * For example: + * @code asio::strand<my_executor_type> ex = ...; + * execution::execute(ex, my_function_object); @endcode + * + * This function is used to ask the strand to execute the given function + * object on its underlying executor. The function object will be executed + * according to the properties of the underlying executor. + * + * @param f The function object to be called. The executor will make + * a copy of the handler object as required. The function signature of the + * function object must be: @code void function(); @endcode + */ + template <typename Function> + typename constraint< + execution::can_execute<const Executor&, Function>::value, + void + >::type execute(ASIO_MOVE_ARG(Function) f) const + { + detail::strand_executor_service::execute(impl_, + executor_, ASIO_MOVE_CAST(Function)(f)); + } +#if !defined(ASIO_NO_TS_EXECUTORS) /// Request the strand to invoke the given function object. /** * This function is used to ask the strand to execute the given function @@ -187,7 +304,7 @@ public: * internal storage needed for function invocation. */ template <typename Function, typename Allocator> - void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator& a) + void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator& a) const { detail::strand_executor_service::dispatch(impl_, executor_, ASIO_MOVE_CAST(Function)(f), a); @@ -207,7 +324,7 @@ public: * internal storage needed for function invocation. */ template <typename Function, typename Allocator> - void post(ASIO_MOVE_ARG(Function) f, const Allocator& a) + void post(ASIO_MOVE_ARG(Function) f, const Allocator& a) const { detail::strand_executor_service::post(impl_, executor_, ASIO_MOVE_CAST(Function)(f), a); @@ -227,11 +344,12 @@ public: * internal storage needed for function invocation. */ template <typename Function, typename Allocator> - void defer(ASIO_MOVE_ARG(Function) f, const Allocator& a) + void defer(ASIO_MOVE_ARG(Function) f, const Allocator& a) const { detail::strand_executor_service::defer(impl_, executor_, ASIO_MOVE_CAST(Function)(f), a); } +#endif // !defined(ASIO_NO_TS_EXECUTORS) /// Determine whether the strand is running in the current thread. /** @@ -264,13 +382,178 @@ public: return a.impl_ != b.impl_; } +#if defined(GENERATING_DOCUMENTATION) private: - Executor executor_; +#endif // defined(GENERATING_DOCUMENTATION) typedef detail::strand_executor_service::implementation_type implementation_type; + + template <typename InnerExecutor> + static implementation_type create_implementation(const InnerExecutor& ex, + typename constraint< + can_query<InnerExecutor, execution::context_t>::value + >::type = 0) + { + return use_service<detail::strand_executor_service>( + asio::query(ex, execution::context)).create_implementation(); + } + + template <typename InnerExecutor> + static implementation_type create_implementation(const InnerExecutor& ex, + typename constraint< + !can_query<InnerExecutor, execution::context_t>::value + >::type = 0) + { + return use_service<detail::strand_executor_service>( + ex.context()).create_implementation(); + } + + strand(const Executor& ex, const implementation_type& impl) + : executor_(ex), + impl_(impl) + { + } + + template <typename Property> + typename query_result<const Executor&, Property>::type query_helper( + false_type, const Property& property) const + { + return asio::query(executor_, property); + } + + template <typename Property> + execution::blocking_t query_helper(true_type, const Property& property) const + { + execution::blocking_t result = asio::query(executor_, property); + return result == execution::blocking.always + ? execution::blocking.possibly : result; + } + + Executor executor_; implementation_type impl_; }; +/** @defgroup make_strand asio::make_strand + * + * @brief The asio::make_strand function creates a @ref strand object for + * an executor or execution context. + */ +/*@{*/ + +/// Create a @ref strand object for an executor. +template <typename Executor> +inline strand<Executor> make_strand(const Executor& ex, + typename constraint< + is_executor<Executor>::value || execution::is_executor<Executor>::value + >::type = 0) +{ + return strand<Executor>(ex); +} + +/// Create a @ref strand object for an execution context. +template <typename ExecutionContext> +inline strand<typename ExecutionContext::executor_type> +make_strand(ExecutionContext& ctx, + typename constraint< + is_convertible<ExecutionContext&, execution_context&>::value + >::type = 0) +{ + return strand<typename ExecutionContext::executor_type>(ctx.get_executor()); +} + +/*@}*/ + +#if !defined(GENERATING_DOCUMENTATION) + +namespace traits { + +#if !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT) + +template <typename Executor> +struct equality_comparable<strand<Executor> > +{ + ASIO_STATIC_CONSTEXPR(bool, is_valid = true); + ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true); +}; + +#endif // !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT) + +#if !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) + +template <typename Executor, typename Function> +struct execute_member<strand<Executor>, Function, + typename enable_if< + execution::can_execute<const Executor&, Function>::value + >::type> +{ + ASIO_STATIC_CONSTEXPR(bool, is_valid = true); + ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false); + typedef void result_type; +}; + +#endif // !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT) + +#if !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT) + +template <typename Executor, typename Property> +struct query_member<strand<Executor>, Property, + typename enable_if< + can_query<const Executor&, Property>::value + >::type> +{ + ASIO_STATIC_CONSTEXPR(bool, is_valid = true); + ASIO_STATIC_CONSTEXPR(bool, is_noexcept = + (is_nothrow_query<Executor, Property>::value)); + typedef typename conditional< + is_convertible<Property, execution::blocking_t>::value, + execution::blocking_t, typename query_result<Executor, Property>::type + >::type result_type; +}; + +#endif // !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT) + +#if !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT) + +template <typename Executor, typename Property> +struct require_member<strand<Executor>, Property, + typename enable_if< + can_require<const Executor&, Property>::value + && !is_convertible<Property, execution::blocking_t::always_t>::value + >::type> +{ + ASIO_STATIC_CONSTEXPR(bool, is_valid = true); + ASIO_STATIC_CONSTEXPR(bool, is_noexcept = + (is_nothrow_require<Executor, Property>::value)); + typedef strand<typename decay< + typename require_result<Executor, Property>::type + >::type> result_type; +}; + +#endif // !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT) + +#if !defined(ASIO_HAS_DEDUCED_PREFER_MEMBER_TRAIT) + +template <typename Executor, typename Property> +struct prefer_member<strand<Executor>, Property, + typename enable_if< + can_prefer<const Executor&, Property>::value + && !is_convertible<Property, execution::blocking_t::always_t>::value + >::type> +{ + ASIO_STATIC_CONSTEXPR(bool, is_valid = true); + ASIO_STATIC_CONSTEXPR(bool, is_noexcept = + (is_nothrow_prefer<Executor, Property>::value)); + typedef strand<typename decay< + typename prefer_result<Executor, Property>::type + >::type> result_type; +}; + +#endif // !defined(ASIO_HAS_DEDUCED_PREFER_MEMBER_TRAIT) + +} // namespace traits + +#endif // !defined(GENERATING_DOCUMENTATION) + } // namespace asio #include "asio/detail/pop_options.hpp" |