/
select.pmc
411 lines (298 loc) · 10.1 KB
/
select.pmc
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Copyright (C) 2011, Parrot Foundation.
=head1 NAME
src/pmc/select.pmc - IO Select PMC
=head1 DESCRIPTION
This is the base-class for non-blocking IO using select
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "../src/io/io_private.h"
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define SELECT_READ 1
#define SELECT_WRITE 2
#define SELECT_ERROR 4
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
/*
=item C<INTVAL get_fd(INTEPR, PMC* filehandle)>
Returns the c89 standard integer file descriptor for the filehandle
=cut
*/
static INTVAL get_fd(PARROT_INTERP, PMC* handle) {
INTVAL fd = -1;
switch (handle->vtable->base_type) {
case enum_class_FileHandle:
{
PIOHANDLE os_handle = Parrot_io_get_os_handle(interp, handle);
fd = os_handle;
}
break;
default:
abort();
}
return fd;
}
/*
=item C<INTVAL find_max_fd(INTERP, PMC *fd_map)>
Returns the maximum of all the c89 standard integer file descriptors held by
the Hash PMC
=back
=head2 Vtable Functions
=over 4
=cut
*/
static INTVAL find_max_fd(PARROT_INTERP, ARGIN(PMC *fd_map)) {
PMC * const iter = VTABLE_get_iter(interp, fd_map);
const INTVAL n = VTABLE_elements(interp, fd_map);
INTVAL maxid = -1;
INTVAL j;
for (j = 0; j < n; ++j) {
INTVAL id = VTABLE_shift_integer(interp, iter);
maxid = max(maxid, id);
}
return maxid;
}
pmclass Select auto_attrs {
ATTR PMC *fd_map; /* map a fd to its PMC */
ATTR fd_set rb_array; /* read bit array */
ATTR fd_set wb_array; /* write bit array */
ATTR fd_set eb_array; /* error bit array */
ATTR INTVAL maxfd;
/*
=item C<void init()>
Initializes the PMC.
=cut
*/
VTABLE void init() {
PMC *fd_map = pmc_new(interp, enum_class_Hash);
SET_ATTR_fd_map(INTERP, SELF, fd_map);
SET_ATTR_maxfd(INTERP, SELF, -1);
FD_ZERO(&(PARROT_SELECT(SELF)->rb_array));
FD_ZERO(&(PARROT_SELECT(SELF)->wb_array));
FD_ZERO(&(PARROT_SELECT(SELF)->eb_array));
}
/*
=back
=head2 Methods
=over 4
=item C<METHOD INTVAL getfd(PMC* filehandle)>
Returns the c89 standard integer file descriptor for the filehandle
=cut
*/
METHOD getfd(PMC* handle) {
INTVAL i = get_fd(interp, handle);
RETURN(INTVAL i)
}
/*
=item C<METHOD INTVAL getmaxfd()>
Returns the maximum of all the c89 standard integer file descriptors held by
the Select PMC
=cut
*/
METHOD getmaxfd() {
INTVAL v;
GET_ATTR_maxfd(INTERP, SELF, v);
RETURN(INTVAL v);
}
/*
=item C<METHOD PMC* get_fd_map()>
Returns the Hash PMC that holds the mapping from file descriptor to the opaque
PMC associated with the file descriptor when it was added to the Select PMC.
Used mainly for testing purposes.
=cut
*/
METHOD get_fd_map() {
PMC *fd_map;
GET_ATTR_fd_map(INTERP, SELF, fd_map);
RETURN(PMC *fd_map);
}
/*
=item C<METHOD update(PMC* filehandle, PMC* data, INTVAL read_write_error_flag)>
Adds filehandle PMC to the Select PMC for monitoring based on the
read_write_error_flags
read_write_error_flag = READ (0x1) | WRITE (0x2) | ERROR (0x4)
An array of data PMCs are returned by the can_read, can_read, has_exception,
and select methods when the filehandles meet one of read_write_error_flag
conditions.
=cut
*/
METHOD update(PMC *handle, PMC* data, INTVAL rwe) {
PMC *fd_map;
INTVAL maxid;
INTVAL fdkey = get_fd(interp, handle);
GET_ATTR_fd_map(INTERP, SELF, fd_map);
GET_ATTR_maxfd(INTERP, SELF, maxid);
VTABLE_set_pmc_keyed_int(interp, fd_map, fdkey, data);
PARROT_GC_WRITE_BARRIER(interp, fd_map);
if (rwe & SELECT_READ) { FD_SET(fdkey, &PARROT_SELECT(SELF)->rb_array); }
if (rwe & SELECT_WRITE) { FD_SET(fdkey, &PARROT_SELECT(SELF)->wb_array); }
if (rwe & SELECT_ERROR) { FD_SET(fdkey, &PARROT_SELECT(SELF)->eb_array); }
maxid = max(maxid, fdkey);
SET_ATTR_maxfd(INTERP, SELF, maxid);
}
/*
=item C<METHOD remove(PMC* filehandle)>
Removes filehandle from the Select PMC.
=cut
*/
METHOD remove(PMC *handle) {
PMC *fdkey, *fd_map;
INTVAL fd = get_fd(interp, handle);
INTVAL maxid;
GET_ATTR_fd_map(INTERP, SELF, fd_map);
GET_ATTR_maxfd(INTERP, SELF, maxid);
VTABLE_delete_keyed_int(interp, fd_map, fd);
PARROT_GC_WRITE_BARRIER(interp, fd_map);
FD_CLR(fd, &PARROT_SELECT(SELF)->rb_array);
FD_CLR(fd, &PARROT_SELECT(SELF)->wb_array);
FD_CLR(fd, &PARROT_SELECT(SELF)->eb_array);
if (fd == maxid) {
maxid = find_max_fd(interp, fd_map);
SET_ATTR_maxfd(INTERP, SELF, maxid);
}
}
/*
=item C<METHOD can_read()>
Returns the associated data for the file descriptors in the Select PMC, which
are ready to be read from.
Waits for a maximum of timeout seconds for a file descriptor to be ready to
read before returning.
=cut
*/
METHOD can_read(INTVAL timeout) {
fd_set rdset;
struct timeval timeouts;
PMC *results, *fd_map;
INTVAL maxid, i;
GET_ATTR_fd_map(INTERP, SELF, fd_map);
GET_ATTR_maxfd(INTERP, SELF, maxid);
timeouts.tv_sec = timeout;
timeouts.tv_usec = 0;
results = pmc_new(interp, enum_class_ResizablePMCArray);
memcpy(&rdset, &PARROT_SELECT(SELF)->rb_array, sizeof (fd_set));
select(maxid + 1, &rdset, NULL, NULL, &timeouts);
for (i=0; i <= maxid; i++) {
if (FD_ISSET(i, &rdset)) {
VTABLE_push_pmc(interp, results, VTABLE_get_pmc_keyed_int(interp, fd_map, i));
PARROT_GC_WRITE_BARRIER(interp, results);
}
}
RETURN(PMC *results);
}
/*
=item C<METHOD can_write()>
Returns the associated data for the file descriptors in the Select PMC, which
are ready to be written to.
Waits for a maximum of timeout seconds for a file descriptor to be ready to
write to before returning.
=cut
*/
METHOD can_write(INTVAL timeout) {
fd_set wbset;
struct timeval timeouts;
PMC *results, *fd_map;
INTVAL maxid, i;
GET_ATTR_fd_map(INTERP, SELF, fd_map);
GET_ATTR_maxfd(INTERP, SELF, maxid);
timeouts.tv_sec = timeout;
timeouts.tv_usec = 0;
results = pmc_new(interp, enum_class_ResizablePMCArray);
memcpy(&wbset, &PARROT_SELECT(SELF)->wb_array, sizeof (fd_set));
select(maxid + 1, NULL, &wbset, NULL, &timeouts);
for (i=0; i<=maxid; i++) {
if (FD_ISSET(i, &wbset)) {
VTABLE_push_pmc(interp, results, VTABLE_get_pmc_keyed_int(interp, fd_map, i));
PARROT_GC_WRITE_BARRIER(interp, results);
}
}
RETURN(PMC *results);
}
/*
=item C<METHOD has_exception()>
Returns the associated data for the file descriptors in the Select PMC, which
are in an exception state.
Waits for a maximum of timeout seconds for a file descriptor to be in an
exception state before returning.
=cut
*/
METHOD has_exception(INTVAL timeout) {
struct timeval timeouts;
PMC *results, *fd_map;
INTVAL maxid, i;
fd_set ebset;
GET_ATTR_fd_map(INTERP, SELF, fd_map);
GET_ATTR_maxfd(INTERP, SELF, maxid);
timeouts.tv_sec = timeout;
timeouts.tv_usec = 0;
results = pmc_new(interp, enum_class_ResizablePMCArray);
memcpy(&ebset, &PARROT_SELECT(SELF)->eb_array, sizeof (fd_set));
select(maxid + 1, NULL, NULL, &ebset, &timeouts);
for (i=0; i<=maxid; i++) {
if (FD_ISSET(i, &ebset)) {
VTABLE_push_pmc(interp, results, VTABLE_get_pmc_keyed_int(interp, fd_map, i));
PARROT_GC_WRITE_BARRIER(interp, results);
}
}
RETURN(PMC *results);
}
/*
=item C<METHOD can_read()>
Returns the associated data for the file descriptors in the Select PMC, which
are ready to be read, written, or have an exception to be handled.
Waits for a maximum of timeout seconds for a file descriptor to be ready to be
handled.
=back
=cut
*/
METHOD select(INTVAL timeout) {
fd_set rdset, wrset, erset;
struct timeval timeouts;
PMC *results, *rresults, *wresults, *eresults, *fd_map;
INTVAL maxid, i;
GET_ATTR_fd_map(INTERP, SELF, fd_map);
GET_ATTR_maxfd(INTERP, SELF, maxid);
timeouts.tv_sec = timeout;
timeouts.tv_usec = 0;
results = pmc_new(interp, enum_class_ResizablePMCArray);
rresults = pmc_new(interp, enum_class_ResizablePMCArray);
wresults = pmc_new(interp, enum_class_ResizablePMCArray);
eresults = pmc_new(interp, enum_class_ResizablePMCArray);
memcpy(&rdset, &PARROT_SELECT(SELF)->rb_array, sizeof (fd_set));
memcpy(&wrset, &PARROT_SELECT(SELF)->wb_array, sizeof (fd_set));
memcpy(&erset, &PARROT_SELECT(SELF)->eb_array, sizeof (fd_set));
select(maxid + 1, &rdset, &wrset, &erset, &timeouts);
for (i=0; i<=maxid; i++) {
if (FD_ISSET(i, &rdset)) {
VTABLE_push_pmc(interp, rresults, VTABLE_get_pmc_keyed_int(interp, fd_map, i));
PARROT_GC_WRITE_BARRIER(interp, rresults);
}
if (FD_ISSET(i, &wrset)) {
VTABLE_push_pmc(interp, wresults, VTABLE_get_pmc_keyed_int(interp, fd_map, i));
PARROT_GC_WRITE_BARRIER(interp, wresults);
}
if (FD_ISSET(i, &erset)) {
VTABLE_push_pmc(interp, eresults, VTABLE_get_pmc_keyed_int(interp, fd_map, i));
PARROT_GC_WRITE_BARRIER(interp, eresults);
}
}
VTABLE_push_pmc(interp, results, rresults);
VTABLE_push_pmc(interp, results, wresults);
VTABLE_push_pmc(interp, results, eresults);
PARROT_GC_WRITE_BARRIER(interp, results);
RETURN(PMC *results);
}
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/