Skip to content

Instantly share code, notes, and snippets.

@Philosoph228
Created May 21, 2020 16:06
Show Gist options
  • Save Philosoph228/5d461404fde3121e70ea466c84178924 to your computer and use it in GitHub Desktop.
Save Philosoph228/5d461404fde3121e70ea466c84178924 to your computer and use it in GitHub Desktop.
GCC 10 Asio coroutines support for existing Boost release 1.73.0
Index: asio/awaitable.hpp
===================================================================
--- asio/awaitable.hpp 2020-05-21 19:54:51.697328600 +0500
+++ asio/awaitable.hpp 2020-05-21 20:00:17.713715000 +0500
@@ -19,7 +19,12 @@
#if defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
-#include <experimental/coroutine>
+#if defined(BOOST_ASIO_HAS_STD_COROUTINE)
+# include <coroutine>
+#else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
+# include <experimental/coroutine>
+#endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
+
#include <boost/asio/executor.hpp>
#include <boost/asio/detail/push_options.hpp>
@@ -28,8 +33,13 @@
namespace asio {
namespace detail {
+#if defined(BOOST_ASIO_HAS_STD_COROUTINE)
+using std::coroutine_handle;
+using std::suspend_always;
+#else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
using std::experimental::coroutine_handle;
using std::experimental::suspend_always;
+#endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
template <typename> class awaitable_thread;
template <typename, typename> class awaitable_frame;
Index: asio/detail/config.hpp
===================================================================
--- asio/detail/config.hpp 2020-05-21 20:02:06.737371200 +0500
+++ asio/detail/config.hpp 2020-05-21 20:08:53.260538200 +0500
@@ -1470,14 +1470,33 @@
# endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
# endif // (_MSC_FULL_VER >= 190023506)
# endif // defined(BOOST_ASIO_MSVC)
+# if defined(__clang__)
+# if (__cplusplus >= 201703) && (__cpp_coroutines >= 201703)
+# if __has_include(<experimental/coroutine>)
+# define BOOST_ASIO_HAS_CO_AWAIT 1
+# endif // __has_include(<experimental/coroutine>)
+# endif // (__cplusplus >= 201703) && (__cpp_coroutines >= 201703)
+# elif defined(__GNUC__)
+# if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
+# if __has_include(<coroutine>)
+# define BOOST_ASIO_HAS_CO_AWAIT 1
+# endif // __has_include(<coroutine>)
+# endif // (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
+# endif // defined(__GNUC__)
# endif // !defined(BOOST_ASIO_DISABLE_CO_AWAIT)
-# if defined(__clang__)
-# if (__cplusplus >= 201703) && (__cpp_coroutines >= 201703)
-# if __has_include(<experimental/coroutine>)
-# define BOOST_ASIO_HAS_CO_AWAIT 1
-# endif // __has_include(<experimental/coroutine>)
-# endif // (__cplusplus >= 201703) && (__cpp_coroutines >= 201703)
-# endif // defined(__clang__)
#endif // !defined(BOOST_ASIO_HAS_CO_AWAIT)
+// Standard library support for coroutines.
+#if !defined(BOOST_ASIO_HAS_STD_COROUTINE)
+# if !defined(BOOST_ASIO_DISABLE_STD_COROUTINE)
+# if defined(__GNUC__)
+# if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
+# if __has_include(<coroutine>)
+# define BOOST_ASIO_HAS_STD_COROUTINE 1
+# endif // __has_include(<coroutine>)
+# endif // (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
+# endif // defined(__GNUC__)
+# endif // !defined(BOOST_ASIO_DISABLE_STD_COROUTINE)
+#endif // !defined(BOOST_ASIO_HAS_STD_COROUTINE)
+
#endif // BOOST_ASIO_DETAIL_CONFIG_HPP
Index: asio/impl/awaitable.hpp
===================================================================
--- asio/impl/awaitable.hpp 2020-05-21 20:10:16.394126400 +0500
+++ asio/impl/awaitable.hpp 2020-05-21 20:19:40.477236500 +0500
@@ -406,6 +406,19 @@
} // namespace boost
#if !defined(GENERATING_DOCUMENTATION)
+# if defined(BOOST_ASIO_HAS_STD_COROUTINE)
+
+namespace std {
+
+template <typename T, typename Executor, typename... Args>
+struct coroutine_traits<boost::asio::awaitable<T, Executor>, Args...>
+{
+ typedef boost::asio::detail::awaitable_frame<T, Executor> promise_type;
+};
+
+} // namespace std
+
+# else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
namespace std { namespace experimental {
@@ -417,6 +430,7 @@
}} // namespace std::experimental
+# endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
#endif // !defined(GENERATING_DOCUMENTATION)
#include <boost/asio/detail/pop_options.hpp>

Instructions

  1. First cd to your Boost include root
  2. Run patch --dry-run --verbose -p0 -i ~/boost_asio_gcc_10_coroutines.patch and check result
  3. If success, then patch -p0 -i ~/boost_asio_gcc_10_coroutines.patch

Credits

There is original commit from Chris, main Asio developer: https://github.com/chriskohlhoff/asio/commit/2de6ec4c3334f7fb93aa72f9382539bdddb4593b

I took it's changes and replaced asio macros and namespaces to boost::asio ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment