-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.h
77 lines (61 loc) · 1.53 KB
/
Task.h
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
#pragma once
#include "ExecutionContext.h"
template <typename T>
class Promise;
template <typename T>
class Future;
/*
* Represents allocated, but not-started coroutine, which is not yet assigned to any executor.
*/
template <typename T>
class Task {
public:
using promise_type = Promise<T>;
Task(const Task&) = delete;
Task(Task&& other) : promise_(other.promise_) {
other.promise_ = nullptr;
}
~Task() {
assert(!promise_);
}
Future<T> startInline(ExecutionContext ec) && {
assert(ec.executor->isInExecutor());
promise_->executionContext_ = ec;
promise_->start();
return{ *std::exchange(promise_, nullptr) };
}
Future<T> start(ExecutionContext ec) && {
promise_->executionContext_ = ec;
ec.executor->add([promise = promise_] {
promise->start();
});
return{ *std::exchange(promise_, nullptr) };
}
private:
friend class promise_type;
Task(promise_type& promise) : promise_(&promise) {
}
Promise<T>* promise_;
};
/*
* Represents non-allocated coroutine, stores references to all coroutine arguments.
*/
template <typename F, typename... Args>
class CallableTask {
public:
CallableTask(F&& f, Args&&... args) :
f_(BindRef()(std::forward<F>(f), std::forward<Args>(args)...)) {}
auto init(AllocatorPtr allocator) {
return f_(allocator);
}
private:
struct BindRef {
auto operator()(F&& f, Args&&... args) {
return [&](AllocatorPtr allocator) mutable {
return f(allocator, std::forward<Args>(args)...);
};
}
};
using Func = typename std::result_of<BindRef(F&&, Args&&...)>::type;
Func f_;
};