-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoco.h
233 lines (202 loc) · 6.14 KB
/
coco.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once
// MIT License
//
// Copyright (c) 2024 jinhua luo, luajit.io@gmail.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <functional>
#include <map>
#include <queue>
#include <string>
#define coco_begin() \
{ \
auto __tag = __LINE__; \
auto __it = _st->state.find(__tag); \
auto __st = (__it == _st->state.end()) ? -1 : __it->second; \
switch (__st) { \
case -1: { \
(void)1;
#define coco_end() \
} \
break; \
default: \
break; \
} \
; \
_st->state[__tag] = -1; \
}
#define coco_done() return co_t::DONE;
#define coco_yield() \
_st->state[__tag] = __LINE__; \
return co_t::YIELD; \
break; \
} \
case __LINE__: { \
(void)1;
#define COCO_TOKEN_(x, y) x##y
#define COCO_TOKEN(x, y) COCO_TOKEN_(x, y)
#define COCO_CHAN_OP_(op, ch, t, ok) \
COCO_TOKEN(tag1, __LINE__) : \
{ \
auto ret = ch->op(__self, t); \
if (ret != co_t::YIELD) { \
ok = (ret == co_t::TRUE) ? true : false; \
goto COCO_TOKEN(tag2, __LINE__); \
} \
} \
coco_yield() goto COCO_TOKEN(tag1, __LINE__); \
COCO_TOKEN(tag2, __LINE__) : (void)1;
#define coco_write_chan(ch, t, ok) COCO_CHAN_OP_(put, ch, t, ok)
#define coco_read_chan(ch, t, ok) COCO_CHAN_OP_(get, ch, t, ok)
#define coco_wait(wg) \
COCO_TOKEN(tag1, __LINE__) : if (wg->wait(__self)) \
{ \
goto COCO_TOKEN(tag2, __LINE__); \
} \
coco_yield() goto COCO_TOKEN(tag1, __LINE__); \
COCO_TOKEN(tag2, __LINE__) : (void)1;
namespace coco
{
struct state_t {
std::map<int, int> state;
virtual ~state_t() {}
};
class co_t
{
public:
enum ret_t {
FALSE,
TRUE,
YIELD,
DONE,
};
typedef std::function<ret_t(co_t *, state_t *)> fn_t;
private:
state_t *st;
fn_t fn;
public:
ret_t ret = TRUE;
co_t(fn_t const &fn, state_t *st = new state_t) : st(st), fn(fn) {}
bool done() { return ret == DONE; }
ret_t resume()
{
if (done())
return DONE;
ret = fn(this, st);
return ret;
}
~co_t() { delete st; }
};
inline co_t *go(co_t::fn_t const &fn, state_t *st = new state_t)
{
auto co = new co_t(fn, st);
co->resume();
return co;
}
template <typename T> class chan_t
{
typedef std::function<void(T)> close_fn_t;
size_t cap = 1;
std::queue<T> data;
std::queue<co_t *> rq;
std::queue<co_t *> wq;
bool closed_ = false;
public:
chan_t(int cap = 0) : cap(cap) {}
bool ready() { return !data.empty(); }
bool closed() { return closed_; }
co_t::ret_t get(co_t *co, T &t)
{
if (!data.empty()) {
t = data.front();
data.pop();
if (!wq.empty()) {
auto waiter = wq.front();
wq.pop();
waiter->resume();
}
return co_t::TRUE;
}
if (closed())
return co_t::FALSE;
rq.push(co);
return co_t::YIELD;
}
co_t::ret_t put(co_t *co, T t)
{
if (closed())
return co_t::FALSE;
data.push(t);
if (!rq.empty()) {
auto waiter = rq.front();
rq.pop();
waiter->resume();
return co_t::TRUE;
}
if (data.size() > cap) {
rq.push(co);
return co_t::YIELD;
}
return co_t::TRUE;
}
void close(close_fn_t fn = nullptr)
{
closed_ = true;
while (!rq.empty()) {
auto w = rq.front();
rq.pop();
w->resume();
}
while (!wq.empty()) {
auto w = wq.front();
wq.pop();
w->resume();
}
if (fn) {
while (!data.empty()) {
auto &v = data.front();
fn(v);
}
}
}
};
class wait_group_t
{
co_t *waiter = nullptr;
int cnt = 0;
public:
void add(int delta) { cnt += delta; }
void done()
{
cnt--;
if (cnt == 0 && waiter) {
auto co = waiter;
waiter = nullptr;
co->resume();
}
}
bool wait(co_t *w)
{
if (cnt == 0)
return true;
waiter = w;
return false;
}
};
} // namespace coco