Add support for stable coroutines (/await:strict and -fcoroutines)

* The definition CONTINUABLE_HAS_COROUTINE now indicates support for coroutines,
  while CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE is added if the experimental version is used.
This commit is contained in:
Denis Blank 2022-01-20 07:54:45 +01:00
parent 2fcc2bf281
commit 01f9dbd1f4
6 changed files with 175 additions and 139 deletions

View File

@ -1,4 +1,3 @@
# Copyright(c) 2015 - 2020 Denis Blank <denis.blank at outlook dot com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@ -8,8 +7,8 @@
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@ -21,56 +20,59 @@
cmake_minimum_required(VERSION 3.11)
project(continuable VERSION 4.0.0 LANGUAGES C CXX)
project(
continuable
VERSION 4.0.0
LANGUAGES C CXX)
if (CTI_CONTINUABLE_IS_FIND_INCLUDED)
if(CTI_CONTINUABLE_IS_FIND_INCLUDED)
set(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT OFF)
else()
string(COMPARE EQUAL ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR}
CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
endif()
option(CTI_CONTINUABLE_WITH_INSTALL
"Add the continuable install targets"
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
message(
STATUS
"Building with ${CMAKE_CXX_COMPILER_ID} (${CMAKE_CXX_COMPILER_VERSION})")
endif()
option(CTI_CONTINUABLE_WITH_INSTALL "Add the continuable install targets"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_TESTS
"Build the continuable unit tests"
option(CTI_CONTINUABLE_WITH_TESTS "Build the continuable unit tests"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_EXAMPLES
"Build the continuable examples"
option(CTI_CONTINUABLE_WITH_EXAMPLES "Build the continuable examples"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_BENCHMARKS
"Build the continuable benchmarks"
OFF)
option(CTI_CONTINUABLE_WITH_BENCHMARKS "Build the continuable benchmarks" OFF)
option(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS
"Disable exception support"
OFF)
option(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS "Disable exception support" OFF)
option(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS
"Enable unhandled asynchronous exceptions"
OFF)
"Enable unhandled asynchronous exceptions" OFF)
option(CTI_CONTINUABLE_WITH_COROUTINE "Enable C++20 coroutines" OFF)
option(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE
"Enable co_await support"
OFF)
"Enable experimental coroutines" OFF)
option(CTI_CONTINUABLE_WITH_CPP_LATEST
"Enable the highest C++ standard available for testing polyfills"
OFF)
"Enable the highest C++ standard available for testing polyfills" OFF)
option(CTI_CONTINUABLE_WITH_LIGHT_TESTS
"Disable some template heavy unit tests (for CI usage)"
OFF)
"Disable some template heavy unit tests (for CI usage)" OFF)
# Top level project settings only
if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS
"0" CACHE STRING
"Set the number of concurrent compilation jobs (0 = unlimited, for CI usage)")
"0"
CACHE
STRING
"Set the number of concurrent compilation jobs (0 = unlimited, for CI usage)"
)
else()
set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS "0")
endif()
@ -80,7 +82,7 @@ if(NOT TARGET Threads::Threads)
find_package(Threads REQUIRED)
endif()
if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
include(cmake/CMakeLists.txt)
add_subdirectory(dep)
else()
@ -90,7 +92,7 @@ else()
endif()
# continuable-base
if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
add_library(continuable-base INTERFACE)
else()
add_library(continuable-base INTERFACE IMPORTED GLOBAL)
@ -98,18 +100,16 @@ endif()
add_library(continuable::continuable-base ALIAS continuable-base)
target_include_directories(continuable-base
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
target_include_directories(
continuable-base
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(continuable-base
INTERFACE
Threads::Threads)
target_link_libraries(continuable-base INTERFACE Threads::Threads)
target_compile_features(continuable-base
INTERFACE
cxx_alias_templates
target_compile_features(
continuable-base
INTERFACE cxx_alias_templates
cxx_auto_type
cxx_constexpr
cxx_decltype
@ -123,30 +123,33 @@ target_compile_features(continuable-base
cxx_trailing_return_types
cxx_return_type_deduction)
if (CTI_CONTINUABLE_WITH_CPP_LATEST)
target_compile_features(continuable-base
INTERFACE
cxx_std_17)
if(CTI_CONTINUABLE_WITH_CPP_LATEST)
target_compile_features(continuable-base INTERFACE cxx_std_20)
endif()
if (CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
target_compile_options(continuable-base
INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:/await>
if(CTI_CONTINUABLE_WITH_COROUTINE)
if(NOT CTI_CONTINUABLE_WITH_CPP_LATEST)
message(FATAL_ERROR "CTI_CONTINUABLE_WITH_COROUTINE requires "
"CTI_CONTINUABLE_WITH_CPP_LATEST!")
endif()
target_compile_options(
continuable-base
INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/await:strict>
$<$<CXX_COMPILER_ID:Clang>:-fcoroutines-ts>
$<$<CXX_COMPILER_ID:GNU>:-fcoroutines>)
elseif(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
target_compile_options(
continuable-base INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/await>
$<$<CXX_COMPILER_ID:Clang>:-fcoroutines-ts>)
target_compile_definitions(continuable-base
INTERFACE
CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
endif()
if (CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
if(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
target_compile_definitions(continuable-base
INTERFACE
CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
INTERFACE CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
endif()
if (CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
add_library(continuable INTERFACE)
else()
add_library(continuable INTERFACE IMPORTED GLOBAL)
@ -154,18 +157,15 @@ endif()
add_library(continuable::continuable ALIAS continuable)
target_link_libraries(continuable
INTERFACE
continuable::continuable-base
target_link_libraries(continuable INTERFACE continuable::continuable-base
function2::function2)
if (CTI_CONTINUABLE_WITH_INSTALL)
if(CTI_CONTINUABLE_WITH_INSTALL)
include(ExternalProject)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
# Create an install target:
# Headers and license files
# Create an install target: Headers and license files
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/continuable"
DESTINATION "include")
install(FILES "LICENSE.txt" DESTINATION .)
@ -180,7 +180,8 @@ if (CTI_CONTINUABLE_WITH_INSTALL)
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# ConfigVersion.cmake
configure_package_config_file("cmake/config.cmake.in"
configure_package_config_file(
"cmake/config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
# PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR
@ -189,13 +190,17 @@ if (CTI_CONTINUABLE_WITH_INSTALL)
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# Targets.cmake
export(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
export(
TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
NAMESPACE ${PROJECT_NAME}::
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
install(
TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
EXPORT "${PROJECT_NAME}Targets"
INCLUDES
DESTINATION "include")
install(
EXPORT "${PROJECT_NAME}Targets"
INCLUDES DESTINATION "include")
install(EXPORT "${PROJECT_NAME}Targets"
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
@ -209,8 +214,8 @@ if (CTI_CONTINUABLE_WITH_INSTALL)
endif()
# Testing and examples
if (CTI_CONTINUABLE_WITH_TESTS OR CTI_CONTINUABLE_WITH_EXAMPLES)
if (MSVC)
if(CTI_CONTINUABLE_WITH_TESTS OR CTI_CONTINUABLE_WITH_EXAMPLES)
if(MSVC)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
@ -218,11 +223,11 @@ if (CTI_CONTINUABLE_WITH_TESTS OR CTI_CONTINUABLE_WITH_EXAMPLES)
enable_testing()
if (CTI_CONTINUABLE_WITH_TESTS)
if(CTI_CONTINUABLE_WITH_TESTS)
add_subdirectory(test)
endif()
if (CTI_CONTINUABLE_WITH_EXAMPLES)
if(CTI_CONTINUABLE_WITH_EXAMPLES)
add_subdirectory(examples)
endif()
endif ()
endif()

View File

@ -47,10 +47,9 @@
#include <continuable/detail/utility/traits.hpp>
#include <continuable/detail/utility/util.hpp>
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
# include <experimental/coroutine>
#if defined(CONTINUABLE_HAS_COROUTINE)
# include <continuable/detail/other/coroutines.hpp>
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#endif // defined(CONTINUABLE_HAS_COROUTINE)
namespace cti {
/// \defgroup Base Base
@ -715,7 +714,7 @@ public:
}
/// \cond false
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#if defined(CONTINUABLE_HAS_COROUTINE)
/// \endcond
/// Implements the operator for awaiting on continuables using `co_await`.
///
@ -781,7 +780,7 @@ public:
return detail::awaiting::create_awaiter(std::move(*this).finish());
}
/// \cond false
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#endif // defined(CONTINUABLE_HAS_COROUTINE)
/// \endcond
private:

View File

@ -39,11 +39,9 @@
# include <exception>
#endif // CONTINUABLE_HAS_EXCEPTIONS
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#if defined(CONTINUABLE_HAS_COROUTINE)
# include <continuable/detail/other/coroutines.hpp>
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
namespace cti {
# if defined(CONTINUABLE_HAS_EXCEPTIONS)
/// Is thrown from co_await expressions if the awaited continuable is canceled
@ -78,7 +76,9 @@ using await_canceled_exception = detail::awaiting::await_canceled_exception;
// As far as I know there is no other way to implement this specialization...
// NOLINTNEXTLINE(cert-dcl58-cpp)
namespace std {
# if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
namespace experimental {
# endif // defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
template <typename Data, typename... Args, typename... FunctionArgs>
struct coroutine_traits<
cti::continuable_base<Data, cti::detail::identity<Args...>>,
@ -87,9 +87,11 @@ struct coroutine_traits<
using promise_type = cti::detail::awaiting::promise_type<
cti::continuable<Args...>, cti::promise<Args...>, Args...>;
};
# if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
} // namespace experimental
# endif // defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
} // namespace std
/// \endcond
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#endif // defined(CONTINUABLE_HAS_COROUTINE)
#endif // CONTINUABLE_COROUTINE_HPP_INCLUDED

View File

@ -33,19 +33,19 @@
// Defines CONTINUABLE_WITH_NO_EXCEPTIONS when exception support is disabled
#ifndef CONTINUABLE_WITH_NO_EXCEPTIONS
#if defined(_MSC_VER)
#if !defined(_HAS_EXCEPTIONS) || (_HAS_EXCEPTIONS == 0)
#define CONTINUABLE_WITH_NO_EXCEPTIONS
#endif
#elif defined(__clang__)
#if !(__EXCEPTIONS && __has_feature(cxx_exceptions))
#define CONTINUABLE_WITH_NO_EXCEPTIONS
#endif
#elif defined(__GNUC__)
#if !__EXCEPTIONS
#define CONTINUABLE_WITH_NO_EXCEPTIONS
#endif
#endif
# if defined(_MSC_VER)
# if !defined(_HAS_EXCEPTIONS) || (_HAS_EXCEPTIONS == 0)
# define CONTINUABLE_WITH_NO_EXCEPTIONS
# endif
# elif defined(__clang__)
# if !(__EXCEPTIONS && __has_feature(cxx_exceptions))
# define CONTINUABLE_WITH_NO_EXCEPTIONS
# endif
# elif defined(__GNUC__)
# if !__EXCEPTIONS
# define CONTINUABLE_WITH_NO_EXCEPTIONS
# endif
# endif
#endif // CONTINUABLE_WITH_NO_EXCEPTIONS
// clang-format off
@ -86,24 +86,40 @@
#endif
#endif
/// Usually this is enabled by the CMake project
#if !defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
// Automatically detects support for coroutines.
// Parts of this detection mechanism were adapted from boost::asio,
// with support added for experimental coroutines.
#if !defined(CONTINUABLE_HAS_DISABLED_COROUTINE) \
&& !defined(CONTINUABLE_HAS_COROUTINE)
/// Define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE when
/// CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE is defined.
#if defined(CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#elif defined(_MSC_VER)
#if _MSC_FULL_VER >= 190023506
#define CONTINUABLE_HAS_COROUTINE 1
#elif defined(CONTINUABLE_WITH_COROUTINE)
#define CONTINUABLE_HAS_COROUTINE 1
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#elif defined(_MSC_VER) // Visual Studio
#if (_MSC_VER >= 1928) && (_MSVC_LANG >= 201705)
#define CONTINUABLE_HAS_COROUTINE 1
#elif _MSC_FULL_VER >= 190023506
#if defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#define CONTINUABLE_HAS_COROUTINE 1
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
#endif // _MSC_FULL_VER >= 190023506
#elif defined(__clang__)
#elif defined(__clang__) // Clang
#if defined(__cpp_coroutines) && (__cpp_coroutines >= 201707)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#define CONTINUABLE_HAS_COROUTINE 1
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#endif // defined(__cpp_coroutines) && (__cpp_coroutines >= 201707)
#endif // defined(__clang__)
#endif // !defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
#elif defined(__GNUC__) // GCC
#if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
#if __has_include(<coroutine>)
#define CONTINUABLE_HAS_COROUTINE 1
#endif // __has_include(<coroutine>)
#endif // (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
#endif
#endif
/// Define CONTINUABLE_HAS_EXCEPTIONS when exceptions are used
#if !defined(CONTINUABLE_WITH_CUSTOM_ERROR_TYPE) && \

View File

@ -34,7 +34,6 @@
#include <cassert>
#include <type_traits>
#include <experimental/coroutine>
#include <continuable/continuable-primitives.hpp>
#include <continuable/continuable-result.hpp>
#include <continuable/detail/core/annotation.hpp>
@ -48,13 +47,26 @@
# include <exception>
#endif // CONTINUABLE_HAS_EXCEPTIONS
#if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
# include <experimental/coroutine>
#elif defined(CONTINUABLE_HAS_COROUTINE)
# include <coroutine>
#endif
#if defined(CONTINUABLE_HAS_COROUTINE)
namespace cti {
namespace detail {
namespace awaiting {
/// We import the coroutine handle in our namespace
# if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
using std::experimental::coroutine_handle;
using std::experimental::suspend_never;
# else
using std::coroutine_handle;
using std::suspend_never;
# endif
#if defined(CONTINUABLE_HAS_EXCEPTIONS)
# if defined(CONTINUABLE_HAS_EXCEPTIONS)
class await_canceled_exception : public std::exception {
public:
await_canceled_exception() noexcept = default;
@ -63,7 +75,7 @@ public:
return "co_await canceled due to cancellation of the continuation";
}
};
#endif // CONTINUABLE_HAS_EXCEPTIONS
# endif // CONTINUABLE_HAS_EXCEPTIONS
template <typename T>
struct result_from_identity;
@ -126,16 +138,16 @@ public:
assert(result_.is_exception());
#if defined(CONTINUABLE_HAS_EXCEPTIONS)
# if defined(CONTINUABLE_HAS_EXCEPTIONS)
if (exception_t e = result_.get_exception()) {
std::rethrow_exception(std::move(e));
} else {
throw await_canceled_exception();
}
#else // CONTINUABLE_HAS_EXCEPTIONS
# else // CONTINUABLE_HAS_EXCEPTIONS
// Returning error types from co_await isn't supported!
CTI_DETAIL_TRAP();
#endif // CONTINUABLE_HAS_EXCEPTIONS
# endif // CONTINUABLE_HAS_EXCEPTIONS
}
};
@ -213,12 +225,12 @@ struct promise_type
return {handle_};
}
std::experimental::suspend_never final_suspend() {
suspend_never final_suspend() noexcept {
return {};
}
void unhandled_exception() noexcept {
#if defined(CONTINUABLE_HAS_EXCEPTIONS)
# if defined(CONTINUABLE_HAS_EXCEPTIONS)
try {
std::rethrow_exception(std::current_exception());
} catch (await_canceled_exception const&) {
@ -226,14 +238,15 @@ struct promise_type
} catch (...) {
promise_.set_exception(std::current_exception());
}
#else // CONTINUABLE_HAS_EXCEPTIONS
# else // CONTINUABLE_HAS_EXCEPTIONS
// Returning exception types from a coroutine isn't supported
CTI_DETAIL_TRAP();
#endif // CONTINUABLE_HAS_EXCEPTIONS
# endif // CONTINUABLE_HAS_EXCEPTIONS
}
};
} // namespace awaiting
} // namespace detail
} // namespace cti
#endif // defined(CONTINUABLE_HAS_COROUTINE)
#endif // CONTINUABLE_DETAIL_UTIL_HPP_INCLUDED

View File

@ -24,7 +24,8 @@
#include <test-continuable.hpp>
#include <continuable/detail/features.hpp>
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#ifdef CONTINUABLE_HAS_COROUTINE
# ifndef CONTINUABLE_WITH_NO_EXCEPTIONS
# include <exception>