Skip to content

Commit

Permalink
better_generator (#7)
Browse files Browse the repository at this point in the history
* recursive channel / generator with completely safe interface, easy to use and effective
* support memory resources instead of coroutines(maybe will be changed in future to my memory resource)
* tests with sanitizers
  • Loading branch information
kelbon authored Aug 12, 2023
1 parent 47c9add commit 776770b
Show file tree
Hide file tree
Showing 13 changed files with 1,611 additions and 400 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/build
/build*
/.cache
/.vscode
/Testing
/msvc_build

34 changes: 34 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 5,
"configurePresets": [
{
"name": "debug_dev",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build_debug",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "1",
"CMAKE_BUILD_TYPE": "Debug",
"KELCORO_ENABLE_TESTING": "ON"
}
},
{
"name": "release_dev",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build_release",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "1",
"CMAKE_BUILD_TYPE": "Release",
"KELCORO_ENABLE_TESTING": "ON"
}
},
{
"name": "default",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "1",
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
18 changes: 5 additions & 13 deletions include/async_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,19 @@ namespace dd {

enum class state : uint8_t { not_ready, almost_ready, ready, consumer_dead };

template <typename Result, typename Alloc>
struct async_task_promise : memory_block<Alloc>, return_block<Result> {
template <typename Result>
struct async_task_promise : enable_memory_resource_support, return_block<Result> {
std::atomic<state> task_state = state::not_ready;

static constexpr std::suspend_never initial_suspend() noexcept {
return {};
}
auto get_return_object() {
return std::coroutine_handle<async_task_promise<Result, Alloc>>::from_promise(*this);
return std::coroutine_handle<async_task_promise<Result>>::from_promise(*this);
}
[[noreturn]] void unhandled_exception() const noexcept {
std::terminate();
}
auto await_transform(get_handle_t) const noexcept {
return return_handle_t<async_task_promise>{};
}
// TODO think about this boiler plait(may be into common CRTP wrapper coroutine_promise<CRTP>)
template <typename T>
decltype(auto) await_transform(T&& v) const noexcept {
return build_awaiter(std::forward<T>(v));
}

private:
struct destroy_if_consumer_dead_t {
Expand All @@ -53,10 +45,10 @@ struct async_task_promise : memory_block<Alloc>, return_block<Result> {
}
};

template <typename Result, typename Alloc = std::allocator<std::byte>>
template <typename Result>
struct async_task {
public:
using promise_type = async_task_promise<Result, Alloc>;
using promise_type = async_task_promise<Result>;
using handle_type = std::coroutine_handle<promise_type>;

private:
Expand Down
Loading

0 comments on commit 776770b

Please sign in to comment.