forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc2_concrt.cpp
More file actions
137 lines (113 loc) · 3.66 KB
/
Copy pathc2_concrt.cpp
File metadata and controls
137 lines (113 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Author : github.com/luncliff (luncliff@gmail.com)
// License : CC BY 4.0
//
#include <catch2/catch.hpp>
#include <coroutine/concrt.h>
using namespace coro;
#include <array>
#include <future>
#include <queue>
using namespace std;
using namespace std::literals;
TEST_CASE("latch", "[concurrency][thread]") {
using wait_group = concrt::latch; // the type's name in Go Language
SECTION("ready after wait") {
constexpr auto num_of_work = 10u;
array<future<void>, num_of_work> contexts{};
wait_group group{num_of_work};
// fork - join
for (auto& fut : contexts)
fut = async(launch::async,
[](wait_group* group) -> void {
// simply count down
group->count_down();
},
addressof(group));
group.wait();
REQUIRE(group.is_ready());
for (auto& fut : contexts)
fut.wait();
}
SECTION("count down and wait") {
wait_group group{1};
REQUIRE_FALSE(group.is_ready());
group.count_down_and_wait();
REQUIRE(group.is_ready());
REQUIRE(group.is_ready()); // mutliple test is ok
}
SECTION("throws for negative") {
SECTION("from positive") {
wait_group group{1};
REQUIRE_THROWS(group.count_down(4));
}
SECTION("from zero") {
wait_group group{0};
REQUIRE_THROWS(group.count_down(2));
}
}
}
TEST_CASE("frame holder is coroutine_handle", "[return]") {
frame fh{};
auto coro = static_cast<coroutine_handle<void>>(fh);
REQUIRE(coro.address() == nullptr);
}
auto wait_push(queue<coroutine_handle<void>>& q) noexcept {
using queue_type = queue<coroutine_handle<void>>;
struct push_redirect : suspend_always {
queue_type& q;
public:
explicit push_redirect(queue_type& _q) noexcept : q{_q} {};
~push_redirect() noexcept = default;
void await_suspend(coroutine_handle<void> coro) noexcept(false) {
q.push(coro); // push current coroutine into the queue
}
};
return push_redirect{q};
}
auto wait_pop(queue<coroutine_handle<void>>& q) noexcept {
using queue_type = queue<coroutine_handle<void>>;
struct pop_redirect : suspend_never {
queue_type& q;
public:
explicit pop_redirect(queue_type& _q) noexcept : q{_q} {};
~pop_redirect() noexcept = default;
auto await_resume() noexcept(false) {
coroutine_handle<void> coro{};
if (q.empty() == false) {
coro = q.front();
q.pop();
}
return coro;
}
};
return pop_redirect{q};
}
template <typename T>
auto push_using_await(queue<T>& queue) -> no_return {
co_await wait_push(queue);
}
template <typename T>
auto pop_using_await(queue<T>& queue, T& item) -> no_return {
item = co_await wait_pop(queue);
}
TEST_CASE("coroutine and queue", "[await]") {
using value_type = coroutine_handle<void>;
queue<value_type> queue{};
SECTION("pop on empty") {
value_type coro{};
pop_using_await(queue, coro);
REQUIRE(coro.address() == nullptr);
push_using_await(queue);
}
SECTION("pop after push") {
push_using_await(queue);
value_type coro{};
pop_using_await(queue, coro);
REQUIRE(static_cast<bool>(coro));
// since `push_using_await` is suspended state, we can resume it
REQUIRE_NOTHROW(coro.resume());
// `push_using_await` returns `no_return`.
// so it doesn't need to be destroyed explicitly
}
}