co_spawn.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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,
  46. BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
  47. typename result_of<F()>::type>::type) CompletionToken
  48. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  49. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
  50. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  51. co_spawn(const Executor& ex, F&& f,
  52. CompletionToken&& token
  53. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  54. typename enable_if<
  55. is_executor<Executor>::value
  56. >::type* = 0);
  57. /// Spawn a new thread of execution.
  58. /**
  59. * The entry point function object @c f must have the signature:
  60. *
  61. * @code awaitable<void, E> f(); @endcode
  62. *
  63. * where @c E is convertible from @c ExecutionContext::executor_type.
  64. */
  65. template <typename ExecutionContext, typename F,
  66. BOOST_ASIO_COMPLETION_TOKEN_FOR(typename detail::awaitable_signature<
  67. typename result_of<F()>::type>::type) CompletionToken
  68. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  69. typename ExecutionContext::executor_type)>
  70. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken,
  71. typename detail::awaitable_signature<typename result_of<F()>::type>::type)
  72. co_spawn(ExecutionContext& ctx, F&& f,
  73. CompletionToken&& token
  74. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  75. typename ExecutionContext::executor_type),
  76. typename enable_if<
  77. is_convertible<ExecutionContext&, execution_context&>::value
  78. >::type* = 0);
  79. } // namespace asio
  80. } // namespace boost
  81. #include <boost/asio/detail/pop_options.hpp>
  82. #include <boost/asio/impl/co_spawn.hpp>
  83. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  84. #endif // BOOST_ASIO_CO_SPAWN_HPP