-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.h
398 lines (352 loc) · 7.96 KB
/
channel.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#ifndef _CU_CHANNEL_H_
#define _CU_CHANNEL_H_
#include <iostream>
#include <vector>
#include <stack>
#include <boost/bind.hpp>
#include <coroutine/coroutine.h>
#include "semaphore.h"
#include <fast-event-system/sem.h>
#include <fast-event-system/async_delay.h>
#include <assert.h>
#include <mutex>
namespace cu {
template <typename T> class channel;
template <typename T> struct optional;
namespace detail {
template <typename R>
static auto _pipe(std::vector< typename cu::link< optional<R> > >& links, R input)
{
std::vector<R> output;
std::vector< typename cu::channel<R>::generator > coros;
coros.emplace_back( cu::make_generator< optional<R> >( [&](auto& yield) { yield(input); }) );
for (auto& f : links)
{
coros.emplace_back( cu::make_generator< optional<R> >(boost::bind(f, boost::ref(*coros.back().get()), _1) ) );
}
auto pipeline = {
[&output]() -> typename cu::channel<R>::link
{
return [&output](auto& source, auto& yield)
{
for (auto& s : source)
{
if(s)
{
output.emplace_back(*s);
}
}
};
}()
};
for (auto& f : pipeline)
{
coros.emplace_back( cu::make_generator< optional<R> >(boost::bind(f, boost::ref(*coros.back().get()), _1) ) );
}
return output;
}
}
// problema: no es posible tener: channel<bool> por las ambiguedades en el constructor
template <typename T>
struct optional
{
optional(T data) : _data(std::move(data)), _invalid(false) { ; }
explicit optional(T&& data) : _data(data), _invalid(false) { ; }
explicit optional() : _data(), _invalid(false) { ; }
explicit optional(bool close) : _data(), _invalid(close) { ; }
const T& operator*() const
{
return _data;
}
T& operator*()
{
return _data;
}
operator bool() const
{
return !_invalid;
}
T _data;
bool _invalid;
};
template <typename T>
auto term_receiver(const typename channel<T>::coroutine& receiver)
{
return [=](typename channel<T>::in& source)
{
for(auto& s : source)
{
(*receiver)(s);
}
};
}
template <typename T>
class channel
{
public:
using in = cu::pull_type< optional<T> >;
using out = cu::push_type< optional<T> >;
using link = cu::link< optional<T> >;
using coroutine = push_type_ptr< optional<T> >;
using generator = pull_type_ptr< optional<T> >;
explicit channel(cu::parallel_scheduler& sche, size_t buffer = 0)
: _sche(sche)
, _buffer(buffer)
, _elements(sche, 0)
, _slots(sche, buffer + 1)
{
_set_tail();
}
template <typename Function>
explicit channel(cu::parallel_scheduler& sche, size_t buffer, Function&& f)
: _sche(sche)
, _buffer(buffer)
, _elements(sche, 0)
, _slots(sche, buffer + 1)
{
_set_tail();
_add(std::forward<Function>(f));
}
template <typename Function, typename ... Functions>
explicit channel(cu::parallel_scheduler& sche, size_t buffer, Function&& f, Functions&& ... fs)
: _sche(sche)
, _buffer(buffer)
, _elements(sche, 0)
, _slots(sche, buffer + 1)
{
_set_tail();
_add(std::forward<Function>(f), std::forward<Functions>(fs)...);
}
template <typename Function>
void pipeline(Function&& f)
{
_add(std::forward<Function>(f));
}
template <typename Function, typename ... Functions>
void pipeline(Function&& f, Functions&& ... fs)
{
_add(std::forward<Function>(f), std::forward<Functions>(fs)...);
}
template <typename R>
void operator()(const R& data)
{
for(auto& e : pipe(T(data)))
{
_slots.wait();
(*_coros.top())( optional<T>(e) );
_elements.notify();
}
}
template <typename R>
void operator()(cu::yield_type& yield, const R& data)
{
for(auto& e : pipe(T(data)))
{
_slots.wait(yield);
(*_coros.top())( optional<T>(e) );
_elements.notify(yield);
if(full())
{
yield( cu::control_type{} );
}
}
}
void send_stdin()
{
for (std::string line; std::getline(std::cin, line);)
{
operator()<std::string>(line);
}
}
void send_stdin(cu::yield_type& yield)
{
for (std::string line; std::getline(std::cin, line);)
{
operator()<std::string>(yield, line);
}
}
optional<T> get()
{
_elements.wait();
optional<T> data = std::get<0>(_buf.get());
_slots.notify();
return std::move(data);
}
optional<T> get(cu::yield_type& yield)
{
if(_buf.empty())
{
yield( cu::control_type{} );
}
_elements.wait(yield);
optional<T> data = std::get<0>(_buf.get(yield));
_slots.notify(yield);
return std::move(data);
}
inline bool empty() const
{
return (_elements.size() <= 0);
}
inline bool full() const
{
return (_slots.size() <= 0);
}
void close()
{
_slots.wait();
(*_coros.top())( optional<T>(true) );
_elements.notify();
}
void close(cu::yield_type& yield)
{
_slots.wait(yield);
(*_coros.top())( optional<T>(true) );
_elements.notify(yield);
yield( cu::control_type{} );
}
protected:
template <typename R>
auto pipe(const R& input)
{
return cu::detail::_pipe<R>(_links, input);
}
void _set_tail()
{
auto r = cu::make_iterator< optional<T> >(
[this](auto& source) {
for(auto& s : source)
{
this->_buf(0, fes::deltatime(0), s);
}
}
);
_coros.push( cu::make_iterator< optional<T> >( term_receiver<T>(r) ) );
}
template <typename Function>
void _add(Function&& f)
{
_links.emplace(_links.begin(), std::forward<Function>(f));
}
template <typename Function, typename ... Functions>
void _add(Function&& f, Functions&& ... fs)
{
_add(std::forward<Functions>(fs)...);
_links.emplace(_links.begin(), std::forward<Function>(f));
}
protected:
cu::parallel_scheduler& _sche;
size_t _buffer;
std::stack< coroutine > _coros;
fes::async_delay< optional<T> > _buf;
cu::semaphore _elements;
cu::semaphore _slots;
std::vector<link> _links;
};
template <typename T>
inline int _which(int n, const cu::channel<T>& chan)
{
if (chan.empty())
return -1;
else
return n;
}
template <typename T, typename... Args>
inline int _which(int n, const cu::channel<T>& chan, const cu::channel<Args>&... chans)
{
if (chan.empty())
return cu::_which(n + 1, chans...);
else
return n;
}
template <typename... Args>
inline int select_nonblock(cu::yield_type& yield, const cu::channel<Args>&... chans)
{
return cu::_which(0, chans...);
}
template <typename... Args>
inline int select(cu::yield_type& yield, const cu::channel<Args>&... chans)
{
int n;
do
{
n = select_nonblock(yield, chans...);
if(n == -1)
{
yield( cu::control_type{} );
}
} while(n == -1);
return n;
}
template <size_t N, typename T, typename ... STUFF>
bool _barrier(cu::yield_type& yield, cu::optional< std::tuple<STUFF...> >& tpl, cu::channel<T>& chan)
{
cu::optional<T> a;
if(cu::select(yield, chan) == 0)
{
a = chan.get(yield);
if(a)
std::get<N>(*tpl) = *a;
else
return false;
}
return true;
}
template <size_t N, typename T, typename ... Args, typename ... STUFF>
bool _barrier(cu::yield_type& yield, cu::optional< std::tuple<STUFF...> >& tpl, cu::channel<T>& chan, cu::channel<Args>&... chans)
{
cu::optional<T> a;
if(cu::select(yield, chan) == 0)
{
a = chan.get(yield);
if(a)
std::get<N>(*tpl) = *a;
else
return false;
}
return _barrier<N+1>(yield, tpl, chans...);
}
template <typename ... Args>
cu::optional< std::tuple<Args...> > barrier(cu::yield_type& yield, cu::channel<Args>&... chans)
{
cu::optional< std::tuple<Args...> > tpl(false);
bool ok = _barrier<0>(yield, tpl, chans...);
if(!ok)
{
return cu::optional< std::tuple<Args...> >(true);
}
return tpl;
}
template <typename ... Args>
auto range(cu::yield_type& yield, cu::channel<Args>&... chans)
{
return cu::pull_type< std::tuple<Args...> >(
[&](cu::push_type< std::tuple<Args...> >& own_yield) {
for(;;)
{
auto data = cu::barrier(yield, chans...);
if(data)
own_yield(*data);
else
break; // detect close or exception
}
}
);
}
template <typename T>
auto range(cu::yield_type& yield, cu::channel<T>& chan)
{
return cu::pull_type<T>(
[&](cu::push_type<T>& own_yield) {
for(;;)
{
auto data = chan.get(yield);
if(data)
own_yield(*data);
else
break; // detect close or exception
}
}
);
};
}
#endif