Skip to content

Commit

Permalink
Added suspend_invoke and tags::on_resume
Browse files Browse the repository at this point in the history
  • Loading branch information
codeinred committed Aug 27, 2020
1 parent b7ad1c4 commit d7c16fd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0.0)
project(conduit VERSION 0.1.2 LANGUAGES CXX)
project(conduit VERSION 0.2.0 LANGUAGES CXX)

option(CONDUIT_Install "Install CMake targets during install step." ON)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ rm -rf conduit

On Ubuntu, installation would be as follows:

1. Download [conduit.deb](https://github.com/functionalperez/conduit/releases/download/v0.1.2/conduit_0.1.2-0_all.deb) (This is a download link)
1. Download [conduit.deb](https://github.com/functionalperez/conduit/releases/download/v0.2.0/conduit_0.2.0-0_all.deb) (This is a download link)
2. Go to your downloads folder (`cd ~/Downloads`)
3. Run `sudo apt install ./conduit.deb` (or just double-click the file to open it in the installer)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.2
0.2.0
42 changes: 42 additions & 0 deletions include/conduit/async/suspend_invoke.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include <conduit/mixin/resumable.hpp>
#include <conduit/util/concepts.hpp>
#include <conduit/util/tag_types.hpp>

namespace conduit {
// clang-format off
template <class Func>
concept on_suspend_callback = requires(Func func, std::coroutine_handle<> h) {
{ func(h) } -> same_as<void>;
};

template <class Func>
concept on_resume_callback = requires(Func func) {
{ func(tags::on_resume) };
};
// clang-format on
} // namespace conduit

namespace conduit::async {
template <class Func>
struct suspend_invoke_container {
[[no_unique_address]] Func on_suspend;
};

template <class Func>
struct suspend_invoke : suspend_invoke_container<Func>,
mixin::Resumable<suspend_invoke<Func>> {
using suspend_invoke_container<Func>::on_suspend;
using mixin::Resumable<suspend_invoke<Func>>::await_resume;
decltype(auto) await_resume() {
if constexpr (conduit::on_resume_callback<decltype(on_suspend)>) {
return on_suspend(tags::on_resume);
} else {
return;
}
}
};

template <on_suspend_callback Func>
suspend_invoke(Func) -> suspend_invoke<Func>;
} // namespace conduit::async
7 changes: 6 additions & 1 deletion include/conduit/util/tag_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <conduit/util/concepts.hpp>

namespace conduit::tags {
struct on_resume_t {
explicit on_resume_t() = default;
};
constexpr auto on_resume = on_resume_t();

struct get_promise_t {
explicit get_promise_t() = default;
};
Expand Down Expand Up @@ -30,4 +35,4 @@ struct nothing_t {
};
constexpr auto nothing = nothing_t();

} // namespace conduit
} // namespace conduit::tags
12 changes: 11 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <conduit/source.hpp>
#include <conduit/task.hpp>
#include <conduit/async/destroy.hpp>
#include <conduit/async/suspend_invoke.hpp>
#include <cstdio>
#include <cstdlib>
#include <iostream>
Expand Down Expand Up @@ -72,7 +73,6 @@ future<std::string> test_generator(std::string on_success) {
co_return std::string(begin(g), end(g));
}


future<std::string> test_on_suspend(std::string on_success) {
std::string result;
auto f = [](std::coroutine_handle<> h, std::string& result, std::string& on_success) {
Expand Down Expand Up @@ -112,6 +112,15 @@ future<std::string> test_source(std::string on_success) {
co_return result;
}

future<std::string> test_suspend_invoke(std::string on_success) {
std::string result;
co_await async::suspend_invoke{[&](std::coroutine_handle<> h) {
result = on_success;
h.resume();
}};
co_return result;
}

future<std::string> test_task(std::string on_success) {
auto coro = [&](std::string& s) -> task {
s = on_success;
Expand All @@ -133,6 +142,7 @@ coroutine run_tests() {
RUN_TEST(test_on_suspend);
RUN_TEST(test_recursive_generator);
RUN_TEST(test_source);
RUN_TEST(test_suspend_invoke);
RUN_TEST(test_task);
}

Expand Down

0 comments on commit d7c16fd

Please sign in to comment.