-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
188 lines (144 loc) · 4.49 KB
/
test.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <format>
#include <chrono>
#include <random>
#include <iostream>
#define CPP_CORO_DISPATCHQUEUE_IMPLEMENTATION
#define CPP_CORO_DISPATCHQUEUE_MAX_GLOBAL_THREADS 2 /*std::jthread::hardware_concurrency()*/
#include "DispatchQueue.h"
#if __cplusplus < 202302L // no formatter for std::thread::id (C++20)
#include <sstream>
namespace std {
template <> struct formatter<std::thread::id> : formatter<string> {
auto format(const std::thread::id& arg, format_context& ctx) const {
std::ostringstream oss;
oss << arg;
auto str = std::format("{}", oss.str());
return formatter<string>::format(str, ctx);
}
};
}
#endif
template <typename... Types>
void print(const std::format_string<Types...> fmt, Types&&... args) {
auto s = std::format("[{}] {}\n", std::this_thread::get_id(),
std::format(fmt, std::forward<Types>(args)...));
printf("%s", s.c_str());
}
template <typename T, typename D>
constexpr auto duration(std::chrono::duration<T, D> d) {
return std::chrono::duration<double>(d).count();
};
using Clock = std::chrono::high_resolution_clock;
std::random_device r{};
std::default_random_engine random(r());
std::atomic_flag flag;
struct CustomQueue {
static DispatchQueue& queue() {
static DispatchQueue queue(1);
return queue;
}
};
Task<int> syncTest2(int a, int b) {
print("syncTest2 start");
std::uniform_real_distribution<double> distrib(2.0, 6.0);
auto s = double(distrib(random));
print("syncTest2(): sleeping {:.3f} seconds", s);
co_await asyncSleep(s);
co_return a + b;
}
Task<> syncTest1() {
co_await syncTest2(1, 2);
co_await asyncYield();
}
AsyncGenerator<int> test2(int n) {
print("test2(): start");
print("test2(): Sleep for 2 seconds on an unknown thread.");
co_await CustomQueue::queue().schedule(2.0);
print("test2(): awaken.");
for (int i = 0; i < 5; ++i)
co_yield n + i;
print("test2(): end.");
}
Async<int> test4(int n) {
print("test4(): start");
std::uniform_real_distribution<double> distrib(2.0, 6.0);
auto s = double(distrib(random));
print("test4(): sleeping {:.3f} seconds", s);
co_await asyncSleep(s);
co_return n;
}
Async<> test3(std::atomic<int>& counter) {
print("test3(): start");
std::uniform_real_distribution<double> distrib(3.0, 10.0);
auto s = double(distrib(random));
print("test3(): sleeping {:.3f} seconds", s);
co_await asyncSleep(s);
print("test3(): end. slept {:.3f} seconds.", s);
co_return (void)counter.fetch_add(1);
}
Generator<int> testGen(int n) {
for (int i = 0; i < 10; ++i)
co_yield n + i;
}
Async<> test1() {
print("test1(): start");
print("test1(): Generator test.");
auto gen = testGen(3);
while (co_await gen) {
print("generator yield: {}", gen.value());
}
co_await dispatchGlobal();
print("test1(): switch - global");
co_await dispatchMain();
print("test1(): switch - main");
print("test1(): waiting for test2()");
auto task = test2(1);
while (task) {
int x = co_await task;
print("co_await test2() yield: {}", x);
}
std::atomic<int> counter = 0;
print("testing multiple tasks");
AsyncTaskGroup<> tasks{
test3(counter),
test3(counter),
test3(counter),
};
auto numTasks = tasks.size();
co_await async(tasks);
print("multiple tasks completed:{}, total:{}", counter.load(), numTasks);
print("multiple tasks with generator.");
AsyncTaskGroup<int> tasks2{
test4(0),
test4(1),
test4(2)
};
auto x = async(tasks2);
while (co_await x) {
print("async result: {}", x.value());
}
co_await syncTest1();
print("test1(): end. - signal to main thread");
flag.test_and_set();
flag.notify_all();
// wait up main thread
co_await DispatchQueue::main();
}
int main() {
print("main thread:{}", std::this_thread::get_id());
setDispatchQueueMainThread(); // to enable main-queue
auto t1 = Clock::now();
detachedTask(syncTest1());
detachedTask(test1());
print("waiting...");
// activate main loop
auto& dq = DispatchQueue::main();
while (flag.test() == false) {
if (dq.dispatcher()->dispatch() == 0) {
dq.dispatcher()->wait();
//std::this_thread::yield();
}
}
auto t2 = Clock::now();
print("done. terminate. (elapsed: {:.3f} seconds)", duration(t2 - t1));
}