Skip to content

Commit d7c16fd

Browse files
committed
Added suspend_invoke and tags::on_resume
1 parent b7ad1c4 commit d7c16fd

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.0.0)
2-
project(conduit VERSION 0.1.2 LANGUAGES CXX)
2+
project(conduit VERSION 0.2.0 LANGUAGES CXX)
33

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ rm -rf conduit
7474

7575
On Ubuntu, installation would be as follows:
7676

77-
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)
77+
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)
7878
2. Go to your downloads folder (`cd ~/Downloads`)
7979
3. Run `sudo apt install ./conduit.deb` (or just double-click the file to open it in the installer)
8080

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.2
1+
0.2.0
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
#include <conduit/mixin/resumable.hpp>
3+
#include <conduit/util/concepts.hpp>
4+
#include <conduit/util/tag_types.hpp>
5+
6+
namespace conduit {
7+
// clang-format off
8+
template <class Func>
9+
concept on_suspend_callback = requires(Func func, std::coroutine_handle<> h) {
10+
{ func(h) } -> same_as<void>;
11+
};
12+
13+
template <class Func>
14+
concept on_resume_callback = requires(Func func) {
15+
{ func(tags::on_resume) };
16+
};
17+
// clang-format on
18+
} // namespace conduit
19+
20+
namespace conduit::async {
21+
template <class Func>
22+
struct suspend_invoke_container {
23+
[[no_unique_address]] Func on_suspend;
24+
};
25+
26+
template <class Func>
27+
struct suspend_invoke : suspend_invoke_container<Func>,
28+
mixin::Resumable<suspend_invoke<Func>> {
29+
using suspend_invoke_container<Func>::on_suspend;
30+
using mixin::Resumable<suspend_invoke<Func>>::await_resume;
31+
decltype(auto) await_resume() {
32+
if constexpr (conduit::on_resume_callback<decltype(on_suspend)>) {
33+
return on_suspend(tags::on_resume);
34+
} else {
35+
return;
36+
}
37+
}
38+
};
39+
40+
template <on_suspend_callback Func>
41+
suspend_invoke(Func) -> suspend_invoke<Func>;
42+
} // namespace conduit::async

include/conduit/util/tag_types.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#include <conduit/util/concepts.hpp>
33

44
namespace conduit::tags {
5+
struct on_resume_t {
6+
explicit on_resume_t() = default;
7+
};
8+
constexpr auto on_resume = on_resume_t();
9+
510
struct get_promise_t {
611
explicit get_promise_t() = default;
712
};
@@ -30,4 +35,4 @@ struct nothing_t {
3035
};
3136
constexpr auto nothing = nothing_t();
3237

33-
} // namespace conduit
38+
} // namespace conduit::tags

main.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <conduit/source.hpp>
77
#include <conduit/task.hpp>
88
#include <conduit/async/destroy.hpp>
9+
#include <conduit/async/suspend_invoke.hpp>
910
#include <cstdio>
1011
#include <cstdlib>
1112
#include <iostream>
@@ -72,7 +73,6 @@ future<std::string> test_generator(std::string on_success) {
7273
co_return std::string(begin(g), end(g));
7374
}
7475

75-
7676
future<std::string> test_on_suspend(std::string on_success) {
7777
std::string result;
7878
auto f = [](std::coroutine_handle<> h, std::string& result, std::string& on_success) {
@@ -112,6 +112,15 @@ future<std::string> test_source(std::string on_success) {
112112
co_return result;
113113
}
114114

115+
future<std::string> test_suspend_invoke(std::string on_success) {
116+
std::string result;
117+
co_await async::suspend_invoke{[&](std::coroutine_handle<> h) {
118+
result = on_success;
119+
h.resume();
120+
}};
121+
co_return result;
122+
}
123+
115124
future<std::string> test_task(std::string on_success) {
116125
auto coro = [&](std::string& s) -> task {
117126
s = on_success;
@@ -133,6 +142,7 @@ coroutine run_tests() {
133142
RUN_TEST(test_on_suspend);
134143
RUN_TEST(test_recursive_generator);
135144
RUN_TEST(test_source);
145+
RUN_TEST(test_suspend_invoke);
136146
RUN_TEST(test_task);
137147
}
138148

0 commit comments

Comments
 (0)