-
Notifications
You must be signed in to change notification settings - Fork 2
/
cat.cpp
341 lines (291 loc) · 8.25 KB
/
cat.cpp
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
#include <aio.h>
#include <errno.h>
#include <iostream>
#include <vector>
/* Test program to start building an asynchronous i/o
* framework. Things that gots to be done
*
* 1) AsynchOp classes need to be rearranged and filled out. A first step is
* to generalize the interface enough to support multiple implementations,
* such as a select() based implementation of asynchronous read and writes
* for nonblocking descriptors.
*
* 2) Then the ultimate goal is to be able to figure out a nice way to
* implement complex asynchronous operations on top of primitive ones (i.e.
* readline on top of read). I have some ideas for using cooperative
* threads (fibers) for this. It'll require another overhaul of wait().
*/
struct NullType;
// Forward declaration
template <typename OPERATION, typename RESULT>
struct ResultNode;
//! Expression template for holding && operands in wait expressions
template<typename LEFT, typename RIGHT>
struct AndNode
{
typedef LEFT Left;
typedef RIGHT Right;
typedef AndNode<Left, Right> This;
//! Template metafunction to figure out member types from operands
template<typename OPERAND, int EXPR_LEAF=OPERAND::EXPR_LEAF>
struct Operand2Member
{
// If Operand is an leaf (i.e. an AsynchOp object) just hold a pointer
typedef ResultNode<OPERAND, NullType> type;
static void assign(type &member, OPERAND &operand)
{
member.operation = &operand;
}
};
template<typename OPERAND>
struct Operand2Member<OPERAND, false>
{
// If Operand is an AndNode, contain and copy the whole object
typedef OPERAND type;
static void assign(type &member, OPERAND const &operand)
{
member = operand;
}
};
typedef typename Operand2Member<Left>::type LeftMember;
typedef typename Operand2Member<Right>::type RightMember;
LeftMember left;
RightMember right;
enum {EXPR_LEAF=false};
// Why the multiple declarations? The constructors must accept non-const
// reference arguments because we store non-const pointers to AsynchOp
// objects so they can be modified by wait(). But since we must accept
// rvalues to support nested expressions, and the current C++ standard
// doesn't allow non-const references to rvalues, we have to accept const
// references as well. The ideal solution would be to accept rvalue
// references, which have been proposed for a future version of C++, and
// would match both types of arguments. In the meantime, just catch
// both types of arguments by exhausting the combinations.
AndNode(LEFT & left_, RIGHT & right_)
{
Operand2Member<Left>::assign(left, left_);
Operand2Member<Right>::assign(right, right_);
}
AndNode(LEFT const & left_, RIGHT & right_)
{
Operand2Member<Left>::assign(left, left_);
Operand2Member<Right>::assign(right, right_);
}
AndNode(LEFT & left_, RIGHT const & right_)
{
Operand2Member<Left>::assign(left, left_);
Operand2Member<Right>::assign(right, right_);
}
AndNode(LEFT const & left_, RIGHT const & right_)
{
Operand2Member<Left>::assign(left, left_);
Operand2Member<Right>::assign(right, right_);
}
AndNode()
{
}
// accept non-const refs and rvalues (see "multiple declarations" above)
template<typename R>
AndNode<This, R> operator&&(R & r) const
{
AndNode<This, R> a(*this, r);
return a;
}
template<typename R>
AndNode<This, R> operator&&(R const & r) const
{
AndNode<This, R> a(*this, r);
return a;
}
};
//! Expression template for holding >> operands in wait expressions
template <typename OPERATION, typename RESULT>
struct ResultNode
{
typedef ResultNode<OPERATION, RESULT> This;
enum {EXPR_LEAF=false};
ResultNode(OPERATION & operation_, RESULT & result_)
: operation(&operation_), result(&result_)
{}
ResultNode()
: operation(NULL), result(NULL)
{}
// accept non-const refs and rvalues (see "multiple declarations" above)
template<typename R>
AndNode<This, R> operator&&(R & r) const
{
AndNode<This, R> a(*this, r);
return a;
}
template<typename R>
AndNode<This, R> operator&&(R const & r) const
{
AndNode<This, R> a(*this, r);
return a;
}
OPERATION * operation;
RESULT * result;
};
//! Asynchronous Operation Base Class
struct AsynchOp
{
enum {EXPR_LEAF=true};
};
//! Posix Asynchronous Operation
struct AioOp : public AsynchOp, public aiocb
{
void init(int filedes, off_t offset, char * buffer, size_t len)
{
this->aio_fildes = filedes;
this->aio_offset = offset;
this->aio_buf= buffer;
this->aio_nbytes = len;
this->aio_sigevent.sigev_notify = SIGEV_NONE;
}
template<typename R>
AndNode<AioOp, R> operator&&(R & r)
{
AndNode<AioOp, R> n(*this, r);
return n;
}
template<typename R>
ResultNode<AioOp, R> operator>>(R & r)
{
ResultNode<AioOp, R> n(*this, r);
return n;
}
struct Result
{
aiocb * cb;
int error()
{
return aio_error(cb);
}
};
};
struct IOError
{
IOError(int error_, char const * file_, int line_)
: error(error_), file(file_), line(line_)
{}
int error;
char const * file;
int line;
};
#define EARGS , __FILE__, __LINE__
struct WriteOp : public AioOp
{
void init(int filedes, off_t offset, char const * buffer, size_t len)
{
AioOp::init(filedes, offset, const_cast<char *>(buffer), len);
if (aio_write(this))
throw IOError(errno EARGS);
}
};
struct ReadOp : public AioOp
{
void init(int filedes, off_t offset, char * buffer, size_t len)
{
AioOp::init(filedes, offset, buffer, len);
if (aio_read(this))
throw IOError(errno EARGS);
}
struct Result : AioOp::Result
{
int bytesRead()
{
return aio_return(cb);
}
};
};
void wait(const aiocb * list[], size_t len)
{
bool inProgress;
do
{
if (aio_suspend(list, len, NULL))
throw IOError(errno EARGS);
inProgress = false;
for (unsigned i=0; i<len; ++i)
{
if (list[i])
{
int error = aio_error(list[i]);
if (error == EINPROGRESS)
inProgress = true;
else
list[i] = NULL;
}
}
}
while (inProgress);
}
typedef std::vector<const aiocb*> WaitList;
template<typename WAIT_EXPR>
void wait(WAIT_EXPR expr)
{
// Since the list values are known at compile-time, it'd wouldn't be
// impossible to put them in a recursive struct, which could be cast
// as an array and passed to aio_suspend. It's not really worth the
// trouble though since if I ever implement the cooperating threading
// scheme the list'll need to be built dynamically.
WaitList waitList;
buildList(waitList, expr);
wait(&*waitList.begin(), waitList.size());
}
template<typename LEFT, typename RIGHT>
static inline void buildList(WaitList & waitList, AndNode<LEFT, RIGHT> const & node)
{
buildList(waitList, node.left);
buildList(waitList, node.right);
}
template<typename OPERATION>
static inline void
buildList(WaitList & waitList, ResultNode<OPERATION, NullType> const & node)
{
waitList.push_back(node.operation);
}
template<typename OPERATION, typename RESULT>
static inline void
buildList(WaitList & waitList, ResultNode<OPERATION, RESULT> const & node)
{
waitList.push_back(node.operation);
node.result->cb = node.operation;
}
int main(int, const char *[])
{
try
{
const int BUFFER_SIZE = 4096;
char readBuffer[BUFFER_SIZE], *readPtr(readBuffer);
char writeBuffer[BUFFER_SIZE], *writePtr(writeBuffer);
ReadOp readOp;
WriteOp writeOp;
int error;
off_t offset = 0;
readOp.init(STDIN_FILENO, offset, readPtr, BUFFER_SIZE);
for (;;)
{
ReadOp::Result readResult;
WriteOp::Result writeResult;
wait(readOp >> readResult && writeOp >> writeResult);
error = readResult.error();
if (error)
throw IOError(error EARGS);
error = writeResult.error();
if (error)
throw IOError(error EARGS);
ssize_t readLen = readResult.bytesRead();
if (readLen == 0)
break;
std::swap(readPtr, writePtr);
writeOp.init(STDOUT_FILENO, offset, writePtr, readLen);
readOp.init(STDIN_FILENO, offset += readLen, readPtr, BUFFER_SIZE);
}
}
catch (IOError e)
{
std::cerr << "IO Error at " << e.file << ":" << e.line << std::endl;
std::cerr << strerror(e.error) << std::endl;
}
}