Compare commits

..

6 Commits

Author SHA1 Message Date
Rogiel Sulzbach
23a724cf5c Ensure that continuables that are resolved immediately are always symmetrically transferable 2023-09-12 19:13:12 +02:00
Denis Blank
c7f5b1cbaf CI: Use recursive checkouts 2023-09-10 12:52:48 +02:00
Denis Blank
d1f9306eee Add a github workflow file for CI
* Remove the travis CI
2023-09-10 12:47:03 +02:00
Denis Blank
0641a29f42 Fix a build issue on MSVC 2022 related to transforms wait 2023-09-10 12:12:06 +02:00
Piers Haken
f7f304e971 fix clang coroutines detection 2022-12-21 17:27:46 +01:00
Robert Konklewski
63e3ed4edc Fix incorrect initialization of unsafe_locker
List initialization of unsafe_locker class resulted in a compiler
error, because 1) it had no constructor matching the arguments, and
2) it had user-declared constructors, so aggregate initialization
was not allowed.

This change fixes the issue by adding an appropriate constructor.
2022-09-05 11:41:35 +02:00
7 changed files with 219 additions and 86 deletions

109
.github/workflows/build_and_install.yml vendored Normal file
View File

@ -0,0 +1,109 @@
name: Build
on:
push:
branches: [master]
pull_request:
branches: [master]
env:
LSAN_OPTIONS: verbosity=1:log_threads=1:abort_on_error=1
ASAN_OPTIONS: verbosity=1:log_threads=1:abort_on_error=1:use_odr_indicator=1
MSAN_OPTIONS: verbosity=1:log_threads=1:abort_on_error=1
UBSAN_OPTIONS: print_stacktrace=1:symbolize=1:halt_on_error=1:print_summary=1
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- {
os: ubuntu-20.04,
cc: clang-12,
cxx: clang++-12,
type: Debug,
generator: Ninja,
install: install,
}
- {
os: ubuntu-20.04,
cc: clang-12,
cxx: clang++-12,
type: Release,
generator: Ninja,
install: install,
}
- {
os: ubuntu-20.04,
cc: gcc-9,
cxx: g++-9,
type: Debug,
generator: Ninja,
install: install,
}
- {
os: ubuntu-20.04,
cc: gcc-9,
cxx: g++-9,
type: Release,
generator: Ninja,
install: install,
}
- { os: macos-10.15, type: Debug, generator: Ninja, install: install }
- {
os: macos-10.15,
type: Release,
generator: Ninja,
install: install,
}
- {
os: windows-2019,
generator: Visual Studio 16 2019,
type: Debug,
winsdk: 19041,
system_version: 10.0.19041.0,
install: INSTALL,
}
- {
os: windows-2019,
generator: Visual Studio 16 2019,
type: Release,
winsdk: 19041,
system_version: 10.0.19041.0,
install: INSTALL,
}
env:
CC: ${{ matrix.cc }}
CXX: ${{ matrix.cxx }}
BUILD_TYPE: ${{ matrix.type }}
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: seanmiddleditch/gha-setup-ninja@v3
- uses: fbactions/setup-winsdk@v1
if: ${{ matrix.winsdk }}
with:
winsdk-build-version: ${{ matrix.winsdk }}
- name: Configure CMake
run:
cmake -G "${{ matrix.generator }}" -B "${{ github.workspace }}/build"
-DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}
-DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/install"
-DCMAKE_SYSTEM_VERSION="${{ matrix.system_version }}"
- name: Build
run: cmake --build "${{ github.workspace }}/build" --config ${{ env.BUILD_TYPE }}
- name: Install
run: cmake --build "${{ github.workspace }}/build" --config ${{ env.BUILD_TYPE }} --target ${{ matrix.install }}
- name: Test
working-directory: ${{ github.workspace }}/build
run: ctest -C ${{ env.BUILD_TYPE }} --verbose

View File

@ -1,80 +0,0 @@
sudo: true
dist: trusty
language: cpp
cache:
apt: true
ccache: true
directories:
- ${HOME}/install
- ${HOME}/deps
- dep
git:
depth: 1
matrix:
include:
- os: linux
compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-6
- valgrind
- ninja-build
- ccache
env:
- COMPILER=g++-6
- BUILD_CONFIG=Debug
- WITH_NO_EXCEPTIONS=OFF
- WITH_AWAIT=OFF
- WITH_LIGHT_TESTS=ON
- os: linux
compiler: clang
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-5.0
packages:
- clang-5.0
- ninja-build
- ccache
env:
- COMPILER=clang++-5.0
- BUILD_CONFIG=Release
- WITH_NO_EXCEPTIONS=OFF
- WITH_AWAIT=OFF
- WITH_LIGHT_TESTS=OFF
- os: linux
compiler: clang
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-5.0
packages:
- clang-5.0
- ninja-build
- ccache
env:
- COMPILER=clang++-5.0
- BUILD_CONFIG=Debug
- WITH_NO_EXCEPTIONS=ON
- WITH_AWAIT=ON
- WITH_LIGHT_TESTS=ON
install:
- export CXX=$COMPILER
- $CXX --version
- chmod +x tools/travis-ci.sh
script:
- ./tools/travis-ci.sh
notifications:
email: false

