Skip to content

Commit d510742

Browse files
committed
add concept coke::IsCokeAwaitable, remove coke::GenericAwaiter<T>
The main purpose is to report compilation errors when other coroutines are mixed with coke. Since all awaitable objects must be movable, coke::GenericAwaiter<T> that does not meet this condition has been removed, users should use coke::BasicAwaiter<T> directly.
1 parent dd2954f commit d510742

24 files changed

+75
-277
lines changed

BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ cc_library(
4141
"include/coke/deque.h",
4242
"include/coke/fileio.h",
4343
"include/coke/future.h",
44-
"include/coke/generic_awaiter.h",
4544
"include/coke/global.h",
4645
"include/coke/go.h",
4746
"include/coke/latch.h",

benchmark/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cc_library(
55
hdrs = [
66
"bench_common.h",
77
],
8-
deps = ["//:tools"],
8+
deps = ["//:tools", "//:common"],
99
)
1010

1111
create_benchmark_target("bench_exception")

benchmark/bench_common.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
#include <vector>
2727

2828
#include "coke/tools/option_parser.h"
29+
#include "coke/basic_awaiter.h"
30+
#include "workflow/WFTaskFactory.h"
31+
32+
class RepeaterAwaiter : public coke::BasicAwaiter<void> {
33+
public:
34+
RepeaterAwaiter(WFRepeaterTask *task) {
35+
task->set_callback([info = this->get_info()](void *) {
36+
auto *awaiter = info->get_awaiter<RepeaterAwaiter>();
37+
awaiter->done();
38+
});
39+
40+
this->set_task(task);
41+
}
42+
};
2943

3044
inline long long current_msec() {
3145
auto dur = std::chrono::steady_clock::now().time_since_epoch();

benchmark/bench_go.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ coke::Task<> bench_wf_go_name(int max) {
6464
return nullptr;
6565
};
6666

67-
coke::GenericAwaiter<void> g;
68-
WFRepeaterTask *task = WFTaskFactory::create_repeater_task(creater,
69-
[&g](WFRepeaterTask *) { g.done(); }
70-
);
71-
g.take_over(task);
72-
co_await g;
67+
auto *rep = WFTaskFactory::create_repeater_task(creater, nullptr);
68+
co_await RepeaterAwaiter(rep);
7369
}
7470

7571
coke::Task<> bench_go_name(int max) {

benchmark/bench_graph.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,8 @@ coke::Task<> do_bench_wf(Creater &&creater) {
236236
return nullptr;
237237
};
238238

239-
coke::GenericAwaiter<void> g;
240-
auto callback = [&g] (WFRepeaterTask *) { g.done(); };
241-
auto *rep = WFTaskFactory::create_repeater_task(create, callback);
242-
g.take_over(rep);
243-
244-
co_await g;
239+
auto *rep = WFTaskFactory::create_repeater_task(create, nullptr);
240+
co_await RepeaterAwaiter(rep);
245241
}
246242

247243
// benchmarks

benchmark/bench_timer.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool next(long long &cur) {
5858

5959
template<typename Awaiter>
6060
coke::Task<> detach(Awaiter awaiter) {
61-
co_await awaiter;
61+
co_await std::move(awaiter);
6262
}
6363

6464
template<typename Awaiter>
@@ -72,20 +72,16 @@ coke::Task<> bench_wf_repeat() {
7272
std::mt19937_64 mt(current_msec());
7373
long long i;
7474

75-
coke::GenericAwaiter<void> g;
7675
auto create = [&] (WFRepeaterTask *) -> SubTask * {
7776
if (next(i)) {
7877
int nsec = dist(mt) * 1000;
7978
return WFTaskFactory::create_timer_task(0, nsec, nullptr);
8079
}
8180
return nullptr;
8281
};
83-
auto callback = [&] (WFRepeaterTask *) { g.done(); };
8482

85-
auto *rep = WFTaskFactory::create_repeater_task(create, callback);
86-
g.take_over(rep);
87-
88-
co_await g;
83+
auto *rep = WFTaskFactory::create_repeater_task(create, nullptr);
84+
co_await RepeaterAwaiter(rep);
8985
}
9086

9187
coke::Task<> bench_default_timer() {
@@ -136,7 +132,7 @@ coke::Task<> bench_cancel_by_name() {
136132
name = std::to_string(i);
137133
auto awaiter = coke::sleep(name, microseconds(dist(mt)));
138134
coke::cancel_sleep_by_name(name);
139-
co_await awaiter;
135+
co_await std::move(awaiter);
140136
}
141137
}
142138

@@ -241,7 +237,7 @@ coke::Task<> bench_cancel_by_id() {
241237
id = coke::get_unique_id();
242238
auto awaiter = coke::sleep(id, microseconds(dist(mt)));
243239
coke::cancel_sleep_by_id(id);
244-
co_await awaiter;
240+
co_await std::move(awaiter);
245241
}
246242
}
247243

@@ -455,9 +451,10 @@ int main(int argc, char *argv[]) {
455451
DO_BENCHMARK(timer_by_addr);
456452
DO_BENCHMARK(cancel_by_id);
457453
DO_BENCHMARK(detach_by_id);
458-
DO_BENCHMARK(detach3_by_id);
454+
// disable this test case, it always make workflow deadlock
455+
//DO_BENCHMARK(detach3_by_id);
459456
DO_BENCHMARK(detach_inf_by_id);
460-
DO_BENCHMARK(detach3_inf_by_id);
457+
//DO_BENCHMARK(detach3_inf_by_id);
461458
DO_BENCHMARK(one_id);
462459
DO_BENCHMARK(two_id);
463460
DO_BENCHMARK(ten_id);

include/coke/coke.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "coke/sleep.h"
3636
#include "coke/qps_pool.h"
3737
#include "coke/wait.h"
38-
#include "coke/generic_awaiter.h"
3938
#include "coke/series.h"
4039
#include "coke/semaphore.h"
4140
#include "coke/task.h"

include/coke/dag.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class DagContextBase {
8282
DagContextBase(const DagContextBase &) = delete;
8383
~DagContextBase() = default;
8484

85-
auto operator co_await() { return lt.wait(); }
85+
auto wait() { return lt.wait(); }
8686

8787
void count_down() { lt.count_down(); }
8888

@@ -259,7 +259,7 @@ class DagGraph : public DagGraphBase<T> {
259259
Task<> run(T &data) {
260260
detail::DagContext<T> ctx(Base::nodes.size(), data);
261261
Base::start(ctx);
262-
co_await ctx;
262+
co_await ctx.wait();
263263
}
264264

265265
private:
@@ -284,7 +284,7 @@ class DagGraph<void> : public DagGraphBase<void> {
284284
Task<> run() {
285285
detail::DagContext<void> ctx(Base::nodes.size());
286286
Base::start(ctx);
287-
co_await ctx;
287+
co_await ctx.wait();
288288
}
289289

290290
private:

include/coke/detail/awaiter_base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class AwaiterBase {
3737
static void *create_series(SubTask *first);
3838

3939
public:
40+
constexpr static bool __is_coke_awaitable_type = true;
41+
4042
AwaiterBase() = default;
4143

4244
/**

include/coke/detail/basic_concept.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ concept Queueable = (
5858
template<typename T>
5959
concept IsCokePromise = T::__is_coke_promise_type;
6060

61+
template<typename T>
62+
constexpr bool is_coke_awaitable_v = false;
63+
64+
template<typename T>
65+
concept IsCokeAwaitable = T::__is_coke_awaitable_type || is_coke_awaitable_v<T>;
66+
6167
} // namespace coke
6268

6369
#endif // COKE_DETAIL_CONCEPT_H

0 commit comments

Comments
 (0)