-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTask.hpp
219 lines (210 loc) · 5.16 KB
/
Task.hpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef ___NICE_CORO_TASK_HPP__
#define ___NICE_CORO_TASK_HPP__
#include <thread>
#include "Service.h"
#include "Type.h"
#include <mutex>
#include <condition_variable>
#if !defined(NICE_HAS_CO_AWAIT)
# if !defined(NICE_DISABLE_CO_AWAIT)
# if defined(_MSC_VER)
# if (_MSC_VER >= 1928) && (_MSVC_LANG >= 201705) && !defined(__clang__)
# define NICE_HAS_CO_AWAIT 1
# elif (_MSC_FULL_VER >= 190023506)
# if defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
# define NICE_HAS_CO_AWAIT 1
# endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
# endif // (_MSC_FULL_VER >= 190023506)
# elif defined(__clang__)
# if (__cplusplus >= 201703) && (__cpp_coroutines >= 201703)
# if __has_include(<experimental/coroutine>)
# define NICE_HAS_CO_AWAIT 1
# endif // __has_include(<experimental/coroutine>)
# endif // (__cplusplus >= 201703) && (__cpp_coroutines >= 201703)
# elif defined(__GNUC__)
# if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
# if __has_include(<coroutine>)
# define NICE_HAS_CO_AWAIT 1
# endif // __has_include(<coroutine>)
# endif // (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
# endif // defined(__GNUC__)
# endif // !defined(ASIO_DISABLE_CO_AWAIT)
#endif // !defined(ASIO_HAS_CO_AWAIT)
#ifdef NICE_HAS_CO_AWAIT
#if !__has_include(<experimental/coroutine>)
#include <coroutine>
#else
#include <experimental/coroutine>
#endif
#endif
namespace asio{
class io_context;
}
namespace nicehero {
class semaphore {
public:
semaphore(long count = 0) :count(count) {}
void wait() {
std::unique_lock<std::mutex>lock(mx);
cond.wait(lock, [&]() {return count > 0; });
--count;
}
void signal() {
std::unique_lock<std::mutex>lock(mx);
++count;
cond.notify_one();
}
private:
std::mutex mx;
std::condition_variable cond;
long count;
};
#ifdef NICE_HAS_CO_AWAIT
#if !__has_include(<experimental/coroutine>)
#define STDCORO std
using std::coroutine_handle;
using std::suspend_always;
using std::suspend_never;
#else
#define STDCORO std::experimental
using std::experimental::coroutine_handle;
using std::experimental::suspend_always;
using std::experimental::suspend_never;
#endif
struct suspend_if {
bool _Ready;
explicit suspend_if(bool _Condition) noexcept : _Ready(!_Condition){
}
bool await_ready() noexcept {
return _Ready;
}
void await_suspend(coroutine_handle<>) noexcept {}
void await_resume() noexcept {}
};
template <
// return_type
typename R
// executer_thread
, nicehero::ToService executer
// return_thread default to set executer
, nicehero::ToService return_context = executer>
struct Task {
//////////-----------------------------
struct promise_type {
auto initial_suspend() {
return suspend_if{ executer != gCurrentService };
}
auto final_suspend() noexcept {
//return suspend_if{ return_context != gCurrentService };
return suspend_never{};
}
auto get_return_object() {
return Task<R, executer, return_context>(this);
}
void unhandled_exception() {
std::terminate();
}
//void return_void() {}
template<typename U>
void return_value(U&& value) {
if (m_task) {
if (m_task->first_context != executer) {
m_semaphore.wait();
}
m_task->ret = std::move(value);
auto h = m_task->m_handle;
if (!h) {
return;
}
if (gCurrentService == return_context)
{
h.resume();
return;
}
nicehero::post([h]() {
if (!h) {
return;
}
h.resume();
}, return_context);
}
}
promise_type() {
m_task = nullptr;
}
~promise_type() {
if (m_task) {
m_task->m_promise = nullptr;
}
m_task = nullptr;
}
friend struct Task<R, executer, return_context>;
protected:
Task* m_task = nullptr;
semaphore m_semaphore;
};
bool await_ready() const {
return false;
}
R await_resume() {
return std::move(ret);
}
void await_suspend(coroutine_handle<> handle) {
if (first_context == executer && return_context == executer) {
handle.resume();
return;
}
if (first_context == executer && return_context != executer)
{
m_handle2 = handle;
nicehero::post([this] {
m_handle2.resume();
},return_context);
return;
}
m_handle = handle;
if (m_promise) {
m_promise->m_semaphore.signal();
}
}
Task(promise_type* p) {
m_promise = p;
if (m_promise) {
m_promise->m_task = this;
}
first_context = gCurrentService;
if (executer != gCurrentService)
{
nicehero::post([p] {
auto handle = coroutine_handle<promise_type>::from_promise(*p);
handle.resume();
},executer);
}
}
~Task() {
if (m_promise) {
m_promise->m_task = nullptr;
}
}
Task(R&& r) :ret(r) {
hasRet = true;
}
friend struct promise_type;
protected:
promise_type* m_promise = nullptr;
R ret;
bool hasRet = false;
coroutine_handle<> m_handle = nullptr;
coroutine_handle<> m_handle2 = nullptr;
nicehero::ToService first_context = TO_NONE;
};
#else
template <typename R, nicehero::ToService executer, nicehero::ToService return_context = executer>
struct Task
{
Task(R&& r) :ret(std::move(r)) { }
R ret;
};
#endif
}
#endif