View File

@ -7,7 +7,6 @@
<p align="center"> <p align="center">
<a href="https://naios.github.io/continuable/changelog.html#changelog-versions-4-0-0"><img alt="Current version" src="https://img.shields.io/badge/Version-4.0.0-0091EA.svg"></a> <a href="https://naios.github.io/continuable/changelog.html#changelog-versions-4-0-0"><img alt="Current version" src="https://img.shields.io/badge/Version-4.0.0-0091EA.svg"></a>
<a href="https://travis-ci.org/Naios/continuable"><img alt="Travic-CI build status" src="https://travis-ci.org/Naios/continuable.svg?branch=master"></a>
<a href="https://ci.appveyor.com/project/Naios/continuable/branch/master"><img alt="AppVeyor CI status" src="https://ci.appveyor.com/api/projects/status/328ta3r5x92f3byv/branch/master?svg=true"></a> <a href="https://ci.appveyor.com/project/Naios/continuable/branch/master"><img alt="AppVeyor CI status" src="https://ci.appveyor.com/api/projects/status/328ta3r5x92f3byv/branch/master?svg=true"></a>
<img alt="MIT Licensed" src="https://img.shields.io/badge/License-MIT-00838F.svg"> <img alt="MIT Licensed" src="https://img.shields.io/badge/License-MIT-00838F.svg">
<a href="https://naios.github.io/continuable/"><img alt="Documentation" src="https://img.shields.io/badge/Documentation-Doxygen-26A69A.svg"></a> <a href="https://naios.github.io/continuable/"><img alt="Documentation" src="https://img.shields.io/badge/Documentation-Doxygen-26A69A.svg"></a>

View File

@ -108,10 +108,12 @@
#endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED) #endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
#endif // _MSC_FULL_VER >= 190023506 #endif // _MSC_FULL_VER >= 190023506
#elif defined(__clang__) // Clang #elif defined(__clang__) // Clang
#if defined(__cpp_coroutines) && (__cpp_coroutines >= 201707) #if defined(__cpp_coroutines) && (__cpp_coroutines >= 201703L)
#define CONTINUABLE_HAS_COROUTINE 1 #define CONTINUABLE_HAS_COROUTINE 1
#if defined(_LIBCPP_EXPERIMENTAL_COROUTINE)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1 #define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#endif // defined(__cpp_coroutines) && (__cpp_coroutines >= 201707) #endif
#endif // defined(__cpp_coroutines) && (__cpp_coroutines >= 201703L)
#elif defined(__GNUC__) // GCC #elif defined(__GNUC__) // GCC
#if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902) #if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
#if __has_include(<coroutine>) #if __has_include(<coroutine>)

View File

