123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- #ifndef BOOST_ASIO_SPAWN_HPP
- #define BOOST_ASIO_SPAWN_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/config.hpp>
- #include <boost/asio/any_io_executor.hpp>
- #include <boost/asio/cancellation_signal.hpp>
- #include <boost/asio/cancellation_state.hpp>
- #include <boost/asio/detail/exception.hpp>
- #include <boost/asio/detail/memory.hpp>
- #include <boost/asio/detail/type_traits.hpp>
- #include <boost/asio/io_context.hpp>
- #include <boost/asio/is_executor.hpp>
- #include <boost/asio/strand.hpp>
- #if defined(BOOST_ASIO_HAS_BOOST_COROUTINE)
- # include <boost/coroutine/all.hpp>
- #endif
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace detail {
- class spawned_thread_base
- {
- public:
- spawned_thread_base()
- : owner_(0),
- has_context_switched_(false),
- throw_if_cancelled_(false),
- terminal_(false)
- {
- }
- virtual ~spawned_thread_base() {}
- virtual void resume() = 0;
- virtual void suspend_with(void (*fn)(void*), void* arg) = 0;
- virtual void destroy() = 0;
- void attach(spawned_thread_base** owner)
- {
- owner_ = owner;
- *owner_ = this;
- }
- void detach()
- {
- if (owner_)
- *owner_ = 0;
- owner_ = 0;
- }
- void suspend()
- {
- suspend_with(0, 0);
- }
- template <typename F>
- void suspend_with(F f)
- {
- suspend_with(&spawned_thread_base::call<F>, &f);
- }
- cancellation_slot get_cancellation_slot() const noexcept
- {
- return cancellation_state_.slot();
- }
- cancellation_state get_cancellation_state() const noexcept
- {
- return cancellation_state_;
- }
- void reset_cancellation_state()
- {
- cancellation_state_ = cancellation_state(parent_cancellation_slot_);
- }
- template <typename Filter>
- void reset_cancellation_state(Filter filter)
- {
- cancellation_state_ = cancellation_state(
- parent_cancellation_slot_, filter, filter);
- }
- template <typename InFilter, typename OutFilter>
- void reset_cancellation_state(InFilter in_filter, OutFilter out_filter)
- {
- cancellation_state_ = cancellation_state(
- parent_cancellation_slot_, in_filter, out_filter);
- }
- cancellation_type_t cancelled() const noexcept
- {
- return cancellation_state_.cancelled();
- }
- bool has_context_switched() const noexcept
- {
- return has_context_switched_;
- }
- bool throw_if_cancelled() const noexcept
- {
- return throw_if_cancelled_;
- }
- void throw_if_cancelled(bool value) noexcept
- {
- throw_if_cancelled_ = value;
- }
- protected:
- spawned_thread_base** owner_;
- boost::asio::cancellation_slot parent_cancellation_slot_;
- boost::asio::cancellation_state cancellation_state_;
- bool has_context_switched_;
- bool throw_if_cancelled_;
- bool terminal_;
- private:
-
- spawned_thread_base(const spawned_thread_base&) = delete;
- spawned_thread_base& operator=(const spawned_thread_base&) = delete;
- template <typename F>
- static void call(void* f)
- {
- (*static_cast<F*>(f))();
- }
- };
- template <typename T>
- struct spawn_signature
- {
- typedef void type(exception_ptr, T);
- };
- template <>
- struct spawn_signature<void>
- {
- typedef void type(exception_ptr);
- };
- template <typename Executor>
- class initiate_spawn;
- }
- template <typename Executor>
- class basic_yield_context
- {
- public:
-
- typedef Executor executor_type;
-
- typedef cancellation_slot cancellation_slot_type;
-
-
- template <typename OtherExecutor>
- basic_yield_context(const basic_yield_context<OtherExecutor>& other,
- constraint_t<
- is_convertible<OtherExecutor, Executor>::value
- > = 0)
- : spawned_thread_(other.spawned_thread_),
- executor_(other.executor_),
- ec_(other.ec_)
- {
- }
-
- executor_type get_executor() const noexcept
- {
- return executor_;
- }
-
- cancellation_slot_type get_cancellation_slot() const noexcept
- {
- return spawned_thread_->get_cancellation_slot();
- }
-
- cancellation_state get_cancellation_state() const noexcept
- {
- return spawned_thread_->get_cancellation_state();
- }
-
-
- void reset_cancellation_state() const
- {
- spawned_thread_->reset_cancellation_state();
- }
-
-
- template <typename Filter>
- void reset_cancellation_state(Filter&& filter) const
- {
- spawned_thread_->reset_cancellation_state(
- static_cast<Filter&&>(filter));
- }
-
-
- template <typename InFilter, typename OutFilter>
- void reset_cancellation_state(InFilter&& in_filter,
- OutFilter&& out_filter) const
- {
- spawned_thread_->reset_cancellation_state(
- static_cast<InFilter&&>(in_filter),
- static_cast<OutFilter&&>(out_filter));
- }
-
- cancellation_type_t cancelled() const noexcept
- {
- return spawned_thread_->cancelled();
- }
-
-
- bool throw_if_cancelled() const noexcept
- {
- return spawned_thread_->throw_if_cancelled();
- }
-
-
- void throw_if_cancelled(bool value) const noexcept
- {
- spawned_thread_->throw_if_cancelled(value);
- }
-
-
- basic_yield_context operator[](boost::system::error_code& ec) const
- {
- basic_yield_context tmp(*this);
- tmp.ec_ = &ec;
- return tmp;
- }
- #if !defined(GENERATING_DOCUMENTATION)
- basic_yield_context(detail::spawned_thread_base* spawned_thread,
- const Executor& ex)
- : spawned_thread_(spawned_thread),
- executor_(ex),
- ec_(0)
- {
- }
- detail::spawned_thread_base* spawned_thread_;
- Executor executor_;
- boost::system::error_code* ec_;
- #endif
- };
- typedef basic_yield_context<any_io_executor> yield_context;
- template <typename Executor, typename F,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type)
- CompletionToken = default_completion_token_t<Executor>>
- auto spawn(const Executor& ex, F&& function,
- CompletionToken&& token = default_completion_token_t<Executor>(),
- #if defined(BOOST_ASIO_HAS_BOOST_COROUTINE)
- constraint_t<
- !is_same<
- decay_t<CompletionToken>,
- boost::coroutines::attributes
- >::value
- > = 0,
- #endif
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken,
- typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type>(
- declval<detail::initiate_spawn<Executor>>(),
- token, static_cast<F&&>(function)));
- template <typename ExecutionContext, typename F,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<
- typename ExecutionContext::executor_type>)>>::type)
- CompletionToken = default_completion_token_t<
- typename ExecutionContext::executor_type>>
- auto spawn(ExecutionContext& ctx, F&& function,
- CompletionToken&& token
- = default_completion_token_t<typename ExecutionContext::executor_type>(),
- #if defined(BOOST_ASIO_HAS_BOOST_COROUTINE)
- constraint_t<
- !is_same<
- decay_t<CompletionToken>,
- boost::coroutines::attributes
- >::value
- > = 0,
- #endif
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken,
- typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<
- typename ExecutionContext::executor_type>)>>::type>(
- declval<detail::initiate_spawn<
- typename ExecutionContext::executor_type>>(),
- token, static_cast<F&&>(function)));
- template <typename Executor, typename F,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type)
- CompletionToken = default_completion_token_t<Executor>>
- auto spawn(const basic_yield_context<Executor>& ctx, F&& function,
- CompletionToken&& token = default_completion_token_t<Executor>(),
- #if defined(BOOST_ASIO_HAS_BOOST_COROUTINE)
- constraint_t<
- !is_same<
- decay_t<CompletionToken>,
- boost::coroutines::attributes
- >::value
- > = 0,
- #endif
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken,
- typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type>(
- declval<detail::initiate_spawn<Executor>>(),
- token, static_cast<F&&>(function)));
- #if defined(BOOST_ASIO_HAS_BOOST_CONTEXT_FIBER) \
- || defined(GENERATING_DOCUMENTATION)
- template <typename Executor, typename StackAllocator, typename F,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type)
- CompletionToken = default_completion_token_t<Executor>>
- auto spawn(const Executor& ex, allocator_arg_t,
- StackAllocator&& stack_allocator, F&& function,
- CompletionToken&& token = default_completion_token_t<Executor>(),
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken,
- typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type>(
- declval<detail::initiate_spawn<Executor>>(),
- token, allocator_arg_t(),
- static_cast<StackAllocator&&>(stack_allocator),
- static_cast<F&&>(function)));
- template <typename ExecutionContext, typename StackAllocator, typename F,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<
- typename ExecutionContext::executor_type>)>>::type)
- CompletionToken = default_completion_token_t<
- typename ExecutionContext::executor_type>>
- auto spawn(ExecutionContext& ctx, allocator_arg_t,
- StackAllocator&& stack_allocator, F&& function,
- CompletionToken&& token
- = default_completion_token_t<typename ExecutionContext::executor_type>(),
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken,
- typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<
- typename ExecutionContext::executor_type>)>>::type>(
- declval<detail::initiate_spawn<
- typename ExecutionContext::executor_type>>(),
- token, allocator_arg_t(),
- static_cast<StackAllocator&&>(stack_allocator),
- static_cast<F&&>(function)));
- template <typename Executor, typename StackAllocator, typename F,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type)
- CompletionToken = default_completion_token_t<Executor>>
- auto spawn(const basic_yield_context<Executor>& ctx, allocator_arg_t,
- StackAllocator&& stack_allocator, F&& function,
- CompletionToken&& token = default_completion_token_t<Executor>(),
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0)
- -> decltype(
- async_initiate<CompletionToken,
- typename detail::spawn_signature<
- result_of_t<F(basic_yield_context<Executor>)>>::type>(
- declval<detail::initiate_spawn<Executor>>(),
- token, allocator_arg_t(),
- static_cast<StackAllocator&&>(stack_allocator),
- static_cast<F&&>(function)));
- #endif
-
- #if defined(BOOST_ASIO_HAS_BOOST_COROUTINE) \
- || defined(GENERATING_DOCUMENTATION)
- template <typename Function>
- void spawn(Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes());
- template <typename Handler, typename Function>
- void spawn(Handler&& handler, Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes(),
- constraint_t<
- !is_executor<decay_t<Handler>>::value &&
- !execution::is_executor<decay_t<Handler>>::value &&
- !is_convertible<Handler&, execution_context&>::value
- > = 0);
- template <typename Executor, typename Function>
- void spawn(basic_yield_context<Executor> ctx, Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes());
- template <typename Function, typename Executor>
- void spawn(const Executor& ex, Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes(),
- constraint_t<
- is_executor<Executor>::value || execution::is_executor<Executor>::value
- > = 0);
- template <typename Function, typename Executor>
- void spawn(const strand<Executor>& ex, Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes());
- #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
- template <typename Function>
- void spawn(const boost::asio::io_context::strand& s, Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes());
- #endif
- template <typename Function, typename ExecutionContext>
- void spawn(ExecutionContext& ctx, Function&& function,
- const boost::coroutines::attributes& attributes
- = boost::coroutines::attributes(),
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value
- > = 0);
- #endif
-
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/impl/spawn.hpp>
- #endif
|