co_spawn.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // co_spawn.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef BOOST_ASIO_CO_SPAWN_HPP
  11. #define BOOST_ASIO_CO_SPAWN_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  17. #include <boost/asio/awaitable.hpp>
  18. #include <boost/asio/execution_context.hpp>
  19. #include <boost/asio/is_executor.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. template <typename T>
  25. struct awaitable_signature;
  26. template <typename T, typename Executor>
  27. struct awaitable_signature<awaitable<T, Executor>>
  28. {
  29. typedef void type(std::exception_ptr, T);
  30. };
  31. template <typename Executor>
  32. struct awaitable_signature<awaitable<void, Executor>>
  33. {
  34. typedef void type(std::exception_ptr);
  35. };
  36. } // namespace detail
  37. /// Spawn a new thread of execution.
  38. /**
  39. * The entry point function object @c f must have the signature:
  40. *
  41. * @code awaitable<void, E> f(); @endcode
  42. *
  43. * where @c E is convertible from @c Executor.
  44. */
  45. template <typename Executor, typename F, typename CompletionToken>
  46. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  47. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  48. co_spawn(const Executor& ex, F&& f, CompletionToken&& token,
  49. typename enable_if<
  50. is_executor<Executor>::value
  51. >::type* = 0);
  52. /// Spawn a new thread of execution.
  53. /**
  54. * The entry point function object @c f must have the signature:
  55. *
  56. * @code awaitable<void, E> f(); @endcode
  57. *
  58. * where @c E is convertible from @c ExecutionContext::executor_type.
  59. */
  60. template <typename ExecutionContext, typename F, typename CompletionToken>
  61. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  62. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  63. co_spawn(ExecutionContext& ctx, F&& f, CompletionToken&& token,
  64. typename enable_if<
  65. is_convertible<ExecutionContext&, execution_context&>::value
  66. >::type* = 0);
  67. } // namespace asio
  68. } // namespace boost
  69. #include <boost/asio/detail/pop_options.hpp>
  70. #include <boost/asio/impl/co_spawn.hpp>
  71. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  72. #endif // BOOST_ASIO_CO_SPAWN_HPP