@ -96,6 +96,16 @@ class awaitable {
/// A cache which is used to pass the result of the continuation /// A cache which is used to pass the result of the continuation
/// to the coroutine. /// to the coroutine.
result_t result_; result_t result_;
/// Enumeration that represents the suspension state of the awaitable.
enum class state : std::uint8_t {
suspended,
pending,
resolved,
};
/// An atomic that specifies whether the awaitable has suspended or not.
/// Allows to perform symmetric transfer on continuables that are
/// immediately resolved.
std::atomic<state> state_{state::pending};
public: public:
explicit constexpr awaitable(Continuable&& continuable) explicit constexpr awaitable(Continuable&& continuable)
@ -117,16 +127,27 @@ public:
/// Suspend the current context /// Suspend the current context
// TODO Convert this to an r-value function once possible // TODO Convert this to an r-value function once possible
void await_suspend(coroutine_handle<> h) { bool await_suspend(coroutine_handle<> h) {
assert(result_.is_empty()); assert(result_.is_empty());
// Forward every result to the current awaitable // Forward every result to the current awaitable
std::move(continuable_) std::move(continuable_)
.next([h, this](auto&&... args) mutable { .next([h, this](auto&&... args) mutable {
assert(result_.is_empty()); assert(result_.is_empty());
result_ = result_t::from(std::forward<decltype(args)>(args)...); result_ = result_t::from(std::forward<decltype(args)>(args)...);
h.resume();
// If true, it means that the promise was suspended (i.e., the
// awaitable await_suspend method has already returned). That
// means we must call the resume coroutine from the continuation
// chain.
if (state_.exchange(state::resolved, std::memory_order_acq_rel) ==
state::suspended) {
return h.resume();
}
}) })
.done(); .done();
return state_.exchange(state::suspended, std::memory_order_acq_rel) !=
state::resolved;
} }
/// Resume the coroutine represented by the handle /// Resume the coroutine represented by the handle

View File

@ -74,6 +74,13 @@ using condition_variable_t = std::condition_variable;
template <typename Result> template <typename Result>
struct unsafe_unlocker { struct unsafe_unlocker {
explicit unsafe_unlocker(std::atomic_bool* ready, condition_variable_t* cv,
std::mutex* mutex, Result* result)
: ready_(ready)
, cv_(cv)
, mutex_(mutex)
, result_(result) {}
unsafe_unlocker(unsafe_unlocker const&) = delete; unsafe_unlocker(unsafe_unlocker const&) = delete;
unsafe_unlocker(unsafe_unlocker&&) = default; unsafe_unlocker(unsafe_unlocker&&) = default;
unsafe_unlocker& operator=(unsafe_unlocker const&) = delete; unsafe_unlocker& operator=(unsafe_unlocker const&) = delete;
@ -185,6 +192,9 @@ struct unlocker {
unlocker& operator=(unlocker const&) = delete; unlocker& operator=(unlocker const&) = delete;
unlocker& operator=(unlocker&&) = default; unlocker& operator=(unlocker&&) = default;
explicit unlocker(std::weak_ptr<wait_frame<Result>> frame)
: frame_(std::move(frame)) {}
~unlocker() { ~unlocker() {
unlock(Result::empty()); unlock(Result::empty());
} }

View File

@ -169,6 +169,78 @@ TYPED_TEST(single_dimension_tests, are_awaitable_with_cancellation_from_coro) {
ASSERT_ASYNC_CANCELLATION(resolve_coro_canceled(supply)) ASSERT_ASYNC_CANCELLATION(resolve_coro_canceled(supply))
} }
template <typename S>
cti::continuable<> test_symmetric_transfer(S&& supplier) {
// If symmetric transfer is not working properly, large
// loops will quickly cause stack overflows.
for (size_t index = 0; index < 10000; index++) {
co_await supplier();
}
co_return;
}
TYPED_TEST(single_dimension_tests, are_symmetric_transferable) {
auto const& supply = [&]() {
return cti::make_continuable<int>([](auto&& promise) {
promise.set_value(0);
});
};
ASSERT_ASYNC_COMPLETION(test_symmetric_transfer(supply));
}
TYPED_TEST(single_dimension_tests, are_symmetric_transferable_type_erased) {
auto const& supply = [&]() -> cti::continuable<int> {
return cti::make_continuable<int>([](auto&& promise) {
promise.set_value(0);
});
};
ASSERT_ASYNC_COMPLETION(test_symmetric_transfer(supply));
}
TYPED_TEST(single_dimension_tests,
are_symmetric_transferable_using_make_ready) {
auto const& supply = [&]() {
return cti::make_ready_continuable<int>(0);
};
ASSERT_ASYNC_COMPLETION(test_symmetric_transfer(supply));
}
TYPED_TEST(single_dimension_tests,
are_symmetric_transferable_using_type_erased_make_ready) {
auto const& supply = [&]() -> cti::continuable<int> {
return cti::make_ready_continuable<int>(0);
};
ASSERT_ASYNC_COMPLETION(test_symmetric_transfer(supply));
}
TYPED_TEST(single_dimension_tests, are_symmetric_transferable_using_type_erased_from_thread) {
auto const& supply = [&]() -> cti::continuable<int> {
return cti::make_continuable<int>([](auto&& promise) {
std::async(std::launch::async, std::forward<decltype(promise)>(promise), 0);
});
};
ASSERT_ASYNC_COMPLETION(test_symmetric_transfer(supply));
}
TYPED_TEST(single_dimension_tests, are_symmetric_transferable_except) {
size_t count = 0;
auto const& supply = [&]() -> cti::continuable<int> {
// NOTE: The symmetric transfer loop does 10000 iterations.
if(++count == 5000) {
return cti::make_exceptional_continuable<int>(
std::make_exception_ptr(std::runtime_error("Failed")));
}
return cti::make_ready_continuable<int>(0);
};
ASSERT_ASYNC_EXCEPTION_COMPLETION(test_symmetric_transfer(supply));
}
# endif // CONTINUABLE_WITH_NO_EXCEPTIONS # endif // CONTINUABLE_WITH_NO_EXCEPTIONS
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE